Jimmy
Jimmy

Reputation: 105

Passing a value in javascript

I'm trying the tutorial from infotuts here: http://www.infotuts.com/ajax-table-add-edit-delete-rows-dynamically-jquery-php/

And there's a javascript like this:

 $(function(){
 $.ajax({
         url:"DbManipulate.php",
                  type:"POST",
                  data:"actionfunction=showData",
        cache: false,
        success: function(response){

          $('#demoajax').html(response);
          createInput();

        }

       });

Now I want to add a parameter so that the line: url:"DbManipulate.php" becomes url:"DbManipulate.php?q=[some value]

I tried to alter the script like this:

 var cat=2;

 $(function(){
 $.ajax({
         url:"DbManipulate.php?q="+cat.val(),
                  type:"POST",
                  data:"actionfunction=showData",
        cache: false,
        success: function(response){

          $('#demoajax').html(response);
          createInput();

        }

       });

But it doesn't work. The variable cat never gets into the function. How to pass the variable "cat" so that the DbManipulate.php file receives the $q variable and I can use it using $_GET?

Thank you

Upvotes: 3

Views: 71

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Try simply this way to sent your data variable(cat) using GET Method

var cat=2;
$(function(){
 $.ajax({
         url:"DbManipulate.php",
         type:"GET",
         data:{actionfunction:showData,cat:cat},
         cache: false,
         success: function(response){
          console.log(response);
          $('#demoajax').html(response);
          createInput();
        }
       });

 // in DbManipulate.php, try to catch cat using $_GET like this
  $cat=$_GET['cat'];
 //do further processing

EDIT

cat=2;
url="DbManipulate.php";
function yourFunc(cat,url){
$.ajax({
    type: "GET",
    url: url+'?q='+cat,
    dataType: "json",
    cache: false,
    success: function (response) {
        $('#demoajax').html(response);
              createInput();
    }
});
}
//in DbManipulate.php 
 $cat=$_GET['q'];

More Info:http://api.jquery.com/jquery.ajax/

Upvotes: 1

Related Questions