Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get 2 variable from $.post() function

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

like image 442
Mehran Pourjenabi Avatar asked May 17 '26 23:05

Mehran Pourjenabi


1 Answers

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

like image 66
R3tep Avatar answered May 20 '26 11:05

R3tep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!