Jump to content

$POST problem


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>

</form>

 

The problem is with the php code

$country=$_POST['country'];

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

 

how to solve this??thx in advance!

Link to comment
Share on other sites

1. You need to tie your onchange event to the select element, not one of it's options.

2. You need to check it's selected index, not its value (google 'javascript select onchange' to see examples of what I mean).

3. You do have a submit button for your form, correct?  You need a way to send the value to PHP.

Link to comment
Share on other sites

Quick, dirty, and untested:

 

<!DOCTYPE html>
<html>
   <head></head>

   <body>
      <form name="form1" action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
         <select name="country" id="country">
            <option value="brazil">Brazil</option>
         </select>

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

         <input type="submit" name="submit" value="Submit" />
      </form>
   </body>

   <script type="text/javascript">
      var oSelect = document.getElementById('country');

      oSelect.onchange = function() {
         if (this.options[this.selectedIndex].value == 'brazil') {
            // do stuff
         }
      }
   </script>

</html>

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.