Jump to content

no value when using onchange event


robert_gsfame

Recommended Posts

I have dynamic combobox

 

below is the html code and javascript

function show(){

if (document.form1.country.value == "Brazil"){document.getElementById('city').innerHTML = "<select name='mycity' id='mycity'><option value='ABC' id='ABC'>ABC</option></select>"}

 

<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">

<select name="country"><option value="brazil" id="brazil" onchange= onchange="show()">Brazil</option>

 

<div id="city"></div>

<input type="submit" value="submit>

</form>

 

The problem is with the php code

$country=$_POST['country'];

$city=$_POST['mycity']; <--------------- no value

 

how to solve this??thanks in advance!

Link to comment
Share on other sites

this is because you are adding the mycity select box to the dom dynamically so it will not exist when a post is performed.  One way to solve this is to have a hidden input element and set its value to the selected value in the dropdown via js then reference the hidden element in the php to get the value.  Any time you add a form element after the dom has loaded it wont be seen by the browser when the form is submitted.

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.