简体   繁体   English

PHP Ajax-仅发送数据库中的第一个输入值...为什么?

[英]PHP Ajax - Only send the first input value in database…Why?

$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>

Here is the saveform.php 这是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();
}
?>

Multiple inputs can't have same id. 多个输入不能具有相同的ID。 you need to add the name attribute to all the inputs like this 您需要将name属性添加到所有这样的输入中

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

and then in your ajax request 然后在你的ajax请求中

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

and then parse it as an array in php 然后将其解析为php中的数组

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 saveform.php

if(isset($_GET['done']))
{
    $text = mysql_escape_string($_GET['texte']);

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

Try it 试试吧

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM