I want to send a variable from a html element to php with ajax. It's a button, so the value property cannot be used to assign a variable. Is it possible to send it's id property to a php file with jquery or would there be a better way?
You can pass the ID, or use a data-
attribute on the button to store what you want to pass.
If you just want to send the ID field of an element to a server with jQuery, you can do the following:
myButton = $(".some.selector.that.finds.the .button")
$.post("http://myserver.com/myfile.php", {theID: myButton.attr('id')});
If you want to store data along with that element, use the data-
property like so:
HTML:
<button name="mybutton" data-extra="blahblahblah...">
Javascript:
myButton = $(".some.selector.that.finds.the .button")
$.post("http://myserver.com/myfile.php", {myData: myButton.data('extra')});
use hidden input element. set them and then read them in php
Very easy to do with ajax. Used $.post for example, can change to $.get also
http://api.jquery.com/jQuery.post/
$( selector ).click( function(){
$.post( url, { id: this.id} , function( data){
/* do something here on ajax success*/
})
})