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
https://forums.phpfreaks.com/topic/196483-no-value-when-using-onchange-event/
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.

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.