| | |
select option problem??
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2008
Posts: 293
Reputation:
Solved Threads: 11
PHP Syntax (Toggle Plain Text)
<form> <table border="0" width="100%"> <tr> <td align="right" width="60%"><select name="select" size="1" > <option>Select Category</option> <? $sql1=mysql_query("select name from categories") or die(mysql_error()); $num=mysql_num_rows($sql1); while($row=mysql_fetch_array($sql1)) { $category = $row['name']; ?> <option><?php echo $category; ?></option> <? } ?> </select> </td> </tr> </table> </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
PHP Syntax (Toggle Plain Text)
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.
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 4
php Syntax (Toggle Plain Text)
<form> <table border="0" width="100%"> <tr> <td align="right" width="60%"><select name="select" size="1" onChange='submit()'> <option value='0'>Select Category</option> <? $sql1=mysql_query("select name from categories") or die(mysql_error()); $num=mysql_num_rows($sql1); while($row=mysql_fetch_array($sql1)) { $category = $row['name']; ?> <option value='<?php echo $category; ?>'><?php echo $category; ?></option> <? } ?> </select> </td> </tr> </table> </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.
Programmer
.Net Consulting
If this post is answer for your query then don't forget to mark as an answer.
•
•
Join Date: Apr 2008
Posts: 293
Reputation:
Solved Threads: 11
Here i am modified code...
Here i am choosing option from list...
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??
PHP Syntax (Toggle Plain Text)
<form> <table border="0" width="100%"> <tr> <td align="right" width="60%"> <select name="cmbcategory" size="1" onChange="submit();"> <option value="0">Select 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>
Here i am choosing option from list...
PHP Syntax (Toggle Plain Text)
echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n";
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??
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> 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.
(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.
2. Place this in the widget:
3. Under your form, place this "placeholder":
3. In your myAjax.js file:
4. In the new changelevelname.php file:
Oh Darn! Just realised, do you just want to show the name as separate text? If so, forget Ajax, just do this:
In javascript (in a file or as part of a script tag):
(i) Forms should be passed to a form handler where data can be validated and appropriate action taken, e.g.
PHP Syntax (Toggle Plain Text)
<form id=myform" name="myform" method = "post" action="myhandler.php"> ... </form>
(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.
PHP Syntax (Toggle Plain Text)
<script src="prototype.js"></script> <script src="myAjax.js"></script>
PHP Syntax (Toggle Plain Text)
<select id="namelevel" name="namelevel" onchange="alterName();return false;"> ... </select>
PHP Syntax (Toggle Plain Text)
<div id="nametext"></div>
3. In your myAjax.js file:
PHP Syntax (Toggle Plain Text)
function alterName(){ var nameLevel = $F('namelevel'); //this retrieves the value of the option selected var url = changelevelname.php; //the new php file holding the sql var oOptions = {method:post,parameters: 'id=' + nameLevel}; var oAjax = new Ajax.Updater('nametext',url,oOptions); //this replaces the div called 'nametext' with the output from changelevelname.php }
PHP Syntax (Toggle Plain Text)
$id = addslashes(htmlentities($_POST['id'])); $sql = "SELECT name FROM categories WHERE id = '{$id}'"; $rs = mysql_query($sql); 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:
PHP Syntax (Toggle Plain Text)
<select id="changename" onchange="repeatMe();return false;"> ... </select> ... <p id="output"></p>
PHP Syntax (Toggle Plain Text)
function repeatMe(){ var x=document.getElementById("changename"); document.getElementById('output').innerHTML = "You selected: " + x.options[x.selectedIndex].text; }
Try this (I removed the size="1" attribute in your select tag).
OR if that doesn't work: Can you post the whole code instead?
PHP Syntax (Toggle Plain Text)
<form> <table border="0" width="100%"> <tr> <td align="right" width="60%"> <select name="cmbcategory" 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>
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.
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.
![]() |
Similar Threads
- Popups and problem loading desktop (Viruses, Spyware and other Nasties)
- OPTION problem (PHP)
- C++ menu problem (C++)
- IE 6 Problem (Web Browsers)
- Possible CWS problem (Viruses, Spyware and other Nasties)
- Trojan Problem (Viruses, Spyware and other Nasties)
- problem with caller.exe (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: How to post form to database and to an url
- Next Thread: Get an html tags name or id attribute?
| Thread Tools | Search this Thread |
301 access apache api array autocomplete beginner binary broken button cakephp checkbox class cms code compression cron curl data database dataentry date display dropdown dropdownlist duplicates dynamic echo email error execution file files folder form forms function functions google href htaccess html htmlspecialchars httppost image include insert integration ip javascript joomla jquery limit link links login mail md5 menu methods mlm multiple mysql oop paypal pdf pdfdownload php phpvotingscript query radio random recursion remote script search secure server session sessions source space sql subscription syntax system table tutorial tutorials update upload url validator variable video virus volume votedown web youtube





