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:
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.
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.
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.
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?