Jump to content

[SOLVED] submit form javascript


csckid

Recommended Posts

<form method="post" action="thank.php">
number=0;
questionno[number] = document.createElement("input");
	questionno[number].id = "questionno"+number;
	questionno[number].name = "questionno"+number;
	questionno[number].size =2;
	questionno[number].type = "text";
	questionno[number].value= 1+number;
<input type="submit" name="save">
</form>

 

 

now if i click on the submit button and try to get value of

isset($_POST['question0']) this returns false

 

any help plz

<form name="question" method="post" action="thanks.php">


<input type="button" id="create" value="add" onClick="addquestion()">

<input type="submit" id="savecreate" name="savecreate" value="save">


<script type="text/javascript">
number=0;
function addquestion(){
questionno[number] = document.createElement("input");
	questionno[number].id = "questionno"+number;
	questionno[number].name = "questionno"+number;
	questionno[number].size =2;
	questionno[number].type = "text";
	questionno[number].value= number;
}
</script>
</form>

 

i clicked on submit button and on thanks.php $_POST['questionno0'] was called... no result

Well, there is apparently some details you are leaving out, which makes this more difficult to debug. Based upon the code above I would have to assume there is a gloabl array variable for "questionno". Also the function you have never increases the variable "number" (see note below). Lastly, I don't se anything that actually adds the field to the page! Once I made those changes, it works fine for me.

 

form page

<html>
<head>
<script type="text/javascript">

//Gloabl variables
var questionno = new Array();
var number = 0;

function addquestion()
{
    //Create the form element
    questionno[number] = document.createElement("input");
    questionno[number].id = "questionno" + number;
    questionno[number].name = "questionno" + number;
    questionno[number].size = 2;
    questionno[number].type = "text";
    questionno[number].value = number;

    //Add form element to page
    document.getElementById("questionForm").appendChild(questionno[number]);
    number++;
    return;
}

</script>
</head>

<body>
<form name="question" id="questionForm" method="post" action="thanks.php">

<input type="button" id="create" value="add" onClick="addquestion()">
<input type="submit" id="savecreate" name="savecreate" value="save">

</form>
</body>
</html>

 

thanks.php page

<?php

echo "<pre>";
print_r($_POST);
echo "</pre>";

?>

 

If I add three fields to the form page the results I get on the thanks.php page are as follows:

Array
(
    [savecreate] => save
    [questionno0] => 0
    [questionno1] => 1
    [questionno2] => 2
)

 

 

however, it is completely unnecessary to add an index number to every field - and not as efficient. Simply create the fields with a name such that the resulting POST data will be returned as an array variable. (you would need to use an index for the ID values for the fields if you must use that).

questionno[number].name = "questionno[]";

 

Then on the processing page you can access all of the values entered into that group of fields using the variable $_POS['questionno'] which will be an array of all the values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.