How to solve
if i select one Value form dropdown. Biside I have another drop down and its values should be auto Updated According to Option I have selected from first dropdown ?
you can try to generate a lookup table whenever you create a drop-down list:
function createDropdown(&$ddlLookup) {
echo '<select multiple name="items[]">';
try {
$items = mysql_query("SELECT item_id,item_type FROM items");
while ($row = mysql_fetch_assoc($items)) {
echo '<option value="'.$row['item_type'].'"';
echo '>'. $row['item_type'] . '</option>'."\n";
$ddlLookup[$item_type] = $item_id;
}
}
catch(PDOException $e) {
echo 'No results';
}
echo '</select>';
}
Then whenever you need the id for a given description you use that table(array) to get it:
$mainDropdownLUT = array();
createDropdown($mainDropdownLUT);
var_dump($mainDropdownLUT['testCow']);
-> 734
Also, if you need to pass it to another page it can be serialized and added to a hidden field.
$mainDropdownLUT = serialize($mainDropdownLUT);
"<input type="hidden" value =\"$mainDropdownLUT\">"
-------------------------**OTHER PAGE **--------------
$mainDropdownLUT = unserialize($mainDropdownLUT);
Related
My form have multiple checkboxes in it (each with the code):
<input type="checkbox" name="id[]" value="<? echo $row['id'] ?>">
when user select id = 2 , 3 and 9 and submit the value. In my update php i am using following code to get selected value
echo "Check box test<pre>" ;
print_r($id);
echo "</pre>";
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $check) {
echo $check."\n";
}
}
// update data in mysql database
$sql="UPDATE table SET display = '2' WHERE id IN ($check)";
i am always getting last selected id updated like 9
but i am not getting result as i wanted like
$sql="UPDATE table SET display = '2' WHERE id IN (2,3,9)";
. please Help what to do. i am very new to php.
You are doing nothing with $check, Make id like this
$ids="";
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $id) {
$ids[] = $id;
}
}
$check = implode(",", $ids);
Because you are iterating the loop and $check will have latest value for the iteration.
foreach($_POST['id'] as $check) {
echo $check."\n";
}
Why can't you try instead ?
$check[] = $_POST['id'];
I think you need to get a better understanding of echo and the diference between server-side and client-side.
What you are looking for is implode
if(!empty($_POST['id'])){
$check = '(' . implode(',', $_POST['id']) . ')';
}
I am populating a select list with a PHP MySQL query. At the same time, I also want to set the select value to the current value in the db. It goes as such
1) Query the associated table and populate the <select> with each entry as an <option>
2) Set the <select> value to the current entry in the database
Right now I am doing this, but when I view the page the <select> is simply at the first option value.
echo '
<select name = "arena" type = "text" value = "'.$row['arena'].'"> ';
try{
$retrieveArena = $db->prepare("SELECT arena_id AS key1, arena_name AS val1 FROM arenas");
$retrieveArena->execute();
$retrieveArenaResult = $retrieveArena->fetchAll(PDO::FETCH_ASSOC);
if ($retrieveArena->rowCount() > 0){
foreach ($retrieveArenaResult as $arena){
echo '<option value = "'.$arena['key1'].'">'. $arena['val1'].'</option>
';
}
}else{
returnMsg("No arenas found");
}
}
catch(PDOException $e){
echo $e->getMessage();
}
echo '
</select>';
You need to give the correct <option> tag the selected attribute. So you will need to know what the selected option should be. Then you can use code like this to build the options.
foreach ($retrieveArenaResult as $arena){
echo '<option value = "' . $arena['key1'] .'"';
if (this option is selected) {
echo ' selected';
}
echo '>'. $arena['val1'].'</option>
';
}
I want to access the value selected by user for further processing.
Hence I am using post method to pass the values of whole form.
But GET to access cust_id so that I can reflect change in
further parts of my form. Hence I had to post the following line:
<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>
outside php code. But now, once I select some option from dropdown menu, URL changes accordingly, but dropdown menu does not reflects the change
<?php
$query = "SELECT Cust_id, Cust_Name, Cust_City FROM Customers";
$result = mysql_query($query);
?>
<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>
<?php
while($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['Cust_id'].'">'.$row['Cust_Name'].','.$row['Cust_City'].'</option>';
}
echo '</select>';
?>
How can I, in the same form, access the address of the particular customer id from database when user selects customer name from this dropdown menu?
I think you mean when you change dropdown, the value is not retained, it obviously won't be because your page is being refresh, you need to GET the value from url and put a selected attribute to have that value selected.
Do it this way:
<?php
$query = "SELECT Cust_id,Cust_Name,Cust_City FROM Customers" ;
$result = mysql_query($query);
//checking if GET variable is set, if yes, get the value
$selected_option = (isset($_GET['product'])) ? $_GET['product'] : '';
//we will store all the dropdown html code in a variable and display it later
$select = "<select id='fullname' onChange=\"window.location='sp_menu.php?product='+this.value\" name='fullname'>";
while($row = mysql_fetch_assoc( $result )) {
//checking if the cust_id matches the GET value,
//if yes, add a selected attribute
$selected = ($selected_option==$row['Cust_id'])?'selected':'';
echo '<option value="'.$row['Cust_id'].'"'. $selected. '>' . $row['Cust_Name'] .' , '.$row['Cust_City']. '</option>';
}
$select .= '</select>';
//display the dropdown
echo $select;
?>
I'm making a dropdown, and I want when the user select one values, save to the database.
This is my code:
$query = mysql_query("SELECT name FROM store_locator_bundes");
echo '<select name="bundesland-dropdown">';
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
echo '</select>';
And the conditional
if($_POST[$criteria['submit']['name']]) {
// here I am lost, I have to insert the values of the dropdown to store_locator and the field bundes_id
}
Thanks for you help ;)
You want
$_POST['bundesland-dropdown']; // You want the <select> not the <options>'s name
Then for the insert:
if (isset($_POST['bundesland-dropdown'])){
//mysqli/PDO/whatever database method of insertion you want here
}
I have a dropdown list of gender. I am getting the values from my table 'candidate' and in this table i have a field which is actually a foreign key to another table.
The field is gender_code_cde, and the table name is gender_code. Now gender_code contains 2 rows. and id and a description. Its structure is like:
1->Male
2->Female
Its being displayed in dropdown list. but I want to get the id 1 if male is selected and id 2 if female is selected. My code is below:
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
I am working in codeIgniter.
// this will alert value of dropdown whenever it will change
<script>
function getvalue(val)
{
alert(val);
}
</script>
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER" id="Gender" onchange="getvalue(this.value)">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
In Javascript, you can get the value with native JS.
var e = document.getElementById("give-the-select-element-an-id");
var gender_cde = e.options[e.selectedIndex].value;
If, you want it back at the server, it will come in to PHP as $_GET['GENDER'] or $_POST['GENDER'] depending on if the form does a POST or a GET request.
If you are submitting a (POST) form and this is inside that form you can access the the value (id) from CodeIgniter's $_POST wrapper: $this->input->post('GENDER');. I don't use CodeIgniter so I could be wrong, but it will be located in your $_POST array from PHP.
If you just want to replicate the variable somewhere on the page you can just echo it. Or set via js/jQuery with document.getElementById('#something').value = value; or jQuery('#something').html(value);.
You should also change the following:
use a foreach in place of your while:
foreach ($query as $row) {
echo $row['gender_x'] . $row['gender_y'];
}
use the CodeIgniter SQL wrapper instead of depreciated PHP functions. The query part is $this->db->query('SELECT statement goes here');. You should look at your CodeIgniters' version documentation for a more in depth explanation.
EDIT: To make it a bit more clear, an example:
$query = $this->db->query('SELECT * FROM gender_code');
foreach ($query->result() as $row) {
echo '<option value="' . $row->gender_cde . '">' . $row->geneder_dsc . '</option>';
}
This is assuming you have setup the previous calls in CodeIgniter. Please see http://ellislab.com/codeigniter/user-guide/database/examples.html and you may wish to read http://ellislab.com/codeigniter/user-guide/