I have a dropdown menu that is generated in another file, and I would like to echo the name of item they selected from dropdown menu not the value of it (0,1,2,3...).
Here is code of the dropdown menu that is generated in another file:
<?php
$selectedKey = $_GET['selected_key'];
$query = "SELECT * FROM `1 selected` WHERE Key = '".$selectedKey."'";
$run = mysql_query($query);
$id1 = 0;
while( $row = mysql_fetch_assoc( $run ) ) {
echo "<option value='".$id1."'>".$row['Contract']."</option>";
$id1++;
}
?>
And this is how it looks in index.php:
<select id="text2" name="text2">
</select>
Now after they submit data I want to echo the name of the item they selected, but when I use echo $_REQUEST['text2']; I get number (value) back, but as said I want to get name.
Select box will send only it's selected value index to the server, not the value that you see. So simply use $row['Contract'] as the value to be sent to the server:
echo "<option value='".$row['Contract']."'>".$row['Contract']."</option>";
The problem is browsers do not submit the display text to the server, only the value. There are several workarounds though:
Option 1
Set the value as the display text instead of $id1. The value does not have to differ from the display text.
Option 2
Use a delimeter in the value and set both the value and display text:
echo "<option value='".$id1. ';' . $row['Contract'] ."'>".$row['Contract']."</option>";
Explode on the delimiter to get both:
list($value, $text) = explode(';', $_POST['text2']);
Option 3
Use Javascript, set the form's onsubmit event to a function that populates a hidden input with the display text.
Related
I am having a select field in which I am trying to achieve something like when the dropdown is selected, datatable will be sorted accordingly. Once the user is done with sorting, There is a button to clear all values of dropdown and get the general value to show all rows without sorting. The values are taken fine except the text of the value remains the same.
//dropdown
<select value='Deliver To' name="_location" id="_location" class="SlectBox form-control">
<?php echo location(); ?>
</select>
//PHP to get results to dropdown
function location(){
global $con;
$output.= '<option value="_allCity">All Results</option>';
$_selectquery= "SELECT * FROM _tableone";
$result = mysqli_query($con, $_selectquery);
while($row = mysqli_fetch_array($result)){
$output.= '<option value = "'.$row["name"].'">'.$row["name"].'</option>';
}
return $output;
}
//Button to reset dropdown
$('#reset').click(function(){
$('#_location').val('_allCity').change();
$('#_location').text('All Results').change();
});
When I reset the value is taken correctly but the text is still the manually selected one.
I have a Edit form in php and everything work great. The only issue i have is when i click edit it returns all the data except in the Select drop down. it does not have the chosen category it always shows the first value in the list. But i then can click on the drop down and choose a new category and it works.
//Query the category table
$resultSet = $con->query("SELECT * FROM schedule_category");
<select id="schedule_category" name="schedule_category" class="custom-select">
<?php
while($rows = mysqli_fetch_assoc($resultSet))
{
?>
<option value = "<?php echo($rows['schedule_category'])?>">
<?php echo($rows['schedule_category']) ?>
</option>
<?php
}
?>
</select>
I would like to have it show the correct select option record not the first one in the drop down list. Here is an image of what happens https://imgur.com/a/XVXQ2Sa
You'll need to have your code compare each to the selected value, and add the appropriate keyword:
$previous_selection = // whatever it is, from your data
while($rows = mysqli_fetch_assoc($resultSet))
{
$thisone = $previous_selection == $rows['schedule_category'] ? " selected " : "";
echo '<option value = "';
echo ($rows['schedule_category']) . '"' . $thisone . '>';
echo($rows['schedule_category']) . '</option>';
}
What you're doing here is comparing your previously-selected value to each row, when it matches, the variable $thisone is set to "selected", otherwise it's empty. You then add that to each option line after the value and before the close-tag for the option, and it will add "selected" when the value matches.
Also I personally don't like switching in and out of PHP for no good reason, makes it really difficult to read, hence I echo the various bits of HTML here.
ETA - actually that could be simplified further, if your selection value is the same as the text displayed in the list, there's no need to actually specify the value in the option tag. That is only required when the value is different to the display, for example if you want the user to see your category names, but you want to submit the category ID.
I have a form in an edititem.php page that is used to edit an item. The idea is that from another page, the searchitem.php a user can click edit and is taken to the edititem.php page to edit the selected item. What I'm trying to do is that automatically the default values in the form elements are populated with the values of the item selected to edit (from the searchitem.php) so that the user finds the form populated and only needs to modify the necessary value/s.
I'm passing all the required variables to the edititem.php and I am able to poulate all the input tags but I have a problem with the select tag which is not being set with the desired value. The select tag is being populated dynamically from a mysql database.
<label>Category:</label>
<select name="category">
<?php
$selectedCategory = '';
if (isset($_POST['category'])) {
$selectedCategory = $_POST['category'];
}
$sql_cat = "SELECT id, description FROM category ORDER BY description ASC";
$result_cat = mysqli_query($connection, $sql_cat);
if(mysqli_num_rows($result_cat) > 0){
while($row = mysqli_fetch_assoc($result_cat)){
$selected = '';
if ($selectedCategory == $row['id']) {
$selected = 'selected';
}
echo '<option value="' . htmlspecialchars($row['id']) . '" '.$selected.'>'
. htmlspecialchars($row['description'])
. '</option>';
}
}
?>
With the above code the items in the select tag are being populated dynamically from a table and also if the page is refreshed the selected item is maintained as the selected value.
However I cannot set the default value when I press the edit item from the searchitem.php. The last value in the table is being displayed as default. Any ideas how I can achieve this since I cannot figure out how to do it. Note that all the variables are being passed successfully to the edititem.php page I just need to set the default value of the select drop down list as per the passed variable while keeping the select drop down list dynamic.
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 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/