0

This is the code that I'm using to send a variable (via GET) to another php file: (basically, I click on a button, and then js gets the id and sends the id via ajax to the php file.

$(document).ready(function() {
   $(".doClick").click(function() {
     var category=$(this).attr('id');
     $.ajax({
       url:'aFile.php',
       type:'GET',
       data: $category,
       success: function(data){
           alert("It worked?"); // this is the response
    }
 });
alert($(this).attr("id"));
  });
 });

This is the code in my aFile.php: The php file gets the info via $_GET[] and then assigns it to a variable and uses that variable in a function call.

<head>
<script type="text/javascript">  
$(document).ready(function() {
   function JS() {
 //code
});
</script>
</head>
<body onload="JS()">
<?php
$category = $_GET['category'];

if (function_exists('inventory_insert')) {
echo inventory_insert('{category_name = '.$category.'}');

} else echo('warning');
?>

It's supposed to give me a response back on my main page, but nothing seems to be happening. I don't even get the alert I posted after the ajax script.

3 Answers 3

3

your variable is category but you're sending data: $category

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

2 Comments

Heh, i make that mistake all the time, get used to doing variables in php and sometimes place a $ in front of the vars in my js. Simple little mistake :)
Haha can't believe I missed that. One bug out.
1

You must send key/value pair to server

to receive $_GET['category'] your data sent in ajax needs to be either:

  data: 'category='+category

Or

  data: {category: category}

2 Comments

Thanks! So I fixed that and put down the correct variable, but it's still not sending back a response :/
Use a browser console to inspect the AJAX request. Will see status, response etc
0

You have assigned id into category in jquery. so correct in data params.

 data: {category : category},

Send it in this way to server or php file.

$(document).ready(function() {
 $(".doClick").click(function() {
 var category=$(this).attr('id');
 $.ajax({
   url:'aFile.php',
   type:'GET',
   data: {category : category},
   success: function(data){
       alert("It worked?"); // this is the response
}
});
    alert($(this).attr("id"));
});
});

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.