Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to send a Javascript array to PHP using AJAX. This seems very simple code, but I am getting the "No isset" message.

<?php 

if (isset($_REQUEST['activitiesArray'])) {
echo 'YES isset';   
}else{
echo 'No isset';
}
?>

<!DOCTYPE html>
<html>
<head>

<script  src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">


var activities = ["thing1", "thing2", "thing3"];

$.ajax({        
   type: "POST",
   url: "JavatoDBase.php",
   data: { activitiesArray : activities },
     success: function() {
     $("#lengthQuestion").fadeOut('slow');        
     }
 });

Safari displays the following, which suggests the POST is operating

enter image description here

share|improve this question
1  
what happens when you dump the whole request back to client? –  charlietfl Jun 19 '12 at 1:21
 
possible duplicate of How to debug AJAX requests? How to know which PHP function is invoked? –  hakre Jun 19 '12 at 1:26
 
Look into the request which data is actually submitted and how. –  hakre Jun 19 '12 at 1:26
 
Did you try posting a json instead of an array? –  Eswar Rajesh Pinapala Jun 19 '12 at 1:30
 
Thanks guys. I did try sending a small text string, but same error message. Also. The PHP is in the same file as the javascript i.e. "JavatoDBase.php" so I don't think I'm calling another PHP function –  Jeremy Jun 19 '12 at 1:33
show 5 more comments

2 Answers

up vote 1 down vote accepted

If you make an ajax request, you should return something that you can use in the js to determine how it went. You also only want to load the response, not the entire page html and all.

Try this:

<?php 

if (isset($_REQUEST['activitiesArray'])) {
  echo json_encode(
    array('status' => true)
  );
  exit(0);
}else{
echo 'No isset';
}
?>

<!DOCTYPE html>
<html>
<head>

<script  src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
  var activities = ["thing1", "thing2", "thing3"];

  $.ajax({        
    type: "POST",
    url: "test.php",
    data: { activitiesArray : activities },
    dataType: 'json',
    success: function(data) {
      if(data.status === true) {
        alert('rad');
      }
      else {
        alert('bad');
      }
    }
  });
</script>
share|improve this answer
add comment

I found the answer. All the above code was help is the PHP file (I started with PHP and HTML followed). I then split the PHP into its own (PHP suffix) file and the HTML into its own (HTML suffix) file. It worked like a charm. Thanks for your help.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.