2014-09-05 109 views
-1

希望有人可以帮助使用此代码。我有一个PHP文件与foreach循环来从MySQL数据库中获取数据。当用户点击php文件中的更新时,这个JS脚本被调用。我正在使用“this”来获取单个值。我遇到的问题是没有任何东西传递给JS脚本。将Foreach数据从PHP传递到Ajax

这里是PHP代码,PHP代码在使用echo语句进行测试时工作正常。

<?php 
$artist_about_already = artist_news_check($artist_id); 
if ($artist_about_already == true) { 
    $artist_news = (get_artist_news_all($artist_id)); 
foreach ($artist_news as $news) { 
    $news_id  = $news['id']; 
    $timestamp = $news['timestamp']; 
    $title  = $news['title']; 
    $news  = $news['news']; 
    $news_timestamp = date('D M d Y @ h:i:s', $timestamp); 
?> 
<div class="well no-padding"> 
    <form action="" method="post" name="artist_update_news" id="artist_update_news" class="smart-   form client-form"> 

           <fieldset> 
           <section> 
<strong>Posted on: <?php echo $news_timestamp ?></strong> 

            <label class="input"> <i class="icon-append fa fa-user"></i> 
             <input type="text" name="artist_update_news_title"  id="artist_update_news_title" value="<?php echo $title ?>"> 
             <b class="tooltip tooltip-bottom-right">Needed to update Artist News</b> </label> 
           </section> 
          <section> 
           <label class="textarea">           
            <textarea rows="5" maxlength="2000" name="artist_edit_news" id="artist_edit_news"><?php echo $news ?></textarea> 
           </label> 
          </section> 
    <input type="hidden" name="hiddenId" id ="hiddenId" value="<?php echo $news_id; ?>"> 

          </fieldset> 

          <footer> 
         <button type="submit" class="btn btn-success updateBtn" href="#artist_news.php" id="<?php echo $id ?>"> 
            Update 
           </button> 

          </footer> 
          </form> 
        </div> 

    <?php 
    } 
    } 

    ?>       

当用户单击foreach语句中任何单个记录上的更新时,将调用以下代码。

runAllForms(); 
$(function() {  
$("#artist_update_news").validate({ 
rules: { 
    artist_update_news_title : { 
    required : true 
    }, 
artist_edit_news : { 
required : true 
} 
}, 
     // messages for form validation 
messages : { 
artist_update_news_title : { 
required : 'Please enter your News Heading' 
}, 
artist_edit_news : { 
required : 'Please enter your News' 
} 
}, 

errorPlacement : function(error, element) { 
error.insertAfter(element.parent()); 
}, 
submitHandler: function() { 
alert('in function'); 
var element = $(this); 
var hiddenId = element.attr("hiddenId"); 
var artist_edit_news = element.attr("artist_edit_news"); 
var artist_update_news_title = element.attr("artist_update_news_title"); 
alert(hiddenId); 
alert(artist_edit_news); 
alert(artist_update_news_title); 
$('#artist_update_news').hide(0); 
$('#art_edit_news_message').hide(0); 

$.ajax({ 
url : 'artist_edit_news.php', 
type : 'POST', 
dataType : 'json', 
data: { 
artist_update_news_title : artist_update_news_title.val(), 
artist_edit_news : artist_edit_news.val(), 
hiddenId : hiddenId.val() 
}, 
success : function(data){ 
$('#art_edit_news_message').removeClass().addClass((data.error === true) ? 'error' : 'success') 
.text(data.msg).show(500); 
if (data.error === true) { 
    if (data.goto == 1) { 
    window.location = "http://dev.worldmusicstage.com/main.php#/ajax/artist_news.php"; 
    $('#artist_update_news').show(500); 
     delete json; 
    } 
    else { 

    $('#artist_update_news').show(500); 
    } 
} 
if (data.error === false) { 
    window.location = "http://dev.worldmusicstage.com/main.php#/ajax/artist_news.php"; 
    $('#artist_update_news').show(500); 
delete json; 
} 
}, 
error : function(XMLHttpRequest, textStatus, errorThrown) { 
$('#art_edit_news_message').removeClass().addClass('error') 
alert('The error was: '+errorThrown); 
alert('The error was: '+XMLHttpRequest); 
alert('The error was: '+textStatus); 
alert(errorThrown.stack) 
$('#artist_update_news').show(500); 
} 
}); 
return false; 
} 
}); 
}); 

回答

0

您需要

id ="hiddenId" 

产生输入标签的独特的动态ID,我们还需要设置变量$ ID的值

<button 
     type="submit" 
     class="btn btn-success updateBtn" 
     href="#artist_news.php" 
     id="<?php echo $id ?>" 
> 

你需要生成输入标签的唯一动态ID或使用类别选择器

$("#artist_update_news").validate({ 

<form action="" 
     method="post" 
     name="artist_update_news" 
     id="artist_update_news" 
     class="smart-form client-form"> 
+0

谢谢,我也的确让修订提交按钮。该ID是错误的,我更新到$ news_id。但是,仍然没有工作。 \t \t \t \t \t \t \t <按钮类型= “提交” 类= “BTN BTN-成功updateBtn的” href = “#artist_news.php” ID = “<?PHP的echo $ news_id?>”> – 2014-09-05 03:16:54

+0

感谢凯文。我对如何做到这一点感到困惑。如果我在表单ID中指定一个唯一的ID,那么我该如何在脚本中指定该ID。当我循环遍历foreach时,我可以将1,2,3,4等分配给唯一的ID而不是artist_update_news,但是如何在脚本中识别这个ID。或者我引用为getelementbyid。谢谢你的帮助。格兰特 – 2014-09-05 04:04:20