Chrollo Lucilfer
Chrollo Lucilfer

Reputation: 53

PHP form action and method into Jquery

It is possible to transfer my php form action and method process into jquery process?

I tried to insert input data into database using php form action and method and it is working but in jquery process nothing happen.

<form action="" method="">
    <input type="submit" id="signUpButton" value="Sign Up">
</form>

$(document).ready(function(){
    $("#signUpButton").click(function(){
        $.post("signUpAction.php", function(data, status){
           alert("data: " + data + "\nStatus: " + status);
        });
    });
});

Upvotes: 0

Views: 800

Answers (2)

Rushi Vasani
Rushi Vasani

Reputation: 71

You need to send the form data as well. It can be done by jQuery serialize method.

$(document).ready(function(){
    $("#signUpButton").click(function(){
        $.post("signUpAction.php", $("form").serialize(), function(data, status){ 
            alert("data: " + data + "\nStatus: " + status);
        });
    });
});

Upvotes: 1

Nasser Ali Karimi
Nasser Ali Karimi

Reputation: 4663

You should use ajax to send data to PHP file and using serializeArray() to get your form data and store it in a variable that will send to PHP file.

Try like this

Your HTML

<form>
  <input type="text" name="name" value=''/>
  <input type="email" name="email" value=''/>
</form>

Your js should be like this:

 $('#add_new_stu').click(function(event) {
    var formdata= $("form").serializeArray();
    $.ajax({  
        type: "POST",  
        url: "file.php",  
        data: formdata,  
        success: function(response){
                console.log(response);  
            }
    });
});

With PHP you should loop your result array.

$name= $_POST['name'];
$email= $_POST['email'];
//do some thing here

Upvotes: 8

Related Questions