1
<script type="text/javascript">
$(function() {

$("#epdate").bind("change", function() {
 $.ajax({
     type: "GET", 
     url: "change6-emp.php",
     data: "epdate="+$("#epdate").val(),
     success: function(html) {
         $("#output").html(html);
     }
 });
});


});
</script>

i have this code and i want to add another variable inside ajax script adding another

data: "empname="+$("#empname").val(),

dopesnt work i hope someone would help me. thank you and how can i call a postname or make a postname into session an call it into another php page?

1 Answers1

1

Actually, there are multiple ways, either separate them using a & character.

<script type="text/javascript">
$(function() {

$("#epdate").bind("change", function() {
 $.ajax({
     type: "GET", 
     url: "change6-emp.php",
     data: "epdate=" + $("#epdate").val() + "&empname="+$("#empname").val(),
     success: function(html) {
         $("#output").html(html);
     }
 });
});


});
</script>

Or alternatively, you can use an object which holds the name-value pair.

<script type="text/javascript">
$(function() {

$("#epdate").bind("change", function() {
 $.ajax({
     type: "GET", 
     url: "change6-emp.php",
     data: { epdate : $("#epdate").val(), empname : $("#empname").val() },
     success: function(html) {
         $("#output").html(html);
     }
 });
});


});
</script>

UPDATE 1: You can also pass it as an array in the following format,

data : [{
    name : 'epdate',
    value : $("#epdate").val()
  }, {
    name : 'empname',
    value : $("#empname").val()
  }],

UPDATE 2: There is build in functions in jQuery to do the same, use [serialize()][] or serializeArray() method for that. You can apply it on a form element or input elements and which generates based on the input elements name attribute.

data : $('#epdate,#empname').serialize(),
// or
data : $('#epdate,#empname').serializeArray()

,

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188