PHP从ajax调用中动态生成代码中读取按钮值

时间:2016-08-18 03:22:01

标签: php jquery ajax

我有一些PHP,它是从先前调用ajax函数动态生成的。

代码使用while循环生成信息列表,因此代码下面的代码段为1到无穷大,具体取决于MYSQL查询返回的内容。

always (*)
begin  
my_sig = ~my_sig;
end

单击“保存我”按钮时,我想使用php读取按钮的值,以便将其保存到数据库中。

当点击按钮时,我正在调用一些JQUERY,所以我知道可以读取该值。给我们一个唯一的ID。

echo "<div class='col-md-3'><button class='btn btn-primary-orange    mid' type='submit' id='searchbutton' value='" . $mydata["MyID"] . "' name='jid'><i class='glyphicon glyphicon-heart' aria-hidden='true'></i>&nbsp;&nbsp;Save Me</button></div>";

我以为我可以使用标准PHP

$(document).on('click', '.mid', function(){
alert($(this).attr("value"));
$.ajax({
        type: "POST",
        url: "ajaxlistings.php",
        contentType: 'application/x-www-form-urlencoded',
        data: $("#formID").serialize(),
        cache: false,
        dataType: 'html',
        success: function(data){
            console.log(data);
            $('#jdisplaylisting').html(data);
        },
        error: function (xhr, status, error) {
            alert("Sorry, there was a problem!: " + error);
            console.log(xhr.status);
            console.log(xhr.responseText);
            console.log(error);
        },
        complete: function (xhr, status) {
            // do nothing
        }
    });
    return false;
});

读取与按钮关联的值,但拒绝工作。

我猜这是与动态创建的元素有关吗?

2 个答案:

答案 0 :(得分:0)

serialize()方法不知道哪个按钮被按下,因此它不包含结果中的任何按钮。您需要在代码中自己完成。

data: this.name + '=' + encodeURIComponent(this.value) + '&' + $("#formID").serialize(),

答案 1 :(得分:0)

将值保存在表单中的隐藏字段中

<input type='hidden' value='<?= $mydata["MyID"] ?>' name='my_field'>

当您序列化并提交表单时,该值将发送给PHP

$newvalue = $_POST['my_field'];
相关问题