I currently have some JQuery code where I send the variable 'name' to a PHP file, and that PHP file calls the data in row 'name'
//JQuery Code
$('#next').click(function() {
$('#question > h1').load('/php/getQuestion.php', {
name: qty
});
});
//PHP Code Below
$value = (integer)$_POST["name"];
$sql = "SELECT `question_id`, `question` FROM Question WHERE `question_id` =
{$value}";
I want to do a similar call using JQuery's $.getJSON. Currently this is my code:
var requestAnswer = ($.getJSON('[URL HERE]'));
Is there any way I could send the value of 'name' to my PHP code and get only the JSON request from row: 'name'. Something like:
var requestAnswer = ($.getJSON('[URL HERE]'),{ name:2});
I know this example doesn't work.
You can add the query string into the url like that
$.getJSON('getQuestion.php?name=abc', function(data) {
//process data here
});
on Server side, use $_GET["name"]
to retrieve parameter
Based on jQuery.getJSON() documentation:-
You can pass your data as the second parameter:
$.getJSON( url [, data] [, success(data, textStatus, jqXHR)] )
like below:-
$.getJSON( "Url", {name:2},function( data ) {
//code based on data
});
Again the documentation says:-
"Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL."
So you can retrieve it to php end by using:-
$_GET['name'];