0

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

10
  • 1
    what happens when you dump the whole request back to client? Commented Jun 19, 2012 at 1:21
  • possible duplicate of How to debug AJAX requests? How to know which PHP function is invoked? Commented Jun 19, 2012 at 1:26
  • Look into the request which data is actually submitted and how. Commented Jun 19, 2012 at 1:26
  • Did you try posting a json instead of an array? Commented Jun 19, 2012 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 Commented Jun 19, 2012 at 1:33

2 Answers 2

1

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>
0

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.

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.