0

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

3
  • 1
    Welcome to Stack Overflow. You appear to have syntax errors in your code . Please describe what errors you get in console and if you see any data being sent/received in Network tab. Commented Dec 30, 2019 at 16:24
  • Thanks for great attitude. I am trying to create a login form with Ajax, Jquery and Php.I am following a video tutorial of youtube. I have write all code similar of this video. In the video this code is working but not working to me. I want a solution for that Commented Dec 31, 2019 at 9:01
  • I would encourage you to review the video again or update your post and include a Minimal, Reproducible Example: stackoverflow.com/help/minimal-reproducible-example Commented Dec 31, 2019 at 15:58

2 Answers 2

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);
  });
Sign up to request clarification or add additional context in comments.

1 Comment

FWIW, the op mentions $.post(), $.ajax() would also work but the method signature is pretty different
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.