sagar sagar - 3 months ago 6
jQuery Question

jquery ajax call to execute php script

I have created an HTML form name form.php to take the firstname and lastname from the user.

<form action="form.php" method="post">
Firstname: <input type="text" name="firstname"><br>
Lastname: <input type="text" name="lastname"><br>
<input type="submit">
</form>


I wanted to use jquery ajax method to insert value in the database. how to write php-script and jquery call. Iám completely new to jquery and ajax can anyone please help me?

Answer

Try some thing like this:

$.ajax({
    url: 'process.php',
    type: 'POST',
    data: {
        var1: val1, // variables
        var2: val2
    },
    success: function(response) { // response from process
        // do your stuff here
    }
});

process.php

$var1 = $_REQUEST['var1'];
$var2 = $_REQUEST['var2'];
// do your stuff here