3

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" );
    });
3
  • And what would a .html file do with the variable ? Commented Jan 9, 2015 at 20:25
  • url: "test.html?var1=" + val1 + "&var2=" + val2 Commented Jan 9, 2015 at 20:25
  • i want to pass only one variable Commented Jan 9, 2015 at 20:26

4 Answers 4

11

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

Sign up to request clarification or add additional context in comments.

Comments

2
     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.**

1 Comment

How does that answer the question?
0

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" );
});

Comments

0

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.