Jump to content

[SOLVED] form not POSTing values help pls!


TheStalker

Recommended Posts

ok i have a form that includes some ajax drop down lists. When a country is selected it fills the next drop down list with values from a database.

 

The problem i have is that only the values from the first drop down list (country) is posted. The ones that are from the drop downs created with the AJAX are not!

 

I am pretty new to AJAX so go easy on me

 

here is my code

 

 

main.php

<html>
<head>
<script type="text/javascript" src="selectCountry.js"></script>
</head>
<body>

<form action="main2.php" method ="POST">
Country:
<select onchange="showCountry(this.value)" name="country" >
<option value="">Select one</option>
<option value="1">US</option>
<option value="2">UK</option>
<option value ="3">France</option>
</select>

<input type='submit' value='submit'>
</form>

<p id="here">City: <input type='text' name="city"></p>

</form>

</body>
</html>

 

selectCountry.js

 

var xmlhttp;

function showCountry(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
	alert ("Your browser does not support HTTP Request!");
	return;
}
var url="getCountry.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
	document.getElementById("here").innerHTML=xmlhttp.responseText;
}
else 
{
	document.getElementById("here").innerHTML = '<img src="ajax-loader.gif"> Loading..';
}
}


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}PHP

 

 

getCountry.php

<?php 
$q=$_GET["q"]; 

$con = mysql_connect('localhost', 'root', ''); 
if (!$con) 
  { 
  die('Could not connect: ' . mysql_error()); 
  } 

mysql_select_db("ajax_demo", $con); 

if ($q == 1) 
{ 
    $sql= mysql_query("SELECT * FROM states"); 
     
    echo "State: "; 
    echo "<select name='state'>"; 
            echo "<option value=''>Select one</option>"; 
    while($row = mysql_fetch_array($sql)) 
        { 
            echo "<option value ='1'>".$row['state']."</option>"; 
        }     
        echo "</select>"; 
     

    echo "<p>City: <input type='text'></p>"; 
         
         
} 
else if ($q == 2) 
{ 
    $sql= mysql_query("SELECT * FROM ukTowns"); 
     
    echo "City: "; 
    echo "<select name='city'>"; 
            echo "<option value=''>Select one</option>"; 
    while($row = mysql_fetch_array($sql)) 
        { 
            echo "<option value ='1'>".$row['town']."</option>"; 
        }     
        echo "</select>"; 
     
} 
else if ($q != 1 || $q !=2) 
{ 
    echo "<p>City: <input type='text'></p>"; 
} 



mysql_close($con); 
?> 
thanks for looking ....

 

main2.php

<?php
echo $_POST['country'];
echo $_POST['state'];
echo $_POST['city'];
?>

 

 

i dont see any values for state or city ??

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.