3

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

3
  • 1
    you are using POST, when you tack a variable to the end of the url that is a GET function. In my experience that doesnt work. You should add variable cat to your data that you send (POST) Commented Feb 1, 2015 at 4:21
  • 1
    there could be more errors, but try changing your data: to data:"actionfunction=showData&q="+cat.val()", Commented Feb 1, 2015 at 4:23
  • 3
    In your altered script, var cat=2; is an integer, so you don't need to use .val() to get the value. It should be just url:"DbManipulate.php?q="+cat,. But that will only work if you send it with type GET as mentioned above. Commented Feb 1, 2015 at 4:27

1 Answer 1

1

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/

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

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.