I would like to pass php parameters to an html select options tag. it works fine for an input tag using the value attribute as follows:
<input type="text" name="to" value= "<?php echo $parameter ?>" size=10>
I tried to do the same for the select tag, but some reason its not working. Below is the select tag:
<select name="clinic">
<option value="<?php echo $clinicName; ?>" >Baylor</option>
<option value="<?php echo $clinicName; ?>" >IDCC</option>
</select>
Please note that the parameter would need to passed to an sql statement below these above statements as follows:
$clinicName = $_POST['clinic'];
$sql = "select * from patients where clinic = '$clinicName'";
Should do the trick
<select name="clinic">
<option <?php echo ($clinicName=="Baylor" ? "selected" : ""; ?> value="Baylor">Baylor</option>
<option <?php echo ($clinicName=="IDCC" ? "selected" : ""; ?> value="IDCC">IDCC</option>
</select>
Also, as someone above me mentioned in the comments, you need to read up on SQLi otherwise your site is just one more to the list of already-hacked.
Are you trying to set the current selected value on the <select>? If so, you need to use if statements to check which one to mark as selected:
<option value="Baylor"<?php if($clinicName == 'Baylor') echo ' "selected"'?>>Baylor</option>
<option value="IDCC"<?php if($clinicName == 'IDCC') echo ' "selected"'?>>IDCC</option>
Instead of the if, you could also use the ternary operator:
<option value="Baylor"<?php echo $clinicName == 'Baylor' ? ' "selected"' : ''?>>Baylor</option>
<option value="IDCC"<?php echo $clinicName == 'IDCC') ? ' "selected"' : ''?>>IDCC</option>
First of all, please sanitize your SQL query. Right now it is more exploitable than Lindsay Lohan. If you use mysql_* functions, then write it as:
$sql = "select * from patients where clinic = '".mysql_real_escape_string($clinicName)."'";
Secondly, in your HTML the $clinicName is the same on both cases. So the $_POST['clinic'] will always be the same no matter what. If you want to pre-select a value, then you need to write it as:
<select name="clinic">
<option value="Baylor" <?php if($clinicName=='Baylor'){ echo 'selected'; } ?>>Baylor</option>
<option value="IDCC" <?php if($clinicName=='IDCC'){ echo 'selected'; } ?>>IDCC</option>
</select>
Other than that, I remain a bit confused about your question.
Copy this in your HTML tag
<select name="clinic">
<option value="baylor" >Baylor</option>
<option value="IDCC" >IDCC</option></select>
And this before your HTML tag
<?php
echo $clinicName = $_POST['clinic'];
?>
Related
This question already has an answer here:
Select option default based on database variable [duplicate]
(1 answer)
Closed 7 years ago.
I'm beginner in PHP MySQL. I successfully get the value from database in INPUT type but I can't get the data from database in SELECT type:
Here is my sample edit form where Gender & User Type can't output the value from my database:
and here is data from the table I created:
I'm calling the data in my input box by using this code:
<?php $userRow['agentFname']; ?> //$userRow['ROW NAME IN TABLE'];
But I can't call the data where In select box, like Gender: and User Type here is my code of Select box.
<select class="form-control" name="aGender" >
<option selected disabled>*Gender</option>
<option>Male</option>
<option>Female</option>
</select>
<select class="form-control" name="utype" >
<option selected disabled>*User Type</option>
<option>Agent</option>
<option>Team Leader</option>
<option>Admin</option>
</select>
#Edmhar has a good answer, but it is hard coded and isn't good if you want to add more genders (transgenders and what not).
What I do is something like this:
<?php
$array = array("male", "female", "other");
echo "<select class='form-control' name='aGender' >";
foreach ($array as $gender) {
if ($gender == $databaseValue) {
echo "<option selected>$gender</option>";
} else {
echo "<option>$gender</option>";
}
}
echo "</select>";
?>
Also, don't use disabled on form elements; use read-only. It does the same thing as disabled visually, but disabled does what it says. It blocks the value from being submitted to the database. read-only just prevents editing, but doesn't cause form submission problems. User type will follow the same suit.
I already have my own answer to this
Here how I solved this:
<select class="form-control" name="aGender" >
<?php
if ($userRow['agentGender'] == 'Male') {
echo "<option selected>Male</option>";
echo "<option>Female</option>";
} else {
echo "<option selected>Female</option>";
echo "<option>Male</option>";
}
?>
</select>
I think you can use below conditional code for this issue:
<?php if($userRow['aGender'] == 'Male'){
$selectedMale = 'selected="selected"'
$selectedFemale = ''
}else{
$selectedFemale = 'selected="selected"'
$selectedMale = ''
}?>
<select class="form-control" name="aGender" >
<option selected disabled>*Gender</option>
<option <?php echo $selectedMale ;?>>Male</option>
<option <?php echo $selectedFemale;?>>Female</option>
</select>
and same for the another select box.
It will work hopefully.
In my opinion it's best to use ternary operator in such a case, combined with options element fetched from db/array. Something like:
<?php $options = Array(1 => "Option 1", 2 => "Option 2")
<select>
<?php foreach($options as $key => $opt): ?>
<option value="<?= $key ?>" ($key == $userRow['value'] ? "selected" : "" )><?= $opt ?></option>
<?php endforeach; ?>
</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 an HTML form and I have a select menu. I would like that according to the value chosen by the user this would be the same value that is selected on HTML form. For example if the user chooses HIDE - the value in the select will be HIDE, if the user chooses SHOW - the value would be SHOW.
I have managed to change the value in the SQL table using PDO but I haven't managed to display the selected option based on the SQL table. Regardless of what is being saved in the table - the value being displayed in the HTML form is always show.
<select id="address_privacy" name="address_privacy" tabindex="auto">
<option "<?php if($result['address_privacy'] == 'SHOW') { echo 'selected="selected"';} ?>" value="SHOW">Show Physical Location</option>
<option "<?php if($result['address_privacy'] == 'HIDE') { echo 'selected="selected"';} ?>" value="HIDE">Hide Physical Location</option>
</select>
The above is waht I tried till now.
Try this
<select id="address_privacy" name="address_privacy" tabindex="auto">
<option <?php echo $result['address_privacy'] == 'SHOW' ? 'selected="selected"' : ''?>
value="SHOW">Show Physical Location
</option>
<option <?php echo $result['address_privacy'] == 'HIDE' ? 'selected="selected"' : '' ?>
value="HIDE">Hide Physical Location
</option>
</select>
You could do something like this:
<select id="address_privacy" name="address_privacy" tabindex="auto">
<?php if($result['address_privacy'] == 'SHOW'):?>
<option <?php { echo 'selected="selected"';} ?> value="SHOW">Show Physical Location</option>
<?php else : ?>
<option { echo 'selected="selected"';} ?> value="HIDE">Hide Physical Location</option>
<?php endif;?>
</select>
Regardless of HOW you accomplish this, you need to use == for the comparison, not =. And make sure you remove the " around the <?php ?> sections of your code.
I have a form which POSTs to itselft so the user can do things that you would find in a Shopping Cart.
e.g. Increase quantity, select postage type.
My problem is for my form Select element called "postage" whenever the form reloads itself , it forgets what was selected.
All my other fields remember their values using this:
<input type="text" name="postcode" value="<?php echo $_POST['postcode']; ?> " />
How do I use the $_POST value to automatically select the option in the select field that was done by the user?
I tried this:
<select name="postage" selected="<?php echo $_POST['postage']; ?>" >
and this
<select name="postage" value="<?php echo $_POST['postage']; ?>" >
Thanks
You almost got it. You need to set the attribute selected="selected" (the exact form you need technically depends on your HTML doctype, but this is a safe default) on the <option> element if and only if the value of $postage equals the value of the element. So:
<select name="postage">
<option value="foo"
<?php if ($_POST['postage'] == "foo") echo 'selected="selected" '; ?>
>
</select>
Note that this violates the DRY principle because you now have two occurrences of the string "foo" in there, so it's a prime candidate for refactoring. A good approach would be to keep value/text pairs in an array and iterate over it with foreach to produce the <option> tags.
You need to foreach through all the options.
Make an array with all the dropdown options, loop through that, and compare with what is stored in post.
E.G.:
<?php
$aDropd = array("apple","orange","banana");
echo "<select>";
foreach($aDropd as $sOption){
$sSel = ($sOption == $_POST['postage'])? "Selected='selected'":"";
echo "<option $sSel>$sOption</option>";
}
echo "</select>";
no its not working at all..you need to put some kind of loop for that.
For Example : foreach($record => $values){
if($values == $_POST['postage']){
$selected = "selected='selected' ";
}else{
$selected = "";
}
}
<input name="postage" value="1" <?=$selected?> >
EDITED:
if($_POST['postage'] == 1){
$selected1 = "selected='selected' ";
}else if($_POST['postage'] == 2){
$selected2 = "selected='selected' ";
} and so on..........
<select name="postage">
<option value="1" <?=$selected1;?> />
<option value="2" <?=$selected2;?> />
</select>
I think this may be helpful to you..you can ask me if anything else needed...
Thanks.
The value of selected should be selected
<select name="postage" value="1" <?php echo (($_POST['postage'] == 1)?'selected="selected"':''); ?> >
Your html syntax is wrong. The correct way to write the html is like this:
<select>
<option value ="<?php echo $_POST['postage']; ?>" selected="selected"></option>
</select>
You can also make it shorter:
<select>
<option value ="<?=$_POST['postage']; ?>" selected="selected"></option>
</select>
<select name="foo">
<option value="1" <?php echo strcmp($_POST['foo'],"1")==0?"selected=\"selected\"":"" ?>>option1</option>
<option value="2" <?php echo strcmp($_POST['foo'],"2")==0?"selected=\"selected\"":"" ?>>option2</option>
How can I auto select a field in dropdown.
Say if someone goes to www.xyx/form/?abc
Some value gets selected in the dropdown,
Or if someone goes to www.xyx/form/?def
Some other value gets selected in the dropdown.
I am comfortable with JS and php.
assuming example.com/?sel=xxx
<?php
$sel = $_GET['sel'];
?>
<select ...>
<option val="xxx" <?php if($sel==='xxx') echo 'selected="selected"';?>>Option XXX</option>
<option val="yyy" <?php if($sel==='yyy') echo 'selected="selected"';?>>Option YYY</option>
</select>
No Javascript needed.
PHP
<select name="select">
<option value="abc"<?php ($_GET['select'] == 'abc'? echo 'selected="selected"' : ''); ?>>ABC</option>
<option value="def"<?php ($_GET['select'] == 'def'? echo 'selected="selected"' : ''); ?>>DEF</option>
</select>
<option value="abc" <?php echo isset($_GET['abc']) ? 'selected="selected"' : ''; ?>>abc</option>
hmmm, so what will you do when you have 100s of items in the Options list? The other ideas wont look so great then.
Then you will need to just simply write 1 line of code at the end of select tag:
<?php if(isset($_POST['env_foil_color'])) echo "<script>document.getElementById('env_foil_color').value='{$_POST['env_foil_color']}';</script>"; ?>
where, 'env_foil_color' is the select tag's ID and Name both