Send all jQuery array values to php and insert into the database

I have an array called selectedDays, this array holds several dates (“2015-10-08”, “2015-10-09”, “2015-10-10”…)

I also have <?php include_once('classes/post.class.php'); ?>, from this file I retrieve data from my database and also insert data.

I’m trying to pass all the values held in selectedDays array to the post.class.php file and insert that into my database. I’m using this code to pass the values to the file:

 $("input[type='submit']").click(function(e) {

    for(var i=0; i < selectedDays.length; i++ ) {
     $.ajax({
       url: 'classes/post.class.php',
       data: 'selectedDays='+ selectedDays[i],
       type: "POST",
       success: function(json) {
      }
     });

    }
 
    });

Since the php file is included I’m unsure what to put in the url part, I’ve tried the current page name but still does not work.

To insert the array values into the database I’m using the following on the PHP file:

$selectedDays = $_POST['selectedDays'];

foreach ($_POST['selectedDays'] AS $i => $theDate)    { 
$sql = "INSERT INTO...
}

I keep getting this PHP error : “PHP Notice: Undefined index: selectedDays”. I’m presuming the problem has something to do with the PHP file being included, because when I create a new php file that is not included onto the page it works fine.

Can anyone help please?

You’re executing $.ajax inside loop, that means you’re sending each item of array in separate request.

Better way to send array from JS to PHP is to send it as JSON:

$.post('classes/post.class.php', {selectedDays: JSON.stringify(selectedDays)});

in PHP you can convert that JSON back to array:

$selectedDaysJSON = $_POST['selectedDays'];
$selectedDays = json_decode($selectedDaysJSON, true);

foreach ($selectedDays as $i => $theDate) {  ...  }
1 Like

I’ve changed the code and I’m still getting the same error messages:

PHP Notice: Undefined index: selectedDays in /xxxxx/classes/post.class.php on line 118

PHP Warning: Invalid argument supplied for foreach() in /xxxxx/classes/post.class.php on line 122

selectedDays is defined because when I use alert(selectedDays); I can see the values.

That’s a JavaScript command not a PHP one. Just because a variable is defined in JavaScript doesn’t mean it will be defined in PHP.

Yes, I mean the selectedDays array is defined and contains values, along with the code megazoid provided I can not see why selectedDays is undefined in php.

Dump the content of $_POST at the top of the PHP script so you can see exactly what is being passed. That will tell you whether the problem is with the ajax call or the PHP.

My selectedDays array is not showing.

So it isn’t being passed. What is being passed?

Where do you see these messages?

The value of a dropdown menu

In the error_log text file

So what is that in when it gets to the server. If it is getting passed then it will be in one of the fields that is displayed when you dump $_POST

The dropdown is in a form.

So is the problem this? -

 $.post('classes/post.class.php', {selectedDays: JSON.stringify(selectedDays)});

Felgall means that when you do vardump($_POST) in your PHP script you should see value passed from JavaScript inside that array. If you don’t see it, then you have to research why it is not getting passed (maybe you are just waiting for it in a wrong file or something like that).

Here is the results I get from var_dump: array(1) { [“freq”]=> string(1) “2” }

I have done a lot of research into posting array values to php and have made dozens of attempts. I know it’s difficult to help without seeing all my pages of code, but I will continue to try and figure out what the problems is.

Like I said in my first post, when I have link to an external php file that is not included then it works. So I’m presuming there is a problem with it passing the data to the wrong php file. Thanks for all your help guys.

How your form is supposed to be submitted?
I mean, what should happen when you click Submit button?
Should the form be submitted as usual, or do you want to send its fields only via AJAX in background?
I’m asking because according to code in your OP it looks like that now you have both actions (AJAX request is being sent and then form submits in regular way). And this is probably why you don’t see array, you’re just looking for it after second (regular) request and not after AJAX one.

If you want your form to be submitted only via AJAX, add this line to the click handler of Submit button:

e.preventDefault(); //prevent regular form submit action

Otherwise, if you want to keep default submit action of the form (with page reloading) you have to change way you’re using to pass array with the form.

To do that, add hidden field to the form:

<input type="hidden" name="selectedDaysJSON" value="">

and make form to put JSON representation of the array into that field on submit:

$("input[type='submit']").click(function(e) {
    $('input[name=selectedDaysJSON]').val( JSON.stringify(selectedDays) );
});

PHP code will remain the same in this case

1 Like

Thank you megazoid, that is one solution which I have looked into. I’m not too sure if it would cause any problems but there could possibly be around 100 array values at one time.

Another solution I’m going to try is once the form has been submitted, the user is sent to a confirmation page. I’m thinking I could send the array to the confirmation page and submit it there?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.