I have built a form that has text fields, text areas, radio buttons, and drop downs, and I've created a query to pull the data from the database at the appropriate row, but can't figure out how to program the drop down to reflect the choice that's in the mysql row. Here are a sample of the fields I've been able to program correctly:
<label for="contactname">Contact Name:</label>
<input type="text" name="contactname" value="<?php echo $query['contactname']; ?>"
<label for="delay">Delay:</label>
<input id="Yes" type="radio" name="delay" value="Y" <?php if($query['delay']==='Y') echo 'checked'; ?> >Yes
<input id="No" type="radio" name="delay" value="N" <?php if($query['delay']==='N') echo 'checked'; ?> >No
<label for="notes">Notes: </label>
<textarea name="notes" id="notes"><?php echo $query['notes']; ?></textarea>
These have all worked in pulling data from the mysql database correctly, but I'm trying to figure out how to program the drop downs so that when this form is pulled, the selection populates/displays the selected value when the form is pulled:
<label for="day">Select Option:</label>
<select name="day">
<option value="0">Monday </option>
<option value="1">Tuesday </option>
<option value="2">Wednesday </option>
<option value="3">Thursday </option>
<option value="4">Friday </option>
</select>
Thanks for your help!
You need to append " selected" to the option you want pre-selected in the dropdown.
<label for="day">Select Option:</label>
<select name="day">
<option value="0"<?php if ($query['day'] == 0) echo ' selected="selected"'; ?>>Monday</option>
<option value="1"<?php if ($query['day'] == 1) echo ' selected="selected"'; ?>>Tuesday</option>
<option value="2"<?php if ($query['day'] == 2) echo ' selected="selected"'; ?>>Wednesday </option>
<option value="3"<?php if ($query['day'] == 3) echo ' selected="selected"'; ?>>Thursday</option>
<option value="4"<?php if ($query['day'] == 4) echo ' selected="selected"'; ?>>Friday</option>
</select>
try this example:
//array as an example
$your_db_array = array ('value1','value2','value3');
// Make the pull-down menu:
echo '<select name="pull_down">';
foreach ($your_db_array as $key => $value) {
echo "<option value=\"$key\"";
// Check for stickyness:
if (isset($value_from_DB['value']) && ($value_from_DB['value'] == $key) ){
echo ' selected="selected"';}
echo ">$value</option></select>";
}
Related
I am currently trying to set the default value of a element in HTML with data from an SQL database. I have loaded the values from the database into variables. This is what I have done so far:
<div class="form-group">
Gender: <select class="form-control" name="gender" value="<?php echo $user['gender'] ?>">
<option value="female">Female</option>
<option value="male">Male</option>
<option value="none" selected>Prefer not to say</option>
</select>
</div>
However when I load the page, the default value is "Prefer not to say" when the value stored in $user['gender'] is "male".
You need to add the selected attribute to the option that matches what was in $user['gender']
<div class="form-group">
Gender:
<select class="form-control" name="gender">
<option value="female" <?php echo $user['gender'] == 'female' ? ' selected ' : '';?>>Female</option>
<option value="male" <?php echo $user['gender'] == 'male' ? ' selected ' : '';?>>Male</option>
<option value="none" <?php echo $user['gender'] == 'none' ? ' selected ' : '';?>>Prefer not to say</option>
</select>
</div>
Which is a shorter way of saying
<div class="form-group">
Gender:
<select class="form-control" name="gender">
<?php
if ($user['gender'] == 'female' ) {
?>
<option value="female" selected>Female</option>
<option value="male">Male</option>
<option value="none">Prefer not to say</option>
<?php
}
if ($user['gender'] == 'male' ) {
?>
<option value="female">Female</option>
<option value="male" selected>Male</option>
<option value="none">Prefer not to say</option>
<?php
}
if ($user['gender'] == 'none') {
?>
<option value="female">Female</option>
<option value="male">Male</option>
<option value="none" selected>Prefer not to say</option>
<?php
}
?>
</select>
</div>
I have HTML form which have select options like below
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<?php if ($row[occupation] == 0){
}?>
<option value="0">Parent</option>
<option value="1">Teacher</option>
</select>
</div>
I want
option selected called Parent if ($row[occupation] == 0) else want
show Teacher as selected
. I am new in PHP and does not know how I can I do it. Let me know if someone can help me for do it.
Thanks
You have to echo selected='selected' based on the value of $row['occupation'], like this:
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<option value="0"<?php if($row['occupation'] == 0){ echo " selected='selected'"; } ?>>Parent</option>
<option value="1"<?php if($row['occupation'] == 1){ echo " selected='selected'"; } ?>>Teacher</option>
</select>
</div>
You can use this one.
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name="occupation" required>
<option value="0" <?php if ($row['occupation'] == 0) { echo 'selected'; } ?>>Parent</option>
<option value="1" <?php if ($row['occupation'] == 1) { echo 'selected'; } ?>>Teacher</option>
</select>
</div>
I am new in programming i just want that the value i choose in drop downs must retain selected when i click the search button ...
here is the code
<select name="campaigntype" id="campaigntype" class="text dropDownNewsletter" onchange="GetCampaigns()" style="float:left; margin-right:15px;">
<option value="">Select Campaign</option>
<option value="1" >OB Campaign</option>
<option value="2">Casbo Campaign</option>
</select>
<div id="selected_campaigns" style="float:left">
<select name="campaignid" id="campaignid" class="text dropDownNewsletter">
<option value="">Select Campaign</option>
<?php foreach($campaigns as $key=>$row){?>
<option value="<?php echo $row['id'];?>" <?php if( $row['id'] == $campaign_id) {?> selected="selected" <?php } ?> ><?php echo $row['id'].'-'.$row['title'];?></option>
<?php }?>
</select>
I want to update my form when I click edit button then all info is showing correctly but status value is showing all time same open option. I dont know why it is showing same open status and my currently status is done but it is showing all time open please help me to fix this issue thanks
this is my form code username is showing correctly but status is not showing correct
<p><label class="field" for="username">UserName:</label>
<input name="username" type="text" id="username" value="<?php echo $username; ?>" size="50" />
</p>
<p>
<label class="field" for="Status">Status</label>
<select name="status" id="status" value="<?php echo $status; ?>" >
<option value="open">Open</option>
<option value="done">Done</option>
<option value="pending">Pending</option>
<option value="working">Working</option>
</select>
</p>
Use selected attribute.
<select name="status" id="status">
<option value="open" <?php if($status=="open") { echo "selected"; } ?> >Open</option>
<option value="done" <?php if($status=="done") { echo "selected"; } ?> >Done</option>
<option value="pending" <?php if($status=="pending") { echo "selected"; } ?> >Pending</option>
<option value="working" <?php if($status=="working") { echo "selected"; } ?> >Working</option>
</select>
This is wrong way, correct way is to use like this,
<select name="status">
<?php
$options = array("open","done","pending","working");
$selected = "done";
foreach($options as $option){
if($selected==$option){
echo '<option value="'.$option.'" selected="selected">'.ucfirst($option).'</option>';
}else{
echo '<option value="'.$option.'">'.ucfirst($option).'</option>';
}
}
?>
</select>
If you really want to embed the status in the options tag, you should use the 'selected' attribute for selection of the tag options. Here is your modified code with correct handling:-
<p><label class="field" for="username">UserName:</label>
<input name="username" type="text" id="username" value="<?php echo $username;?>" size="50" />
</p>
<p>
<label class="field" for="Status">Status</label>
<select name="status" id="status" >
<option value="open" <?php echo $status == 'open' ? 'selected' : ''; ?>>Open</option>
<option value="done" <?php echo $status == 'done' ? 'selected' : '' ;?>>Done</option>
<option value="pending" <?php echo $status == 'pending' ? 'selected' : '' ; ?>>Pending</option>
<option value="working" <?php echo $status == 'working' ? 'selected' : '' ; ?>>Working</option>
</select>
</p>
You need selected tag. Modify your form like this
<select name="status" id="status"> <!-- value removed, there's no use of it here -->
<option value="open" <?php if ($status=="open") {echo "selected"}?>>Open</option>
<option value="done" <?php if ($status=="done") {echo "selected"}?>>Done</option>
....
</select>
Use selected attribute for option.
Change your code as
<select name="status" id="status">
<option value="open" <?php selected($status, 'open'); ?>>Open</option>
<option value="done" <?php selected($status, 'done'); ?>>Done</option>
<option value="pending" <?php selected($status, 'pending'); ?>>Pending</option>
<option value="working" <?php selected($status, 'working'); ?>>Working</option>
</select>
<?php
function selected( $selected_value, $value ) {
if( $selected_value === $value ) {
echo 'selected=true';
}
}
?>
how do i add validation on drop down select menu i have code and i'm working on but problem is that is not working nothing display error message. I want to be display error message if drop down selection values shouldn't be selected before click on add to cart button?
if(isset($_REQUEST['check'])){
if (isset($_POST['size']) && $_POST['size'] == '' ) {
echo 'Error, select category';
exit;
}}
<form action="" id="size1" name="size1" method="post">
<select style="background-color:#CCC"
name="size" id="size" onchange="this.form.submit();" >
<option value="">Select</option>
<option <?php if ($_POST['size'] == 'Small')
print 'selected '; ?> value="Small">Small</option>
<option <?php if ($_POST['size'] == 'Medium')
print 'selected '; ?> value="Medium">Medium</option>
<option <?php if ($_POST['size'] == 'Large')
print 'selected '; ?> value="Large">Large</option>
<option <?php if ($_POST['size'] == 'XL')
print 'selected '; ?> value="XL">XL</option>
</select>
<input type="button" class="button1" name="check"
value="Add To Cart" onclick="addtocart(<?php echo $row3['id'];?>);" />
</form
If I get your question right, you wish to validate before the form gets submitted? If so, then you need to use client side validation:
http://docs.jquery.com/Plugins/validation#Example
make u smal change in html here
<option value="0">Select</option>
and function like this to check
function check_selected_or_not(){
var check_selected = jQuery('#size').val();
if (check_selected == 0){
alert('Please select');
}else{
alert('sucess');
}
}
Please try it's work
it should be like this
<?php
if($_POST['country']=="0")
{
echo 'Error, select category';
//exit;
}
?>
<form action="" id="size1" name="size1" method="post">
<select style="background-color:#CCC"
name="size" id="size" onchange="this.form.submit();" >
<option value="0">Select</option>
<option <?php if ($_POST['size'] == 'Small')
print 'selected '; ?> value="Small">Small</option>
<option <?php if ($_POST['size'] == 'Medium')
print 'selected '; ?> value="Medium">Medium</option>
<option <?php if ($_POST['size'] == 'Large')
print 'selected '; ?> value="Large">Large</option>
<option <?php if ($_POST['size'] == 'XL')
print 'selected '; ?> value="XL">XL</option>
</select>
<input type="button" class="button1" name="check"
value="Add To Cart" onclick="addtocart(<?php echo $row3['id'];?>);" />
</form>