John Davidson

javascript - Associative Arrays sent via php POST aren't complete

0 comments
Message:


I have a a dynamic form with a handful of text inputs that share names (for example, "lessons[] and "outlines[]". When I validate after submitting, I print everything to console and it's all there. But in my submit.php, many of the associative arrays stored in post only have about the first 20% of the data that they should.


This is the very last thing I do when validating the form to make sure everything is there (and it is). Structures is 5, all 5 outlines are present, and all 41 lessons are there. It's all accurate and what I expect to see.


var structures = document.getElementsByName("structure[]");
console.log("Structures: " + structures.length);

console.log("OUTLINES")
var outline_ids = document.getElementsByName("outline_ids[]");
var outlines = document.getElementsByName("outlines[]");
for (var i = 0; i < outlines.length; i++){
console.log(outline_ids[i].value + ": " + outlines[i].value);
}

console.log("LESSONS");
var lessons = document.getElementsByName("lessons[]");
var section_info = document.getElementsByName("section_info[]");
for (var i = 0; i < lessons.length; i++){
console.log(lessons[i].value + ": " + section_info[i].value);
}

return true;

Then, at the top of the submit php script, I run print_r($_POST); and see that:


structure only has 2 values instead of all 5


outline_ids has 2 instead of 5


outlines has 1 instead of 5


both lessons and section_info have only 8 values instead of 41


There are zero lines of code between returning true in validation, and printing POST in the submit script. Any ideas why so much data is being lost in POST? Any help would be appreciated. Thank you!


Edit: In the form script, the form itself has the tag <form name="form" action="course_submit.php?courseID=<?php echo $id?>" onsubmit="return validateForm()" method="post"> in case that helps.



My Answer:
't directly supported in JavaScript. However, you can convert the associative array sent via PHP POST into a JavaScript object using JSON. Here's an example:

PHP code to send associative array via POST:

php
$data = array(
'name' => 'John Doe',
'age' => 30,
'email' => 'johndoe@example.com'
);

echo json_encode($data);


JavaScript code to receive and convert the associative array into a JavaScript object:

javascript
fetch('your_php_script.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
console.log(data); // Output: {name: "John Doe", age: 30, email: "johndoe@example.com"}
});


In this example, the PHP script sends the associative array as a JSON string, which is then received and parsed into a JavaScript object using the `json()` method. You can then access the values of the associative array as properties of the JavaScript object.

Rate this post

4 of 5 based on 2048 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us