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

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.