Okay so basically i’m trying to submit a form with some javascript arrays.
I expected this to take the form add the data to hidden fields and then post it automatically.
However I’m getting a strange error.
-
The fields added dynamically aren’t coming as hidden and are showing on screen. -
The code is working extremely slowly(running this locally so I can’t imagine why)
-
It doesn’t link to the PHP page but instead says: The connection to 10.0.0.2 was interrupted.(NOTE: The PHP page is valid and can be visited otherwise)
In this code correct and answers are javascript arrays
$("#questionForm").submit(function(e){
e.preventDefault();
var cform = $('<form></form>');
cform.attr("method", "post");
cform.attr("action"," <?php echo base_url()?>index.php/TestMaker/endTest");
var cfield = $('<input></input>');
cfield.attr("type", "hidden");
cfield.attr("name", "correct");
cfield.attr("value", correct);
cform.append(cfield);
cfield=$('<input></input>');
cfield.attr("type","hidden");
cfield.attr("name", "answers");
cfield.attr("value", answers);
cform.append(cfield);
$(document.body).append(cform);
cform.submit();
});
EDIT
Okay the hidden part is fixed. Error in the second field. Didn’t set it hidden.
Server Side Code:
class TestMaker extends CI_Controller{
function __construct() {
parent::__construct();
}
function endTest(){
$correct_arr=$_POST('correct');
echo "hi";
//$question_ids=$_POST('question_ids');
//$question_types=$_POST('question_type');
}
}
You’re accessing the $_POST array wrongly, the correct way of accessing an array in PHP is using square brackets
[], like this:Also, if you’re using CI, there’s a input class which wraps the $_POST array and is quite helpful, which can be used this way: