As the topic name says I'm trying to pass variables from a HTML form to a PHP script with jQuery and AJAX. I've done this before, but today I don't have access to the files. PHP won't print the first name and last name. I found some examples, but whatever I do it's the same... What I'm doing wrong in the last few days?
This is the HTML code:
First Name: <br />
<input type="text" name="fname" /><br />
Last name: <br />
<input type="text" name="lname" /><br /><br />
<input type="submit" name="submit" value="Send" />
Javascript (jQuery):
$(document).ready(function(){
$('input[name="submit"]').click(function(){
var fname = $('input[name="fname"]').val();
var lname = $('input[name="lname"]').val();
$.ajax({
type: 'POST',
url: 'index.php',
data: {fname:fname, lname:lname},
dataType:"json"
});
});
});
PHP:
if(isset($_POST['fname']) && isset($_POST['lname'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
echo $fname;
echo "<br />";
echo $lname;
}