Fergal O'Hanlon Fergal O'Hanlon - 1 year ago 61
Ajax Question

PHP to read button value from dynamically generated code from an ajax call

I have some PHP that is generated dynamically from a previous call to an ajax function.

The code generates a list of information using a while loop so the snippet below code be 1 to infinity depending on what the MYSQL query returns.

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>";

When the Save Me button is clicked I want to read the value of the button using php so I can save it to a database.

When the button is clicked I am calling some JQUERY so I know the value can be read. Value us a unique ID.

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

I thought I could use the standard PHP

$newvalue = $_POST['jid']

to read the value associated with the button but it refuses to work.

I am guessing it is to do with dynamically created element?

Answer Source

The serialize() method doesn't know which button was pushed, so it doesn't include any buttons in the result. You'll need to do it yourself in the code.

data: this.name + '=' + encodeURIComponent(this.value) + '&' + $("#formID").serialize(),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download