skip to Main Content

How to use ajax Php post method with Jquery?

//ajax starting with jquery

$(document).ready(function(){
    $("#sbBtn").click(function(){
             var userFname=("input#fname").val();
             var userLname=("input#lname").val();

                $.post("savedata_core.php",{uFname:userFname, uLname:userLname}, function(allData){

                   alert(allData);
               });
     });
});

php file : savedata_core.php

2

Answers


  1. From ajax jquery documentation. you can see how to send ajax request.

    for your question. you can do it like this:

    $.ajax({
      method: "POST",
      url: "savedata_core.php",
      data: { uFname:userFname, uLname:userLname }
    }).done(function( allData) {
        alert( allData);
      });
    
    Login or Signup to reply.
  2. Welcome to Stack Overflow. Upon review of your code, I do see some specific issues and you did not state you encountered any errors. Consider the following improvements:

    $(function() {
      $("#sbBtn").click(function(e) {
        e.preventDefault();
        $.post("savedata_core.php", {
          uFname: $("#fname").val(),
          uLname: $("#lname").val()
        }, function(data) {
          console.log(data)
        });
      });
    });
    

    When you were attempting to collect the values, you did not use the proper jQuery syntax. Your variables would not contain the proper content.

    Also check your browsers Network console to review the payload sent and received.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search