首页 > 解决方案 > PHP Ajax - 只发送数据库中的第一个输入值....为什么?

问题描述

$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();
}
?>

标签: phpajax

解决方案


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>

保存表格.php

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

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

推荐阅读