I want to get 2 var from a form with live result with $.post() function in javascript, but when i add second var in js code my php page doesn't load anymore.
this is my script:
function getStates(value) {
$.post("live.php", { name:value, price_var:price_value }, function(data) {
$("#total").show();
$("#total").html(data);
});
}
this is my input:
<input name="username" onkeyup="getStates(this.value)"/>
<input name="username" onkeyup="getStates(this.price_value)"/>
and this is my php page ( so simple ) :
<?php echo $_POST["name"]. ' ' .$_POST["price_var"]; ?>
this works correctly, but i want get another var
You never get the two variables into your js script.
Add id to your inputs
<input id='bar' name="username" onkeyup="getStates()"/>
<input id='foo' name="price_value" onkeyup="getStates()"/>
Change your js code to get the values
function getStates() {
// Get value here
var name = $('#bar').val();
var price_value = $('#foo').val();
// end get value
$.post("live.php", { name:name, price_var:price_value }, function(data) {
$("#total").show();
$("#total").html(data);
});
}
Your php code looks ok
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With