combobox in php

  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Posts: 34

Post 3+ Months Ago

hello again, really every time i got trouble with combo box.....

this time i want to get combo box value as well as its item label...

suppose i have a combo box having following format..

Code: [ Select ]
<form method="post" name="city" action="test2.php">
<select name="select">
<option value="1">bangalore</option>
<option value="2">mumbai</option>
</select>
</form>
  1. <form method="post" name="city" action="test2.php">
  2. <select name="select">
  3. <option value="1">bangalore</option>
  4. <option value="2">mumbai</option>
  5. </select>
  6. </form>


now when i print the accepted values in test2.php then it shows value i.e

Code: [ Select ]
<?php echo $_POST['select'];?>


output will be "1" or "2" but i want to get output as "bangalore" or "mumbai"....

so please help me...
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Code: [ Select ]
<form method="post" name="city" action="test2.php">
<select name="select">
<option value="bangalore">bangalore</option>
<option value="mumbai">mumbai</option>
</select>
</form>
  1. <form method="post" name="city" action="test2.php">
  2. <select name="select">
  3. <option value="bangalore">bangalore</option>
  4. <option value="mumbai">mumbai</option>
  5. </select>
  6. </form>


Whatever you want to print, put into the value parameter in the attribute...
  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Posts: 34

Post 3+ Months Ago

but i have need of both, value & label.....

& value should be only 1 or 2 & label should be same as above...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Is the information coming from the database?
  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Posts: 34

Post 3+ Months Ago

yup it will.......

actually what i m making a site for theater, so i need to set timing for all movies, for doing it i need three values date, city & movie, so the condition i hd ask u is for movies so i can set timing in another page, in 1st page user will select date, city & movie... then according to his selection, he will get timings.....
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Well, you can have the id in the value field and then select whatever you need from the database based on that ID but that is not recommended...

Or, if you are dynamically creating the select option you can have it generate something like...
Code: [ Select ]
<form method="post" name="city" action="test2.php">
<select name="select">
<option value="1,bangalore">bangalore</option>
<option value="2,mumbai">mumbai</option>
</select>
</form>
  1. <form method="post" name="city" action="test2.php">
  2. <select name="select">
  3. <option value="1,bangalore">bangalore</option>
  4. <option value="2,mumbai">mumbai</option>
  5. </select>
  6. </form>

And then the process would look like...
PHP Code: [ Select ]
<?php
$selected = explode(',', $_POST['select']);
$s_id = $selected[0];
$s_name = $selected[1];
?>
  1. <?php
  2. $selected = explode(',', $_POST['select']);
  3. $s_id = $selected[0];
  4. $s_name = $selected[1];
  5. ?>
  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Posts: 34

Post 3+ Months Ago

yup i think it will solved my problem, will try it..

again thanx bogey...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Your welcome... hope it works :)

Actually, I don't see how it could be a threat to use the value of a SELECT form element in a PHP form to select something from the database, but then again, I'm not the expert or even good at SQL Injection security and someone who is more knowledgeable on this topic would be able to explain how it could become an SQL Injection attack... but then, it may not be a threat... whatever :roll:

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 81 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.