Reputation: 373
Hi i'm new in jquery i have wriiten code and i want to pass variable to test.html page how can i do this can any one help
my code
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(this).myClass( "done" );
});
Upvotes: 3
Views: 51889
Reputation: 523
var quantity = $(this).data("quantity");
// you can get data use of j query
$.ajax({
url: "xyx.php?action=add&",
type: "POST",
data:{"product_id":product_id,"qty":quantity}
});
** data:{"product_id":product_id,"qty":quantity} here list of argument accept php code depends on your backed logics so.**
Upvotes: 2
Reputation: 3712
so you scope a variable outside your ajax request and refer to it when making that request. You then can concatenate the string inside the ajax request like i think you are trying too.
var endingUrl = "/help.html";
$.ajax({
url: "test"+endingUrl,
context: document.body,
data: options
}).done(function() {
console.log(endingUrl);
});
Upvotes: 0
Reputation: 97
Create a variable with key:value pairs in JSON format and pass it to the "data" parameter of your ajax call. It will be passed in the post variables, for a POST, and will be in the request line, for a GET.
var options = { "name1" : "value1", "name2" : "value2"};
$.ajax({
url: "test.html",
context: document.body,
data: options
}).done(function() {
$(this).myClass( "done" );
});
Upvotes: 0
Reputation: 2569
AJAX (Asynchronous JavaScript and Xml) is for communicating with a server. The following is an AJAX POST request that is being sent to test.php. PHP runs on servers and can receive, process, and respond to HTTP requests. You may want to look into PHP and server side web communications.
var myVar = "test";
$.ajax({
url: "test.php",
type: "POST",
data:{"myData":myVar}
}).done(function(data) {
console.log(data);
});
The accompanying PHP file may look something like:
<?php
$data = isset($_REQUEST['myData'])?$_REQUEST['myData']:"";
echo $data;
?>
These are very basic examples but can be very useful to learn.
AJAX tutorial: http://www.w3schools.com/ajax/ PHP tutorial: http://www.codecademy.com/en/tracks/php
Upvotes: 11