0

I am trying to send values to other page Using Ajax

But i am unable to receive those values , i don't know where i am wrong

here is my code

<script type="text/javascript">
function get_more_info() { // Call to ajax function
var fval = document.getElementById('get_usecompny').value;
var dataString1 = "fval="+fval;
alert(fval);
var sval = document.getElementById('country').value;
var dataString2 = "sval="+sval;
alert(sval);

$.ajax({
    type: "POST",
    url: "getmoreinfo.php", // Name of the php files
    data: "{'data1':'" + dataString1+ "', 'data2':'" + dataString2+ "'}",
    success: function(html)
    {
        $("#get_more_info_dt").html(html);
    }
  });
 }
</script>

in alert i am getting those value but in page 'getmoreinfo.php' i am not receiving any values

here is my 'getmoreinfo.php' page code

    if ($_POST) {
      $country = $_POST['fval'];
      $country1 = $_POST['sval'];

      echo  $country1;
      echo "<br>";
      echo  $country;   
      }

Please let me know where i am wrong .! sorry for bad English

manikanta gowda
  • 103
  • 2
  • 11

3 Answers3

3

You are passing the parameters with different names than you are attempting to read them with.

Your data: parameter could be done much more simply as below

<script type="text/javascript">
function get_more_info() { // Call to ajax function
    var fval = document.getElementById('get_usecompny').value;
    var sval = document.getElementById('country').value;

    $.ajax({
        type: "POST",
        url: "getmoreinfo.php", // Name of the php files
        data: {fval: fval, sval: sval},
        success: function(html)
        {
            $("#get_more_info_dt").html(html);
        }
      });
}
</script>

Or cut out the intermediary variables as well and use the jquery method of getting data from an element with an id like this.

<script type="text/javascript">
function get_more_info() { // Call to ajax function


    $.ajax({
        type: "POST",
        url: "getmoreinfo.php", // Name of the php files
        data: { fval: $("#get_usecompny").val(), 
                sval: $("#country").val()
              },
        success: function(html)
        {
            $("#get_more_info_dt").html(html);
        }
      });
}
</script>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

No need to create 'dataString' variables. You can present data as an object:

$.ajax({
    ...
    data: {
        'fval': fval,
        'sval': sval
    },
    ...
});

In your PHP, you can then access the data like this:

$country = $_POST['fval'];
$country1 = $_POST['sval'];
Peter
  • 153
  • 1
  • 7
0

The property "data" from JQuery ajax object need to be a simple object data. JQuery will automatically parse object as parameters on request:

$.ajax({
    type: "POST",
    url: "getmoreinfo.php",
    data: {
        fval: document.getElementById('get_usecompny').value,
        sval: document.getElementById('country').value
    },
    success: function(html) {
        $("#get_more_info_dt").html(html);
    }
});