user3488008
user3488008

Reputation: 55

ajax call not getting executed in php

I m sending some checkbox which are selected,their value to next php page with ajax call.but in the above code i m not able to send it to ajax call

code is as below

<html>
<head>
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    function get_check_value() {
        var c_value = [];
        $('input[name="services"]:checked').each(function () {
            c_value.push(this.value);
        });
        return c_value.join(',');
    }
    $(document).ready(function(){
    $('#btnSubmit').on('click', function () {
    alert("hi");
        //var os = $('#originState').val();
      \  //var c = $('#commodity').val();
        //var ds = $('#destState').val();
        var ser = get_check_value();
        //var queryString = "os=" + os;
        var queryString = "&ser=" + ser;
        alert("hi");
       $.ajax({
       //alert("ajax");
        type: "POST",
        url: "sortingajax.php",
        data: "query=" + queryString,
        success: function (b) {
           // alert(a+' ok. '+b)
            $('#results').html(b);
            console.log(b);
        }
    });

  });
 });

</script>
<form name="searchForm">
    <input type="checkbox" name="services" value="twic" />TWIC
    <br/>
    <input type="checkbox" name="services" value="enclosedTrucking" />Enclosed Trucking
    <br/>
    <input type="checkbox" name="services" value="flatBedTrucking" />Flat Bed Trucking
    <br/>
    <input type="submit" id="btnSubmit" value="Submit" />
</form>
<div id="results">
</div>
</body>
</html>

In above code.When i select checkboxes from page,and on click of submit.i want to pass them to next php page with ajax call.but its not going to next page and no response is coming back....

Please help guysss

ajaxphp page

<?php

include('connection.php');
$query=$_GET['query'];
echo $query;
$countsql='SELECT * FROM XML';
    $countsql1=mysql_query($countsql);
$numrows = mysql_num_rows($countsql1);  
$countArray2=array();
print($countsql);
while($row = mysql_fetch_array($countsql1)) {
    // Append to the array


    $countArray2[] = $row;



    //echo $row['PID']."<BR />";
}

?>
<?php
foreach($countArray2 as $array)
{



?>
<div class="search">
hi
</div>

<?php $i++; } ?>

Upvotes: -1

Views: 235

Answers (3)

Maybe you're forgetting to prevent the Submit button default behavior:

<script type="text/javascript">
function get_check_value() {
    var c_value = [];
    $('input[name="services"]:checked').each(function () {
        c_value.push(this.value);
    });
    return c_value.join(',');
}
$(document).ready(function(){
$('#btnSubmit').on('click', function (evt) {

evt.preventDefault();

alert("hi");
    //var os = $('#originState').val();
  \  //var c = $('#commodity').val();
    //var ds = $('#destState').val();
    var ser = get_check_value();
    //var queryString = "os=" + os;
    var queryString = "&ser=" + ser;
    alert("hi");
   $.ajax({
   //alert("ajax");
    type: "POST",
    url: "sortingajax.php",
    data: "query=" + queryString,
    success: function (b) {
       // alert(a+' ok. '+b)
        $('#results').html(b);
        console.log(b);
    }
});

  });
 });

</script>

Upvotes: 0

developerwjk
developerwjk

Reputation: 8659

data: "query=" + queryString, is wrong because the "query=" + is a syntax error. It should be:

 var queryString = "os="+os+"&ser="+ser;

and then

 data : queryString,

Or you can format it like:

  data : {
      'ser' : ser,
      'os'  : os
 }

Then there is the fact that your Ajax is using POST but you're trying to read the request with $_GET rather than $_POST or $_REQUEST in your PHP.

Upvotes: 1

Jai
Jai

Reputation: 74738

You can try with these two amends:

var data : "?ser=" + ser; // put '?' instead of '&'

Or more jQuery way:

 data : { ser : ser },

And you are missing the dataType in your ajax:

dataType :  'html',

On the php side, as this is a post request, so you have to put this:

$query=$_POST['ser'];

Upvotes: 0

Related Questions