Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  162] [ 7]  / answers: 1 / hits: 57882  / 13 Years ago, fri, november 25, 2011, 12:00:00

I'm having trouble bringing back data from my PHP file. I guess I don't really understand the data parameter of this jquery function so I just went off some tutorials.



Jquery



$.ajax(
{
url: 'test.php',
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})


Now from my understanding the test: declares the variable used in php and 1 is the value in that variable. But I'm not entirely sure...



Here's my PHP



$item1 = $_POST['test'];

echo $item1;


Now it's just supposed to alert that value so I know it is at least returning something but in the alert it's just blank so I'm losing the value somewhere, but where?


More From » php

 Answers
31

use $_REQUEST it will handle both the GET and POST



$item1 = $_REQUEST['test'];


by default ajax request is GET type, either specify expilicitly the type like



$.ajax(
{
url: 'test.php',
type:'POST'
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})


or use $_GET like



item1 = $_GET['test'];

echo $item1;

[#88913] Wednesday, November 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleefridad

Total Points: 399
Total Questions: 97
Total Answers: 114

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;