Reputation: 1817
Recently I've been struggling with Jquery and Ajax while trying to submit forms with them. I have a very simple form with a username field and password field as well as a submit button. What the form is supposed to do is that once the form is submitted the info would be sent by Ajax to a php file which then adds the said form values to a database. What I am struggling with is how to get the values from Ajax to php. Here is my code:
$('#form').submit(function(){
var username = $('#username').val();
var password = $('#password').val();
var dataString = 'uname=' + username + '&passw=' + password;
$.ajax({
type: "POST",
url:'check.php',
data: dataString,
success: function(data){
alert(data);//only for testing purposes
}
});
What eludes me is how can I get the dataString from this with php?
Upvotes: 3
Views: 6630
Reputation: 8578
PHP file:
<?php
print_r($_POST);
?>
jQuery part:
var dataString = 'uname=555';
$.ajax({
type: "POST",
url:'check.php',
data: dataString,
success: function(data){
alert(data);//only for testing purposes
}
});
brings me:
So my only guess would be that you are failing to fetch your data in javascript.
One more idea. Replace the type with "GET"
. Then in php file write a line:
echo $_SERVER["REQUEST_URI"];
What does it give you in the alert box? :)
Upvotes: 1
Reputation: 2950
What you send through POST (Either Ajax or non-ajax) It will be available in a PHP global array called $_POST
.
To test, place the following in check.php:
<?php
print_r($_POST);
?>
This should reflect the Ajax variables in your alert()
.
Upvotes: 0