【问题标题】:Passing Foreach data from PHP to Ajax将 Foreach 数据从 PHP 传递到 Ajax
【发布时间】:2014-09-05 01:53:33
【问题描述】:

希望有人可以帮助解决此代码。我有一个带有 foreach 循环的 php 文件,用于从 mysql 数据库中获取数据。当用户点击 php 文件中的 Update 时,会调用这个 JS 脚本。我正在使用“this”来获取一组单独的值。我遇到的问题是没有任何东西传递给 JS 脚本。

这是 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;
}
});
});

【问题讨论】:

    标签: javascript php jquery mysql ajax


    【解决方案1】:

    你需要为输入标签生成一个唯一的动态id

    id ="hiddenId"
    

    我们还需要在

    中设置变量$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">
    

    【讨论】:

    • 谢谢,我确实对提交按钮进行了修改。 ID 错误,我将其更新为 $news_id。但是,仍然无法正常工作。
    • 谢谢凯文。我对如何做到这一点有点困惑。如果我在表单 ID 中指定一个唯一 ID,那么我如何在脚本中指定该 ID。当我遍历 foreach 时,我可以为唯一 ID 分配 1、2、3、4 等,而不是 Artist_update_news,但是我如何在脚本中识别它。或者我是否引用为 getelementbyid。谢谢你的帮助。格兰特
    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 2023-03-13
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    相关资源
    最近更新 更多