10 Replies - 14149 Views - Last Post: 06 July 2010 - 04:31 AM Rate Topic: -----

#1 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

PHP & MySQL Dropdown List.

Posted 06 July 2010 - 01:35 AM

Hi,

I have a table in my database, which holds a contact 'user', their mobile number'mobile' and their landline'landline'. I have populated a dropdown list with the data. here is the code i am using:
           <?php
                /* Include global variable file*/
                include_once("common-inc/inc_sql.php"); 
                
                /* Create connection */
                $conn=mysql_connect(HOSTNAME,USERNAME,PASSWORD) or die(mysql_error());
                
                /* select default database*/
                mysql_select_db(DATABASE);

                $sql = "SELECT * from `contact`";
                $result=mysql_query($sql);
                ?>
                         <form id="form1" name="form1" method="post" action="">
                        
                          <select name="user" id="user">
                          <option value="0" selected="selected">Select</option>
                          <?php while($rows=mysql_fetch_assoc($result)){?>
                        <option value="<?php echo $rows['user'] ?>"><?php echo $rows['user'] ?></option>
                         <?php }
                          mysql_free_result($result);?>
                          </select>
                        </form>
                        <p>&nbsp;</p>
          <div id="info">
                                <table width="100%">
<tbody>
<tr>
<td height="10" width="42%">
<span>Landline</span>
</td>
<td width="50%">
//display landline here
echo $rows['landline']
&nbsp;
<br/>
</td>
</tr>
<tr>
<td width="40%">
<span>Mobile</span>
</td>
<td width="50%">
//display mobile here
echo $rows['mobile']
&nbsp;
<br/>
</td>
</tr>
</tbody>
</table>             
          </div>


What i am trying to achieve is that everytime contact is selected from the dropdown list their mobile and landline is displayed. at the moment i cant do that.

can anyone give me guidelines on how to approach this.

thank you

Is This A Good Question/Topic? 0
  • +

Replies To: PHP & MySQL Dropdown List.

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 01:42 AM

The way I'd do it is to have the elements present in the HTML source but hidden via CSS, and make them become visible when the value of the select element changes, showing different information depending on the value selected.

If you don't want them in the HTML source starting off, though, you could use an AJAX call to grab the additional information and populate empty elements with the data (or create new ones containing the information).

This post has been edited by Valek: 06 July 2010 - 01:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,448
  • Joined: 10-May 07

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 01:42 AM

I would try using the following code :

                $sql = "SELECT * from contact";
                $result=mysql_query($sql);
                if($result) {
                ?>
                         <form id="form1" name="form1" method="post" action="">
                        
                          <select name="user" id="user">
                          <option value="0" selected="selected">Select</option>
                          <?php
                          $i=0; 
                          while($rows=mysql_fetch_assoc($result)) { 	                          
	                          echo "<option value=\"".$i."\">".$rows['user']."</option>";
                                  $i++;   
                          }    
                         <?php }
                          mysql_free_result($result);?>
                          </select>
                        </form>
                <?php
             	}
             	else die("SQL failed.".$sql);
             	?>


You maybe asking : "Why use a numeric value?"

http://www.dreaminco...-drop-down-box/
Was This Post Helpful? 0
  • +
  • -

#4 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 01:59 AM

View PostValek, on 06 July 2010 - 12:42 AM, said:

The way I'd do it is to have the elements present in the HTML source but hidden via CSS, and make them become visible when the value of the select element changes, showing different information depending on the value selected.

If you don't want them in the HTML source starting off, though, you could use an AJAX call to grab the additional information and populate empty elements with the data (or create new ones containing the information).



i am not familar with Ajax, my knowledge is novice at best. how can i go about that?
Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 02:03 AM

As long as you know Javascript, AJAX is extremely easy (still fairly easy if you're familiar with at least one programming language). Here is an excellent tutorial for it :)

This post has been edited by Valek: 06 July 2010 - 02:03 AM

Was This Post Helpful? 1
  • +
  • -

#6 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 03:11 AM

View PostValek, on 06 July 2010 - 01:03 AM, said:

As long as you know Javascript, AJAX is extremely easy (still fairly easy if you're familiar with at least one programming language). Here is an excellent tutorial for it :)


Hi I tried the tutorial and nothing is displayed.

on my contact.php, i added this to the header
<script type="type/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("Info").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("Info").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","contact.php?q="+str,true);
xmlhttp.send();
}
  </script>



on the same page i added the onchange to the dropdown list:
 <?php
                /* Include global variable file*/
                include_once("common-inc/inc_sql.php"); 
                
                /* Create connection */
                $conn=mysql_connect(HOSTNAME,USERNAME,PASSWORD) or die(mysql_error());
                
                /* select default database*/
                mysql_select_db(DATABASE);

                $sql = "SELECT * from `contact`";
                $result=mysql_query($sql);
                ?>
                         <form id="form1" name="form1" method="post" action="">
                        
                          <select name="user" id="user" onchange="showUser(this.value)">
                          <option value="0" selected="selected">Select</option>
                          <?php while($rows=mysql_fetch_assoc($result)){?>
                        <option value="<?php echo $rows['user'] ?>"><?php echo $rows['user'] ?></option>
                         <?php }
                          mysql_free_result($result);?>
                          </select>
                        </form>
                        <p>&nbsp;</p>



then is the display area i added the following
<div id="Info">
 <?php  
 $q=$_GET["q"];
$conn=mysql_connect(HOSTNAME,USERNAME,PASSWORD) or die(mysql_error());
mysql_select_db(DATABASE);
$mysql="SELECT * FROM contact WHERE user = '".$q."'";
$results = mysql_query($mysql);
while($row = mysql_fetch_array($results))
  {
echo $row['mobile'];
  }
 ?>           
          </div>


nothing is being displayed.

This post has been edited by blakpower: 06 July 2010 - 03:15 AM

Was This Post Helpful? 1
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 03:19 AM

You're definitely on the right track. Two questions, though.

What are you echoing out from contact.php? That's what's going to get picked up in xmlhttp.responseText. (A good way to separate mobile and landline numbers would be to have the script AJAX calls echo it out in a manner such as mobile|landline, then use var numbers = xmlhttp.responseText.split("|");, such that it'll make numbers an array containing the two numbers, separated).

Are you using Firefox? If so, you should get a development add-on (Firebug or Web Developer will work nicely) and check to ensure you're not encountering Javascript errors.

This post has been edited by Valek: 06 July 2010 - 03:23 AM

Was This Post Helpful? 0
  • +
  • -

#8 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 03:31 AM

View PostValek, on 06 July 2010 - 02:19 AM, said:

You're definitely on the right track. Two questions, though.

What are you echoing out from contact.php? That's what's going to get picked up in xmlhttp.responseText. (A good way to separate mobile and landline numbers would be to have the script AJAX calls echo it out in a manner such as mobile|landline, then use var numbers = xmlhttp.responseText.split("|");, such that it'll make numbers an array containing the two numbers, separated).

Are you using Firefox? If so, you should get a development add-on (Firebug or Web Developer will work nicely) and check to ensure you're not encountering Javascript errors.



I used Firebug and the function wasnt defined so i put it in a script.js file. and it works sort of. when i select a user, it displays the whole page in the display area and it displays the numbers as well. i assume this has to do with me echoing the contact page.
Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 03:42 AM

Yeap. That's partly why I told you where xmlhttp.responseText is coming from, as I had a feeling that's what you were gonna have happen.

You'd want a script that runs a query on the user to pull out the pieces of data you need from the database (make sure you sanitize the input, though, you can never trust user-accessible input, even with AJAX). Once you get back the values, concatenate them with the pipe between, echo that, and end the script. Make sure to use the split() method of the xmlhttp.responseText string, so that it creates an array of the two numbers. Then, you'd simply tell the browser where to place each piece of data via Javascript like you did with placing the page contents before.

Of course, you can have it just echo the HTML that's gonna go inside the 'Info' element and just have that get input the way you're already doing it, and it'd work fine :)
Was This Post Helpful? 1
  • +
  • -

#10 blakpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 09-February 09

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 04:24 AM

View PostValek, on 06 July 2010 - 02:42 AM, said:

Yeap. That's partly why I told you where xmlhttp.responseText is coming from, as I had a feeling that's what you were gonna have happen.

You'd want a script that runs a query on the user to pull out the pieces of data you need from the database (make sure you sanitize the input, though, you can never trust user-accessible input, even with AJAX). Once you get back the values, concatenate them with the pipe between, echo that, and end the script. Make sure to use the split() method of the xmlhttp.responseText string, so that it creates an array of the two numbers. Then, you'd simply tell the browser where to place each piece of data via Javascript like you did with placing the page contents before.

Of course, you can have it just echo the HTML that's gonna go inside the 'Info' element and just have that get input the way you're already doing it, and it'd work fine :)



Valek,

Thanks for your help, i echoed it through another page which handled the html display.
Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: PHP & MySQL Dropdown List.

Posted 06 July 2010 - 04:31 AM

Excellent! I'm very happy you managed to get it working :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1