PHP doesn't take selected option value on post - php

Well i have a serious problem on updating a form, it only takes the selected value on user action in the select option menu but when the user updates the data without changing the select value it returns the default database value which is set to 0
<select class="selectpicker" name="socio_demo_type" id="socio_demo_type">
<?php
$query_sd = "SELECT * FROM insertion WHERE id_insertion = ".$insertion["id"];
$result_sd = mysql_query($query_sd);
$type = mysql_fetch_array($result_sd);
$val = $type["ciblage_socio_demo_type"];
?>
<option value="0" <?php if($val == 0) echo 'selected' ?> >Pack affinitaire </option>
<option value="1" <?php if($val == 1) echo 'selected' ?> >Achat ciblé (loggué)`enter code here`</option>
</select>

Pass the connection sting as well and use my sqli statements.
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
<select class="selectpicker" name="socio_demo_type" id="socio_demo_type">
<?php
$query_sd = "SELECT * FROM insertion WHERE id_insertion = "$insertion["id"];
$result_sd = mysqli_query($con , $query_sd);
$type = mysqli_fetch_array($result_sd);
$val = $type["ciblage_socio_demo_type"];
?>
<option value="0" <?php if($val == 0) echo 'selected' ?> > Option2 </option>
<option value="1" <?php if($val == 1) echo 'selected' ?> > Option1 </option>
</select>

Related

Show specific information from mysql to dropdownbox in php html.

These is my code to get the information from the database. So status is the one where in my database would be either Interested or Not Interested.
while($row= mysqli_fetch_array($result))
{
$ID =$row['Particulars_ID'];
$name = $row['Name'];
$number =$row['Number'];
$status =$row['Status'];
$remarks =$row['Remarks'];
}
I would like to echo out the value Not Interested if my database shows that this person is not interested. However from my below code, it always show Interested no matter which person i click on.
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
first - you do not need the comma in the select, second - ensure that this is the only element with the id of status, third - simply check the $status value in each option and echo selected if it is.
echo "<select name = 'status' id = 'status'>
<option value='Interested'";
if($status == "Interested"){echo " selected";}
echo">Interested</option>
<option value='Not Interested' ";
if($status == "Not Interested"){echo " selected";}
echo">Not Interested</option>
</select><br>";
since the default is the first option Interested, then if ($status === "Not Interested") set the option attribute selected
<?php
if ($status === "Not Interested") $selected = "selected";
else $selected = "";
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested' $selected>Not Interested</option>
</select><br>";
Change this
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
to this
// Set the selected attributes based on the value of status
$interested = ($status === 'Interested') ? ' selected' : '';
$notInterested = ($interested === '') ? ' selected' : '';
echo <<< SELECT
<select name="status" id="status">
<option$interested>Interested</option>
<option$notInterested>Not Interested</option>
</select>
SELECT;
You don't need the value attribute if the value is the same as the text.
If there were more than two options, I recommend a loop like this:
<?php foreach ($options as $o) : ?>
<option<?php if ($o === $optionValue) ?> selected<?php endif ?>><?= optionValue ?></option>
<?php endforeach ?>

How to get value from select in which there is preselected option in PHP

I am using this code to show the dropdown to the user. When the form is submitted I am using $_POST['newproduct'] to get the value. Variable $isNewProduct can be 0 or 1 only.
index.php
<?php
require "db.php";
$link = mysqli_connect($_srvr, $_user, $_pass, $_db);
$query = "SELECT `new_product` FROM `products` WHERE `id`=1";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
$isNewProduct = $row[0];
mysqli_close($link);
?>
<form action="update.php" method="post">
<select name="newproduct">
<option value="0" <?php echo $isNewProduct ? "" : "selected" ; ?> >No</option>
<option value="1" <?php echo $isNewProduct ? "selected" : "" ; ?> >Yes</option>
</select>
<input type="submit" value="Update" name="update_info_btn" />
</form>
update.php
if( isset($_POST['update_info_btn']) ) {
$new_product = $_POST['newproduct'];
// query to update the row with $new_product in DB.
}
Now if $isNewProduct = 1 then option 'YES' is preselected, when the option is changed to 'NO' and form is submitted, $_POST['newproduct'] always gives 1.
Likewise, I have one more dropdown in which I have around 20 options and one is preselected when the form is submitted after changing the option, it always gives that preselected value.
Replace your code with the following code...
<?php
$newproduct='';
if(isset($_POST['newproduct'])
{
$newproduct=$_POST['newproduct'];
}
?>
<select name="newproduct">
<option value="0" <?php if($newproduct ==0){echo 'selected="selected"';} ?> >No</option>
<option value="1" <?php if($newproduct ==1){echo 'selected="selected"';} ?> >Yes</option>
</select>

selected value get from db into dropdown select box option using php mysql error

I need to get selected value from db into select box. please, tell me how to do it. Here is the code.
Note: 'options' value depends on the category.
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
</select>
<?php
}
?>
I think you are looking for below code changes:
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
<option value="ASP" <?php if($options=="ASP") echo 'selected="selected"'; ?> >ASP</option>
</select>
The easiest way I can think of is the following:
<?php
$selection = array('PHP', 'ASP');
echo '<select>
<option value="0">Please Select Option</option>';
foreach ($selection as $selection) {
$selected = ($options == $selection) ? "selected" : "";
echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>';
}
echo '</select>';
The code basically places all of your options in an array which are called upon in the foreach loop. The loop checks to see if your $options variable matches the current selection it's on, if it's a match then $selected will = selected, if not then it is set as blank. Finally the option tag is returned containing the selection from the array and if that particular selection is equal to your $options variable, it's set as the selected option.
for example ..and please use mysqli() next time because mysql() is deprecated.
<?php
$select="select * from tbl_assign where id='".$_GET['uid']."'";
$q=mysql_query($select) or die($select);
$row=mysql_fetch_array($q);
?>
<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php $s="select * from tbl_new_user where type='client'";
$q=mysql_query($s) or die($s);
while($rw=mysql_fetch_array($q))
{ ?>
<option value="<?php echo $rw['login_name']; ?>"<?php if($row['clientname']==$rw['login_name']) echo 'selected="selected"'; ?>><?php echo $rw['login_name']; ?></option>
<?php } ?>
</select>
Just Add an extra hidden option and print selected value from database
<option value="<?php echo $options;?>" hidden><?php echo $options;?></option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
Select value from drop down.
<select class="form-control" name="category" id="sel1">
<?php foreach($data as $key =>$value) { ?>
<option value="<?php echo $data[$key]->name; ?>"<?php if($id_name[0]->p_name==$data[$key]->name) echo 'selected="selected"'; ?>><?php echo $data[$key]->name; ?></option>
<?php } ?>
</select>
THE EASIEST SOLUTION
It will add an extra in your options but your problem will be solved.
<?php
if ($editing == Yes) {
echo "<option value=\".$MyValue.\" SELECTED>".$MyValue."</option>";
}
?>
$option = $result['semester'];
<option >Select</option>
<option value="1st" <?php if($option == "1st") echo 'selected = "selected"'; ?>>1st</option>
<option value="2nd" <?php if($option == "2nd") echo 'selected = "selected"'; ?>>2nd</option>
<option value="3rd" <?php if($option == "3rd") echo 'selected = "selected"'; ?>>3rd</option>
<option value="4th" <?php if($option == "4th") echo 'selected = "selected"'; ?>>4th</option>
<option value="5th" <?php if($option == "5th") echo 'selected = "selected"'; ?>>5th</option>
<option value="6th" <?php if($option == "6th") echo 'selected = "selected"'; ?>>6th</option>
<option value="7th" <?php if($option == "7th") echo 'selected = "selected"'; ?>>7th</option>
<option value="8th" <?php if($option == "8th") echo 'selected = "selected"'; ?>>8th</option>
</select>
BEST code and simple
<select id="example-getting-started" multiple="multiple" name="category">
<?php
$query = "select * from mine";
$results = mysql_query($query);
while ($rows = mysql_fetch_assoc(#$results)){
?>
<option value="<?php echo $rows['category'];?>"><?php echo $rows['category'];?></option>
<?php
}
?>
</select>
You can also do like this ....
<?php $countryname = $all_meta_for_user['country']; ?>
<select id="mycountry" name="country" class="user">
<?php $myrows = $wpdb->get_results( "SELECT * FROM wp_countries order by country_name" );
foreach($myrows as $rows){
if( $countryname == $rows->id ){
echo "<option selected = 'selected' value='".$rows->id."'>".$rows->country_name."</option>";
} else{
echo "<option value='".$rows->id."'>".$rows->country_name."</option>";
}
}
?>
</select>
Answer is simple.
when u pass value from dropdown.
Just use as if else.
for eg:
foreach($result as $row) {
$GLOBALS['output'] .='<option value="'.$row["dropdownid"].'"'.
($GLOBALS['passselectedvalueid']==$row["dropwdownid"] ? ' Selected' : '').'
>'.$row['valueetc'].'</option>';
}
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php echo $options == 'PHP' ? 'selected' : ''; ?> >PHP</option>
<option value="ASP" <?php echo $options == 'ASP' ? 'selected' : ''; ?> >ASP</option>
</select>
<?php
}
?>
USING PDO
<?php
$username = "root";
$password = "";
$db = "db_name";
$dns = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
$conn = new PDO($dns,$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "select * from mine where username = ? ";
$stmt1 = $conn->prepare($sql);
$stmt1->execute(array($_POST['user']));
$all = $stmt1->fetchAll(); ?>
<div class="controls">
<select data-rel="chosen" name="degree_id" id="selectError">
<?php
foreach($all as $nt) {
echo "<option value =$nt[id]>$nt[name]</option>";
}
?>
</select>
</div>
I'm using eval() PHP function like this:
My PHP code:
$selOps1 = $selOps2 = $selOps3 = '';
eval('$selOps'. $dbRow["DBitem"] . ' = "selected";');
Then in my select box I use it like this:
<select>
<option <?=$selOps1?> value="1">big</option>
<option <?=$selOps2?> value="2">Middle</option>
<option <?=$selOps3?> value="3">Small</option>
</select>
Put value from db into a variable and check like following code example
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
This may help you.
?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res))
{
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
// Assuming $list['options'] is a coma seperated options string
$arr=explode(",",$list['options']);
<?php foreach ($arr as $value) { ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } >
</select>
<?php
}
?>

assign selected option value from database

I have a form which contains a dropdown list of countries generated from database. The values are stored in database. There is an option in which user can view or update the valuesinserted. For updating all the form values get fetched from database. What i require is that when form is loaded for updating the selected option of the country dropdown must be that stored in the database.
For eg: if from the following dropdown option2 is selected and inserted into database.
Dropdown: |option1|<selected>
|option2|
|option3|
during update it should be like this
Dropdown: |option1|
|option2|<selected>
|option3|
Here is the code i tried.
$selected = $list["country_country_name"];
<tr><td>Country</td><td><select onchange="getCountry(this.value);" name="country" id="country" ><?php foreach( $query as $qry ) {
print '<option value="'.$qry["country_country_name"].'"';
if( $qry["country_country_name"] == $selected ) print'selected';
print '>'.$qry["country_country_name"].'</option>'."\n";} ?>
</select></td></tr>
<select id="list" name="list">
<option value=""> Please Select </option>
<?
$list = array('1',
'2',
'3',
'4',
'5',
'6');
while ($L = array_shift($list)) {
?>
<option value="<?=$L?>" <? if($selected == $L){ echo 'selected="selected"'; }?> >
<?= $L ?>
</option>
<?
}
?>
</select>
you can simple get the selected option with this:
$("#list").val();
try please.
$selected = $list["country_country_name"];
<tr><td>Country</td>
<td>
<select onchange="getCountry(this.value);" name="country" id="country" >
<?php foreach( $query as $qry ) {
$sel = '';
if( $qry["country_country_name"] == $selected )
$sel = 'selected="selected"';
echo '<option value="'.$qry["country_country_name"].'" '.$sel.'>'.$qry["country_country_name"].'</option>'."\n";
} ?>
</select>
<?php echo form_error('country'); ?>
</td>
</tr>
When your option tag's value attribute contains the same value as the option's text, the value attribute is not necessary -- so omit it.
Below shows an inline condition statement.
<tr>
<td>Country</td>
<td><select onchange="getCountry(this.value);" name="country" id="country">
<?php
foreach ($query as $row) {
echo "<option" ,
($row["country_country_name"] == $list['country_country_name'] ? ' selected' : '') ,
"{$row['country_country_name']}</option>\n";
}
?>
</select></td>
</tr>

PHP while in edit mode show selected value in to drop down

This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>

Categories