I am making select list from database table everything works fine only issue is in option value if the value consist of two words like "New York" it only returns "New". Here is the code
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['CityName']?>><?=$row['CityName']?></option>
<? } ?>
</select>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
The " " .
You'll have to surrond the value in "", the HTML code thinks the York part is just another attribute of the option tag, so:
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
You haven't included the "" while giving the values. if you look at the html created for the select box by your code is something like
<option value="new" york>new york</option>
correct solution is given below, just included the quotation while giving value of option tag.
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
<? } ?>
</select>
Just use "" for the value of value same as you write HTML tag.
Related
<select name='test'>
<option value='north' india>north india</option>
<option value='goa'>goa</option>
</select>
If I select "north india" in the list PHP echos only "north". Why is that?
This HTML is generated by the PHP code below:
$cond_city= "WHERE state=? GROUP BY cityName ORDER BY cityName ASC";
$data_city= array($state);
$select_city=$this->select_rows(TABLEPREFIX.'region','*',$cond_city,$data_city);
while($arr_city=$select_city->fetch(PDO::FETCH_ASSOC)){
echo '<option value='.$arr_city['cityName'].'>'.$arr_city['cityName'].'</option>';
}
In your code, value is equal to 'north' because you misplaced your quote:
<option value='north' india>north india</option>
You need this instead:
<option value='north india'>north india</option>
To get this result, you will need to add quotes in your PHP code, like this:
<?php
$cond = "WHERE state=? GROUP BY cityName ORDER BY cityName ASC";
$data = array($state);
$select=$this->select_rows(TABLEPREFIX.'region','*',$cond_city,$data_city);
while ($arr_city=$select_city->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$arr_cit.' '.$sel_cnt.' >'.$arr_city['cityName']."'</option>';
}
?>
Because you have set option's value as 'north'. If you want 'north india'as whole then replace below code.
<select name='test'>
<option value='north india'>north india</option>
<option value='goa'>goa</option>
</select>
it's happening because when you are submitting your data, you have value for the name of the input (select in your case). To get the desired value you should indicate it in the value attribute of the select.
<select name='test'>
<option value="north india">North India</option>
</select>
Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>
I have a HTML Form with Drop Down options which pass the values to php sql query I have on the same page as the form. This works if I use the values with LIKE statements but when I try to use the posted values with BETWEEN or >= it doesnt work. If i try with the actual values it does work. Could someone advise on what I am doing wrong. I have posted my HTML Form with the PHP code for the SQL Query.
HTML:
<html><form action="#" method="GET"></select>
<select name="MinPrice">
<option value="">Min Price</option>
<option value="">No Min</option>
<option value="500">£500</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option>
<option value="">Show All</option>
</select>
<select name="MaxPrice">
<option value="">Max Price</option>
<option value="">No Max</option>
<option value="500">£500</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option></select></form></html>
PHP:
<?php
mysql_connect("****", "****", "****");
mysql_select_db("****");
$sql = mysql_query("SELECT * FROM Product WHERE Price >= '%$_GET[MinPrice]%'");
echo "<h3>...Something...";
Or
<?php
mysql_connect("****", "****", "****");
mysql_select_db("****");
$sql = mysql_query("SELECT * FROM Product WHERE Price BETWEEN '%$_GET[MinPrice]%' AND '%$_GET[MaxPrice]%'");
echo "<h3>...Something...";
i dont get any results back, but if i exchange the GET Values with actual numbers it works as I want. Anyone got any suggestions on this?
I have tried absolutely everything I know
why action is "#" ?
if your php code is another page put the name in actions :
ex:
<form action="selectproduct.php" method="GET">
or if php code is on html page set action :
<form action="<?php $PHP_SELF?>" method="GET">
sorry, I can't comment, but could you just change:
echo "<h3>...Something...";
to
echo $sql;
or
var_dump($sql)
and compare value of $sql when you use $_GET and use "actual numbers"
there must be difference :-) ;-)
I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>
I have created the dropdown list in my index page i want to select the one value from that list and validate it if not selected any value the code for that is as follows:
<form action="" method="post">
<select value="state" name="state">
<option selected="">---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php code for the above file is as follows:
<?php
if(!empty($_POST['state'])) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
I dont want to be select the first option in selection list please enter to be selected but the code which I have used is taking that value also I want relevant code how to validate that list?
I prefer adding a disabled attribute to the placeholder option, that way, a user has to choose something if they click the drop-down:
<select value="state" name="state">
<option selected disabled>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
Quick demo: http://jsfiddle.net/hxxJZ/
something like this should to the trick:
<form action="" method="post">
<select name="state">
<option value="0" selected>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php
<?php
if( 0 != $_POST['state'] ) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
your HTML is not right,
try edit this line
<option selected=""...
to this one
<option selected value="">---
select elements do not have an (HTML) value attribute, so remove value="state".
The selected attribute should be selected="selected" (or just SELECTED).