0

I am new to PHP and Ajax so please bear with me. I've searched around and found some answers but still am having trouble. I have an array of check box input values. If a user checks an item it is added to my array list. An example would be:

listOfPrograms = [chrome, firefox, sqlworkbench]

I want to send this array list to a PHP script on my server. My current Ajax script is as follows:

function ajaxPostToPhp(listOfPorgrams)
{
  $.ajax
  ({
      url: 'script.php',
      type: 'post',
      data: ("listOfPrograms" : listOfPrograms), // I believe this is where my issues lies as I do not know exactly that this is doing. I have read the PHP documentation. I tried converting to JSON and kept getting a 500 error.
      success: function(data)
      {
         console.log(data);
      }
   });
}

My PHP script is as folllows:

$myArray = $_Request['listOfPrograms'];
echo $myArray;

This returns only 1 item from the array. I tried setting myArray = [] but I get an undefined index.

Thanks for your help! Sorry for such a noob question.

3 Answers 3

1

You need to fix a few things:

1- Javascript array:

var listOfPrograms = ['chrome', 'firefox', 'sqlworkbench'];

2- Ajax Data:

function ajaxPostToPhp(listOfPrograms)
{
  myListData = {};
  myListData['Programs'] = listOfPrograms; 
  $.ajax({
     url: 'script.php',
     type: 'post',
     data: myListData, 
  success: function(data)
  {
     console.log(data);
  }
});
}

3- Php Code:

$myArray = $_POST['Programs'];
var_dump($myArray);
Sign up to request clarification or add additional context in comments.

1 Comment

@dan08 Just for testing purposes, i assume the OP is familiar with the php language....
0

You are passing an array as post parameter but they can only be strings. You should convert the array to a JSON string first. An easy function for that purpose is JSON.stringify()

var listOfPrograms = ["chrome", "firefox", "sqlworkbench"] 
// I guess you need strings here

function ajaxPostToPhp(listOfPorgrams) {
  $.ajax ({
      url: 'script.php',
      type: 'post',
      // Convert listOfPrograms to a string first
      data: ("listOfPrograms" : JSON.stringify(listOfPrograms)),
      success: function(data) {
         console.log(data);
      }
   });
}

5 Comments

Also, in your php file, you should use echo json_encode($myArray);
Great! I tried this and it works. Two questions: (1) what is "listOfPrograms" : JSON.stringify(listOfPrograms) actually doing? It is converting the array to JSON format... then what? (2) Why does listOfPrograms need to be in quotes? Also, it returns "[\"chrome\",\"firefox\"]" not as individual items but as characters. For example myArray[2] = "c". I would like to be able to pull each item "chrome" and "firefox" to do some manipulation.
(1) What do you mean? It just converts the array to a JSON string. (2) are chrome, firefox variables or do you want to transfer the text? If you want to transfer text, you need the " quotes. (3) I would like to be able to pull each item - In php?
Sorry if I was unclear. The program names are just text inside of an array. In PHP I would like to be able to pull each name(text) out using a for loop. How I would do it in java is: for int i = 0; i < myArray.size(); i++ then myArray.get(i). Which would return "chrome", "firefox", and "sqlworkbench". I would like to do something similar in PHP. However, if I use the index I am only getting a single character when using myArray[3].
Then you definitely need quotes. Additionally, you need to translate the JSON string back to an array in php. $myArray = json_decode($_Request['listOfPrograms']);
0

jquery will kindly turn array values in ajax post data to an array for you. the issue is that in php you can't just echo an array. as a commenter stated, your php file needs to look like

$myArray = $_Request['listOfPrograms']; 
echo json_encode($myArray);

also you should consider using $_POST over $_REQUEST

1 Comment

I tried this and it only returned one item of the array. Thank you for the suggestions though.

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.