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>
Related
I searched and searched the internet for help but I didn't get it so I'm posting here. I've got a select option thing in my code and I want to get the value of it so I can use it in a query, but I don't know how to get the value from the select option in the same page. So how can I get the value from this select? I'll give you the code to my select.
<select name="tamanho">
<?php
$sqltamanhos="SELECT * FROM detalhes_produtos WHERE cod_produto ='$codigo'";
$resultadoo=odbc_exec($ligaBD,$sqltamanhos);
while (odbc_fetch_row($resultadoo))
{
$tamanho = odbc_result($resultadoo,2);
?>
<option value ="<?php echo $tamanho; ?>"> <?php echo $tamanho; ?> </option>
<?php } ?>
you can get the value of select box via posting that value in the form(get/post method).
<form method="get" action="">
<select name="tamanho">
<?php
$sqltamanhos="SELECT * FROM detalhes_produtos WHERE cod_produto ='$codigo'";
$resultadoo=odbc_exec($ligaBD,$sqltamanhos);
while (odbc_fetch_row($resultadoo)){
$tamanho = odbc_result($resultadoo,2);?>
<option value ="<?php echo $tamanho; ?>"><?php echo $tamanho; ?> </option>
<?php }?>
</form>
and get the value via
$value=$_GET['tamanho'];
you can also check this get the value of <select> without submitting on the same page using php
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.
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!
i have a php form which submits data to another php for validation. if any fields are left blank the validator php sends a message back to original php form along with all the pre filled variables using session variables so that user doesn't have to fill them again.
i can use these session variables to fill in text boxes again like this
value="<?php echo $_SESSION['fname'] ?>"
but how to go about populating drop downs, radio buttons and check boxes?
thanks
For a select, checkbox or radio box you would need to check whether the values match. Something like:
<select name="fname">
<option value="1" <?php if($_SESSION['fname'] == 1) echo 'selected'; ?>>1</option>
<option value="2" <?php if($_SESSION['fname'] == 2) echo 'selected'; ?>>2</option>
</select>
It's easier if you are iterating through the values for the option field:
<select name="fname">
<?php foreach($array AS $key => $value) { ?>
<option value="<?php echo $value; ?>" <?php if($_SESSION['fname'] == $value) echo 'selected'; ?>><?php echo $value; ?></option>
<?php } ?>
</select>
A <select> element's selected <option> is not determined by it's value= attribute. In order to mark an option selected, the <option> element must have the selected attribute set on it, as follows:
<select>
<option>1</option>
<option selected>2</option>
<option>3</option>
</select>
In order to do this programmatically, you need to iterate through the options, and manually test for the correct option, and add selected to that.
... Noticed that your double quote at the end of the statement was misplaced, it should be:
<script>
$("#fname").val("<?php echo $_SESSION['fname'] ?>");
</script>
There is a far simpler method using JQuery. You can set Selects with .val() so make the most of it.
<script type="text/javascript">
$("#fname").val("<?php echo $_SESSION['fname']; ?>");
</script>
Don't forget to set the id of the select as well as the name to fname.
In my application am submiting my form by using post for a php page.
<form method="post">
<select name="txtplace">
<option value="1">ajith</option>
</select>
</form>
here when i am try to get the value of my dropdown its only getting 1.How can i get ajith
<option value="ajith">ajith</option>
Should work. You get the value as 1 because you have set the value as 1.
Maybe it's better if you define an array in php:
$arr = array ( 1 => "ajith" );
Then generate the HTML dynamically:
<option name="test" value="$i">$arr[$i]</option>
Then after post:
$key = $_GET['test'];
$value = $arr[$key];
If you just want "ajith" then do as Shoban suggests. If you need both 1 and "ajith" you could always have:
<option value="1-ajith">ajith</option>
And then with php:
$sTxtPlace = $_POST['txtplace'];
list($number,$text) = explode("-",$sTxtPlace,2);
You'll only get '1' in your form script, as that's the value of the option element. You could change to the following:
<form method="post">
<select name="txtplace">
<option value="ajith">ajith</option>
</select>
</form>
rather than using the ID (I presume) of that item.
<option value="ajith">ajith</option>
You can use a hidden element and add an onchange handler to the select to populate the label part. Advantages are that you dont have to change your rendering code and you dont need to explode.
Try:
<form method="post">
<input type="hidden" id="txtPlaceLabel" name="txtPlaceLabel" value="">
<select name="txtplace" onchange="document.getElementById('txtPlaceLabel').value=this.options[this.options.selectedIndex].innerHTML;">
<option value="ajith">ajith</option>
</select>
</form>
Note: Haven't tested the code so maybe some editing will be required.