1

I have a HTML form that submits to PHP. There is an onChange event on a drop down box that calls a JavaScript function which writes a hidden variable into the form. This additional hidden variable doesnt show up in PHP though, why is this?

So;

<script language="JavaScript">
function change() {
  document.getElementById("myDiv").innerHTML="<input type=\"hidden\" name=\"blah\" value=\"1\"/>";
  return;
}
</script>

<form method="post" action="test.php" />
<select name="cid" id="cid" onChange="change();">
<option value="lala">lala</option>
</select>
<div id="myDiv"></div>
</form>

PHP doesn't see $_POST['blah']?

2
  • It's hard to see, where the error lies. Please paste the code where the onChange function is called. And I can't see any submit Button which submits the form. And by the way, it is: <script type="text/javascript"> Commented Aug 22, 2011 at 13:22
  • OK, I have added more code to show whats going on, its a long form so I tried to cut some of the other crap out. Commented Aug 22, 2011 at 14:09

3 Answers 3

3

A more logical solution anyway would be to just add the hidden field to your form, and set the value to 1 in the onChange function.

Sign up to request clarification or add additional context in comments.

1 Comment

I didnt do it this way round because I didnt want the variable to be there if the drop down box wasnt changed but this way works, so thanks :D
2

You must have been setup something wrong, it dose work fine when I test that. Are you sure you call the method before you submit the form ?

Comments

2

I can´t see you executing your change() function.

2 Comments

As I said, the function is called by the onChange event of a drop down box, I can see the code change in the page.
I think adding the field to the DOM doesn't make the browser implicitly add it to the form it has in memory. As stated in my answer; it would make more sense to have the field in the form from the start, and set the value using JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.