Jump to content

Php Triple Drop Down Dynamically Updating Text


backspc

Recommended Posts

I am having some real trouble getting my head around php (just starting out) I have successfully implemented this triple drop down ajax script http://roshanbh.com....ax-and-php.html my problem is getting it to populate text boxes as well.

 

<? $country=intval($_GET['country']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,statename,phone,contact FROM state WHERE countryid='$country'";
$result=mysql_query($query);
?>
<select name="state" onchange="getCity(<?=$country?>,this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['id']?> <?=$row['statename']?> <?=$row['phone']?> <?=$row['contact']?></option>
<? } ?>
</select>
<br/>
on change update this to the selected contact
<br/>
<input type=text value=<?=$row['contact']?>>
<br/>
on change update this to the selected phone
<br/>
<input type=text value=<?=$row['phone']?>>

 

I have been battling this for about a week now it getting beyond a joke :(

 

Free hugs for help :)

Link to comment
Share on other sites

Hi Jessica,

Thanks for the reply.

That java I am using for this part is:

<script language="javascript" type="text/javascript">
// Roshan's Ajax dropdown code with php
// This notice must stay intact for legal use
// Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
// If you have any problem contact me at http://roshanbh.com.np
function getXMLHTTP() { //fuction to return the xml http object
 var xmlhttp=false;
 try{
  xmlhttp=new XMLHttpRequest();
 }
 catch(e) { 
  try{  
   xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(e){
   try{
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(e1){
 xmlhttp=false;
   }
  }
 }

 return xmlhttp;
   }


function getState(countryId) { 

  var strURL="findState.php?country="+countryId;
 var req = getXMLHTTP();

 if (req) {


  req.onreadystatechange = function() {
   if (req.readyState == 4) {
 // only if "OK"
 if (req.status == 200) {	 
  document.getElementById('statediv').innerHTML=req.responseText;	 
 } else {
  alert("1 There was a problem while using XMLHTTP:\n" + req.statusText);
 }
   }   
  }  
  req.open("GET", strURL, true);
  req.send(null);
 } 
}
function getCity(countryId,stateId) { 
 var strURL="findCity.php?country="+countryId+"&state="+stateId;
 var req = getXMLHTTP();

 if (req) {

  req.onreadystatechange = function() {
   if (req.readyState == 4) {
 // only if "OK"
 if (req.status == 200) {	 
  document.getElementById('citydiv').innerHTML=req.responseText;	 
 } else {
  alert("2 There was a problem while using XMLHTTP:\n" + req.statusText);
 }
   }   
  }  
  req.open("GET", strURL, true);
  req.send(null);
 }
}
</script>

After the first drop down is selected the second one is populated with the code from above ie

<select name="state" onchange="getCity(<?=$country?>,this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['id']?> <?=$row['statename']?> <?=$row['phone']?> <?=$row['contact']?></option>

that is when I would like a text box also populated with just the phone and another with just the contact.

 

Hope this makes sense :)

Link to comment
Share on other sites

As one of our member's signatures says, "Java is to Javascript as fun is to funeral." You have javascript, it is not java.

 

 

You may want to look into using jQuery.

 

The problem you're having is that inputs have a value, whereas textareas have innerHTML. Try changing the innerHTML to value. If that doesn't work, definitely look at jQuery. :) It's so much easier.

Link to comment
Share on other sites

Thanks I didn't understand about java & javescript so did a bit of looking up (thanks for pointing it out for me)

I tried the innerHTML values but that didn't get me anywhere in the end I used


<?phpmysql_select_db('db_ajax');
$query="SELECT id,statename,phone,contact FROM state WHERE countryid='$country'";
$result2=mysql_query($query);
?>


<?php
while ($row = mysql_fetch_assoc($result2)) {
$text3 =$row['contact'];
} ?>


<select style='display:none' id="Text3">
<option value="val0"><?php echo htmlentities($text3); ?></option>
</select>


<script language="Javascript" type="text/javascript">
<!--
function showSelected()
{
var selObj = document.getElementById('Text3');
var txtText3Obj = document.getElementById('txtText3');
var selIndex = selObj.selectedIndex;
txtText3Obj.value = selObj.options[selIndex].text;
//-->
</script>
<input type="button" value="Insert" onclick="showSelected();" />
  <input type="text" id="txtText3" name="contact">

 

There is a better way but this works for me at the moment so on to the next issue...

Thanks for your help

Edited by backspc
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.