update selected list box value in php - php

I have select option box and also add many field i want to update through submit button.
Here is code:
<td><select name="select_provider" id="select_provider">
<option value="selected">All</option>
<?php $sql_list_provider=mysql_query("Select opp_provider from ambition_opp_provider");
while($cmd=mysql_fetch_array($sql_list_provider)){
?>
<option><?php echo $cmd['opp_provider'];}?></option>
</select></td>
<td><input type="submit" name="update_provider" id="update_provider" value="UPDATE PROVIDER" style="background-color:#000000; color:#fff"/></td>
Thanks for advance.

In your code </option> is outside the loop that causes to generate invalid html. Simply remove } from and add <?php } ?> after </option>
And add value attribute to option tag. value="<?php echo $cmd['opp_provider'];?>" and in server-side, you'll be able to access it like: $_POST['select_provider'] (assume you use post method).

Related

Change textbox value using dropdown selected in php and mysql

I have texbox and dropdown list which is populated from mysql database.I want to change textbox value using dropdown list, without refreshing the page.
Here is my code and Thanks in Advance.
<select name="select" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php
require("dbcon.php");
$getallcompanies = mysql_query("SELECT * FROM ifcandetails6");
while($viewallcompanies = mysql_fetch_array($getallcompanies)){
?>
<option id="<?php echo $viewallcompanies['tcuid']; ?>"><?php echo $viewallcompanies['tcname'] ?></option>
<?php
}
?>
</select>
Here is my Input field code:
<input type="text" id="field1" value="<?php echo $viewallcompanies['tcname']?>" disabled/>
Use following
$(document).ready(function(){
$("#dropdownlist1").change(function(){
$("#field1").val($(this).val());
});
});
As you can do it on front side itself, you dont need to change in your PHP code. Add the following code on DOM ready.

picking selected value from dropdown on page refresh

I am creating my form and adding error handling.
When the page is refreshed i want to be able to select the previous selected value in the drop down menu but i am struggling to get this to work.
Is anyone able to help
<select value="<? echo $_POST["Bookie"][$i]?>" style="width:100px;" id="Bookie[]" name="Bookie[]">
<option>Bet365</option>
<option>Betbright</option>
<option>Betfair</option>
<option>Betfred</option>
<option>BetVictor</option>
<option>Boylesports</option>
<option>Bwin</option>
<option>Centrebet</option>
<option>Coral</option>
<option>Ladbrokes</option>
<option>Paddy Power</option>
<option>Pinnacle Sports</option>
<option>SBOBET</option>
<option>Sky Bet</option>
<option>Stan James</option>
<option>unibet</option>
<option>William Hill</option>
</select>
You have to repeat this for each <option> tag, changing Paddy Power as appropriate:
<option<?php if(!empty($_POST) && $_POST['Bookie'][$i] == 'Paddy Power') echo ' selected="selected"';?>>Paddy Power</option>
If your <form> tag has method="post", you'll be fine with the above snippet. Otherwise you'll need to change $_POST in the above to $_GET.
Also, remove value="<? echo $_POST["Bookie"][$i]?>" from your <select> tag as that attribute is not supported there. The selected attribute is for setting the selected option in a <select> list.
just use $_SESSION['convert']:
The first time you open the page you will check if session exists:
session_start();
$convert = isset($_SESSION['convert'])?$_SESSION['convert']:"Bet365";
So, when you will write your dropdown like:
<form>
<select>
<option value="Bet365" <?php echo $convert=='Betfair'?'selected':''?>>Bet365</option>
<option value="Coral" <?php echo $convert=='Coral'?'selected':''?>>Coral</option>
</select>
</form>
then on form submit you will take the actual convert selected and save it in session:
session_start();
$convert = $_POST['convert'];
$_SESSION['convert] = $convert;
last selected option after refresh the page

select box value changes on form reloading with php validation

I need a solution for this. Here I want to validate form elements. I did it with PHP but what my problem is on clicking submit button validation process takes place but select box value changes to select dept.
FOR EXAMPLE, if you just click submit button without giving student id by only select dept.. the message shows enter studentid but select box value changes to select dept again as it page reloads first..
php:
if($_POST['submit']!='')
{
$c=0;
if($_POST['studid']=='' ) {
$msg_id="Enter stud id";
$c++;
}
if($_POST[studdept]=='') {
$msg_dept="Enter stud dept";
$c++;
}
if($c==0) {
echo "form has been submitted..";
}
}
HTML:
<form id="myform" action="" method="post">
<table>
<tr>
<td> Studid: *</td> <td> <input type="text" name="studid" maxlength="10" value="<?=$_POST[studid]?>"> </td> <td> <?php echo $msg_id; ?> </td> </tr>
<tr>
<td> StudDept: *</td>
<td>
<select name="studdept">
<option value="" selected="selected" >select dept</option>
<option value="cse" >cse</option>
<option value="eee" >eee</option>
<option value="mech" >mech</option>
<option value="ece" >ece</option>
</select>
</td>
<td><?php echo $msg_dept; ?> </td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"> </td>
</tr>
</table>
</form>
can someone help me here?
Thanks in Advance...
You have to generate your <option></option> list through loop (if you're generating Department list from Database Table) where you have to put condition which you had put it for first <option> pair.
Or You can put a Turnary Operator (if you're using static list).
<select name="studdept">
<option value="none" <?php echo ($_REQUEST['studdept']=='none'?'selected="selected"'):'';?> >select dept</option>
<option value="cse" <?php echo ($_REQUEST['studdept']=='cse'?'selected="selected"'):'';?> >cse</option>
<option value="eee" <?php echo ($_REQUEST['studdept']=='eee'?'selected="selected"'):'';?> >eee</option>
<option value="mech" <?php echo ($_REQUEST['studdept']=='mech'?'selected="selected"'):'';?> >mech</option>
<option value="ece" <?php echo ($_REQUEST['studdept']=='ece'?'selected="selected"');:''?> >ece</option>
</select>
And use $_REQUEST instead $_GET or $_POST.
The method of your form is POST and you are trying to get the department using GET <? echo ($_GET['studdept'] == 'cse' ? 'selected="selected"' : '') ?>.
Anyway, you'll need to improve your HTML so you check if any of the deptartments are selected (not just 'cse'). A for/while loop will do the job.
In addition, I would add " " to the post variables, as you did in if($_POST['studid']=='' ) (but you didn't in if($_POST[studdept]==''))
Your specifies that the form data will be submitted using the POST method.
The way that PHP does this is to store all the "posted" values into an associative array called "$_POST".
Be sure to take notice the names of the form data names, as they represent the keys in the "$_POST" associative array.
Use: $_POST['studdept'] instead of: $_GET['studdept']
Beacause you use method POST for send data and you are trying to get value using $_GET['studdept'].
so you have two ways:
(1) just change the method to GET or
(2) change $_GET['studdept'] to $_POST['studdept'].

select field in form is not sending data using php

so when i send the form with with the first option 'public' selected. the data is inserted. but when i try submitting the form with the other option selected, the ones in the for each loop. the data no longer is sent. i have inspected the elements. and they are outputting all of the correct values. and they are displaying properly. why arent they inserting into the db? when i click submit nothing happens. but when i click submit for the first option, it works fine?
<form method='POST' action='add.php'>
<select>
<option name="user_page_id" value="<?php echo $_SESSION['user_id']; ?>">Public</option>
<?php
$dis=show_groups_select_list($_SESSION['user_id']);
foreach($dis as $key=>$list){
echo '<option name="user_page_id" value="'.$list['id'].'">'.$list['username'].'</option>';
}
?>
</select>
</form>
You need to put the name attribute on the select tag, not the option tag.
<select name="user_page_id">
<?php foreach ($dis as $key => $list): ?>
<option value="...">...</option>
<?php endforeach; ?>
</select>
I know you got your answer, just some more information for those who face the same problem but that solution did not work:
I had the same problem and it turned to be made by Bootstrap!
In case you are using Bootstrap , you need to provide class attribute of select:
<select name="any_name" class="form-control">
<option value="this is what would be sent if selected"> sth </option>
</select>
for more information take a glance at this discussion too!

Get the Selected value from the Drop down box in PHP

I am populating a Drop Down Box using the following code.
<select id="select_catalog">
<?php
$array_all_catalogs = $db->get_all_catalogs();
foreach($array_all_catalogs as $array_catalog){?>
<option value="<?= $array_catalog['catalog_key'] ?>"><?= array_catalog['catalog_name'] ?></option>
Now how can I get the selected option value using PHP (I have the code to get the selected item using Javascript and jQuery) because I want the selected value to perform a query in database.
Any help will be much appreciated. Thank you very much...
You need to set a name on the <select> tag like so:
<select name="select_catalog" id="select_catalog">
You can get it in php with this:
$_POST['select_catalog'];
Couldn't you just pass the a name attribute and wrap it in a form?
<form id="form" action="do_stuff.php" method="post">
<select id="select_catalog" name="select_catalog_query">
<?php <<<INSERT THE SELECT OPTION LOOP>>> ?>
</select>
</form>
And then look for $_POST['select_catalog_query'] ?
You have to give a name attribute on your <select /> element, and then use it from the $_POST or $_GET (depending on how you transmit data) arrays in PHP. Be sure to sanitize user input, though.
Posting it from my project.
<select name="parent" id="parent"><option value="0">None</option>
<?php
$select="select=selected";
$allparent=mysql_query("select * from tbl_page_content where parent='0'");
while($parent=mysql_fetch_array($allparent))
{?>
<option value="<?= $parent['id']; ?>" <?php if( $pageDetail['parent']==$parent['id'] ) { echo($select); }?>><?= $parent['name']; ?></option>
<?php
}
?></select>

Categories