0

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".

17
  • Does the console show any errors? Please validate the data on the PHP side. Commented Aug 18, 2016 at 11:52
  • 1
    can you add the html for this? are you checking for PHP errors and looking at your console? Commented Aug 18, 2016 at 11:52
  • 1
    After this line: var ServiceType = $("#ServiceType").val(); put an alert(ServiceType) and check its value Commented Aug 18, 2016 at 11:54
  • 1
    Check your network tab in the console and look for your booking.php page at the end after you send a request. Commented Aug 18, 2016 at 11:55
  • 2
    console.log(ServiceType); console.log(locality); before ajax request, print_r($_POST) in php file. Commented Aug 18, 2016 at 11:56

2 Answers 2

1

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....

Sign up to request clarification or add additional context in comments.

16 Comments

also on server side print_r($_POST); die(); and check in console what is returning from server.
above code is working fine with testing input, so something wrong with input fields.
event.preventDefault(); I've added this too as it is a normal button with type="button"...
still try with this code and try with relative url path instead of full url.
It is executing that php file, i can get other results from php file through echo, but not post variables @HareshVidja
|
0

That was a problem with redirections in my .htaccess file... an exception rule has been added and it is working properly now!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.