select option problem??

Reply

Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

select option problem??

 
0
  #1
Dec 29th, 2008
  1. <form>
  2. <table border="0" width="100%">
  3. <tr>
  4.  
  5. <td align="right" width="60%"><select name="select" size="1" >
  6. <option>Select Category</option>
  7. <? $sql1=mysql_query("select name from categories") or die(mysql_error());
  8. $num=mysql_num_rows($sql1);
  9. while($row=mysql_fetch_array($sql1))
  10. {
  11. $category = $row['name'];
  12.  
  13. ?>
  14. <option><?php echo $category; ?></option>
  15.  
  16. <?
  17. }
  18. ?>
  19. </select>
  20. </td>
  21. </tr>
  22. </table>
  23. </form>

Here i got i.e. it display
in list student, teacher ,professor etc.
i want to do like when click on any option without press button
only click on option in list
they fire query
  1. select * from table where name='student'; // i.e. selected value from list

my problem is how to check that option is select or not
or get the selected value ??
Last edited by Aamit; Dec 29th, 2008 at 2:15 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: dilipv is an unknown quantity at this point 
Solved Threads: 4
dilipv dilipv is offline Offline
Light Poster

Re: select option problem??

 
0
  #2
Dec 29th, 2008
  1. <form>
  2. <table border="0" width="100%">
  3. <tr>
  4. <td align="right" width="60%"><select name="select" size="1" onChange='submit()'>
  5. <option value='0'>Select Category</option>
  6. <? $sql1=mysql_query("select name from categories") or die(mysql_error());
  7. $num=mysql_num_rows($sql1);
  8. while($row=mysql_fetch_array($sql1))
  9. {
  10. $category = $row['name'];
  11.  
  12. ?>
  13. <option value='<?php echo $category; ?>'><?php echo $category; ?></option>
  14.  
  15. <?
  16. }
  17. ?>
  18. </select>
  19. </td>
  20. </tr>
  21. </table>
  22. </form>

i think you can either submit your form for processing php script on change event of select or you can call javascript function which will call php page for processing. 2nd option is known as Ajax with php.
If you wanna see ajax and php then visit given url
http://www.ajaxprojects.com/ajax/tut...php?itemid=333
hope this will help you.
Dilip Kumar Vishwakarma
Last edited by peter_budo; Dec 29th, 2008 at 5:53 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Dilip Kumar Vishwakarma
Programmer
.Net Consulting
If this post is answer for your query then don't forget to mark as an answer.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: select option problem??

 
0
  #3
Dec 29th, 2008
Here i am modified code...
  1. <form>
  2. <table border="0" width="100%">
  3. <tr>
  4. <td align="right" width="60%">
  5. <select name="cmbcategory" size="1" onChange="submit();">
  6. <option value="0">Select Category</option>
  7. <? $sql1=mysql_query("select name from categories") or die(mysql_error());
  8. while($row=mysql_fetch_array($sql1))
  9. {
  10. $category = $row['name'];
  11. ?>
  12. <option value="<?php echo $category; ?>"><?php echo $category; ?></option>
  13.  
  14. <?
  15. }
  16. ?>
  17. </select>
  18. <?
  19. echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n";
  20. ?>
  21. </td>
  22. </tr>
  23. </table>
  24. </form>

Here i am choosing option from list...
  1. echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n";
I get correct echo value of selected option
But in list view it shows Select Category

e.g suppose i chose student...
in echo it gives You are selected: student
but in option list it shows Select Category not student

where is problem in code??
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: select option problem??

 
0
  #4
Dec 29th, 2008
Try:

<form>
<table border="0" width="100%">
<tr>
<td align="right" width="60%">
<select name="cmbcategory" size="1" onChange="submit();">
<option value="0">Select <?php if (isset($_GET['cmbcategory'])) {echo $_GET['cmbcategory'];} else echo "category"; ?></option>
<? $sql1=mysql_query("select name from categories") or die(mysql_error());
  while($row=mysql_fetch_array($sql1))
 {
	$category = $row['name'];
?>
 <option value="<?php echo $category; ?>"><?php echo  $category; ?></option>

 <?     
     }
  ?>
  </select>
   <?     
	    echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n"; 
  ?>
 </td>		
</tr>
</table>
</form>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 942
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 125
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: select option problem??

 
0
  #5
Dec 29th, 2008
If I get you right, you want to display the selection as additional text below the same form? This doesn't seem to make much sense.

(i) Forms should be passed to a form handler where data can be validated and appropriate action taken, e.g.

  1. <form id=myform" name="myform" method = "post" action="myhandler.php">
  2. ...
  3. </form>
  4.  

(ii) Also, form data should be passed as POST and not GET, otherwise an user could pass data using the url in the address bar, e.g.
http://mypage.php?cmbcategory=professor

If on the other hand, your form just has the select widget and you don't really any other data validated - use AJAX. This will not change the widget because the page is not reloaded. The easiest way to do this would be:

1. Download and link to the Prototype library. Also create a new js file for your ajax/js calls.

  1. <script src="prototype.js"></script>
  2. <script src="myAjax.js"></script>
2. Place this in the widget:
  1. <select id="namelevel" name="namelevel" onchange="alterName();return false;">
  2. ...
  3. </select>
3. Under your form, place this "placeholder":

  1. <div id="nametext"></div>


3. In your myAjax.js file:

  1. function alterName(){
  2. var nameLevel = $F('namelevel'); //this retrieves the value of the option selected
  3. var url = changelevelname.php; //the new php file holding the sql
  4. var oOptions = {method:post,parameters: 'id=' + nameLevel};
  5. var oAjax = new Ajax.Updater('nametext',url,oOptions); //this replaces the div called 'nametext' with the output from changelevelname.php
  6. }
4. In the new changelevelname.php file:

  1. $id = addslashes(htmlentities($_POST['id']));
  2. $sql = "SELECT name FROM categories WHERE id = '{$id}'";
  3. $rs = mysql_query($sql);
  4.  
  5. echo "You are selected: {$rs['name']}";

Oh Darn! Just realised, do you just want to show the name as separate text? If so, forget Ajax, just do this:

  1. <select id="changename" onchange="repeatMe();return false;">
  2. ...
  3. </select>
  4. ...
  5. <p id="output"></p>
In javascript (in a file or as part of a script tag):

  1. function repeatMe(){
  2. var x=document.getElementById("changename");
  3. document.getElementById('output').innerHTML = "You selected: " + x.options[x.selectedIndex].text;
  4. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: select option problem??

 
0
  #6
Dec 30th, 2008
hi kanaku, i tried your code but no category is displayed ....
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: select option problem??

 
0
  #7
Dec 30th, 2008
Try this (I removed the size="1" attribute in your select tag).

  1. <form>
  2. <table border="0" width="100%">
  3. <tr>
  4. <td align="right" width="60%">
  5. <select name="cmbcategory" onChange="submit();">
  6. <option value="0">Select <?php if (isset($_GET['cmbcategory'])) {echo $_GET['cmbcategory'];} else echo "category"; ?></option>
  7. <? $sql1=mysql_query("select name from categories") or die(mysql_error());
  8. while($row=mysql_fetch_array($sql1))
  9. {
  10. $category = $row['name'];
  11. ?>
  12. <option value="<?php echo $category; ?>"><?php echo $category; ?></option>
  13.  
  14. <?
  15. }
  16. ?>
  17. </select>
  18. <?
  19. echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n";
  20. ?>
  21. </td>
  22. </tr>
  23. </table>
  24. </form>

OR if that doesn't work: Can you post the whole code instead?
If you know ASP, you can save other daniweb members from idiots like me by helping out in this forum.

Visit this thread
if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC