【问题标题】:PHP Ajax - Only send the first input value in database....Why?PHP Ajax - 只发送数据库中的第一个输入值....为什么?
【发布时间】:2019-01-25 12:03:51
【问题描述】:
$sql = mysql_query('SELECT * FROM article')or die(mysql_error());

while ($data = mysql_fetch_array($sql))
{
    echo "<td>";
    echo '<div><strong>'.$data['nom_article'].'</strong></div>';
    echo $data['prix'].' FCFA';
    echo "<input type='text' value='".$data['id']."' id='annonce_id'>";
?>
    <button type="submit" id="submit" value="<?php echo $data['id']; ?>" title="<?php echo $data['id']; ?>" style="background: #fff; border: none; cursor: pointer; color: blue;">
    <a href="">Save</a>
    </button>
<?php
    echo "</td>";
}

<script>
$(document).on('click', '#submit', function(){

var t = $('annonce_id').val();

$.ajax({
    url: "saveform.php",
    data:{
    done: 1,
    text: t
        },
    success: function(data)
    {
        alert(t);
    }
});
});
</script>

这里是 saveform.php

<?php 
if(isset($_GET['done']))
{
    $text = mysql_escape_string($_GET['text']);

    mysql_query("INSERT INTO test VALUES('', '".$text."')")or die(mysql_error());
    exit();
}
?>

【问题讨论】:

  • var t = $('annonce_id').val();错误在这里替换为 var t = $('#annonce_id').val();你忘了放#号

标签: php ajax


【解决方案1】:

多个输入不能具有相同的 id。您需要将名称属性添加到这样的所有输入中

 echo "<input type='text' value='".$data['id']."' id='annonce_id' name='annonce_id[]'>";

然后在你的 ajax 请求中

var t = $('input:text#annonce_id').serialize();

然后在php中解析为数组

【讨论】:

    【解决方案2】:
    It is OK now. Here is the solution that I concocted...Thank you @hassanrrs
    
    <button type="submit" id="submit" name="<?php echo $data['id']; ?>" style="background: #fff; border: none; cursor: pointer; color: blue;">
            <a href="">Save</a>
    </button>
    
    <script>
        $(document).on('click', '#submit', function(){
    
            var t = $(this).attr('name');
    
            $.ajax({
                url: "saveform.php",
                data:{
                    done: 1,
                    texte: t
                },
                success: function(data)
                {
                    //alert(t);
                }
            });
        });
    </script>
    

    saveform.php

    if(isset($_GET['done']))
    {
        $text = mysql_escape_string($_GET['texte']);
    
        mysql_query("INSERT INTO test VALUES('', '".$text."')")or die(mysql_error());
        exit();
    }
    

    【讨论】:

      【解决方案3】:

      试试看

      <?php
      $sql = mysql_query('SELECT * FROM article')or die(mysql_error());
      
      while ($data = mysql_fetch_array($sql)) {
          echo "<td>";
          echo '<div><strong>' . $data['nom_article'] . '</strong></div>';
          echo $data['prix'] . ' FCFA';
          echo "<input type='text' value='" . $data['id'] . "' id='annonce_id'>";
          ?>
          <button type="submit" id="submit" value="<?php echo $data['id']; ?>" title="<?php echo $data['id']; ?>" style="background: #fff; border: none; cursor: pointer; color: blue;">
              <a href="">Save</a>
          </button>
          <?php
          echo "</td>";
      }
      ?>
      <script>
          $(document).on('click', '#submit', function () {
      
              var t = $('#annonce_id').val();
      
              $.ajax({
                  url: "saveform.php",
                  data: {
                      done: 1,
                      text: t
                  },
                  success: function (data)
                  {
                      alert(t);
                  }
              });
          });
      </script>
      

      【讨论】:

      • 您的解决方案不适用于我的情况。我找到了另一种方法,我已经在上面发布了。
      • 也许你是对的,但我想说的是 var t = $('annonce_id').val();应该是 var t = $('#annonce_id').val();
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2022-07-29
      • 1970-01-01
      相关资源
      最近更新 更多