Vyshnavi Samudrala Vyshnavi Samudrala - 3 months ago 12
Ajax Question

Can't get jquery variables in php via POST

I have this jquery in another processing.js file and is included in the html file having the form

$("#1st_submit").on("click", function(event) {
var ServiceType = $("#ServiceType").val();
var locality = $("#locality").val();
$.ajax({
type: "POST",
url: 'http://localhost/revamp/booking.php',
data: {
ServiceType: ServiceType,
locality: locality
},
error: function(req, err){ console.log('my message ' + err); },
success: function (data) {
$("#1st_booking_form").hide();
$("#2nd_booking_form").show();
console.log(data);
alert("success");
}
});//ajax
})


In the PHP code, I am accessing the POST variables as

$ServiceType = $_POST['ServiceType'];
$locality = $_POST['locality'];


But I am not getting the POST values in the PHP, It's null

HTML Code with the form is this way

<form role="form" method="post" id="1st_book" action="">
<div class="row" id="1st_booking_form">

<div class="col-md-12 col-sm-12">
<input class="inputMaterial" id="ServiceType" type="text" value="<?php echo $service_name_s; ?>" name="ServiceType" required>
</div>
<div class="col-md-12 col-sm-12">
<input class="inputMaterial" id="locality" type="text" value="<?php echo $locality_s; ?>" name="locality" required>

</div>


<br>
<center><button type="button" class="btn_full" id="1st_submit"> Book Now </button></center>
</div>
</form>


The whole code is working with
type:"GET"
.

Answer

Please mentione #1st_submit is link or simple button?, you have not prevented further event action after this event thats why may be create issue.

$(document).on("click","#1st_submit", function(e) {
    $.ajax({
        type: "POST",
        url: 'http://localhost/revamp/booking.php', // try url with relative path
        data: {
            ServiceType: $("#ServiceType").val(),
            locality: $("#locality").val()
        },
        error: function(req, err){ console.log('my message ' + err); },
        success: function (data) {
            $("#1st_booking_form").hide();
            $("#2nd_booking_form").show();
            console.log(data);
            alert("success");   
        }
    });//ajax
    return false;
});

If Still not working then

Check if you have added any redirection rules in .htaccess file , or using header('location: .....') it may be cause same issue....

Comments