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.
Related
I tried to write codes which display value in textbox based on value selected in combobox, but I have one problem I puted combobox and texbox in loop but only first row can display value of selected item in combobox where I want each row to display value based on value selected in its combobox
Below are my codes
<?php
require_once('connection.php');
for($counter=0;$counter<=1;$counter++)
{
$queItem= mysqli_query($connect,"select * from oitm");
echo"<select name=\"segment_id\" class=\"form-control\" id=\"segment_id\" STYLE=\"width: 300px\" >";
echo"<option value=\"\" selected=\"selected\">Select a product</option>";
while($rowStockIn = mysqli_fetch_array($queItem))
{
$ItemCode2=$rowStockIn['ItemCode'];
$ItemName2=$rowStockIn['ItemName'];
$ItemPrice2=$rowStockIn['ItemPrice'];
echo "<option value=\"$ItemCode2\" data-itemprice = \"$ItemPrice2\">
$ItemCode2 $ItemName2";
}
echo"</select>";
?>
</span>
<?php
echo"<input id=\"title\" name=\"title\" type=\"text\" value=\"$ItemPrice2\"
><br/>";
}
?>
<script>
$(document).ready(function(){
$("#segment_id").on('change', function () {
var id = $(this).val();
var itemprice = $('option:selected',this).data('itemprice');
$("#title").val(itemprice);
});
});
</script>
I think I have to edit jQuery in order to allow each row to work
Please anyone can help me
Ok, well, if you are using a combo box then clearly the user should be able to select more than one option. Your first problem is that $itemCode2 in your while loop is just a variable not an array. Therefore, as you described in your issue you'll only ever get one value because it will be overwritten each time in the loop.
Make $itemCode2 an array - for example:
$itemCode2[] = $rowStockin['itemCode']
Then you should be able to output the various options that the user can select.
Also, be careful using select * in your queries. Try to be specific in your column selections. select * could leave your script exposed to a sql injection attack.
I am making a drop down menu about brands in a ecommerce website form where i have to enter products. Is there a possible way to retrieve the selected value of the dropdown without using the submit button. Better still, can i use the select option without an enclosing element.
If this is possible, how can i retrieve the value of the dropdown by using jquery.
function getbrands_add_products(){
global $con;
try{
$query = "select * from brands";
$stmt = $con->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
echo "<option>Select Brand</option>";
foreach ($result as $rows) {
$brand_id = $rows['brand_id'];
$brand_title = $rows['brand_title'];
echo "<option value='$brand_id'>$brand_title</option>";
}
} catch(PDOException $e){
echo "Error: ".$e->getMessage();
}
}
This is the function in which i am querying the database and displaying them as a dropdown list for the user to select. How can i access the selected value without using the form submit. Best if it is jquery.
yes you can use it without <form> tag.
in fact you can use any form related element without the <form> tag.
if you want to use the <select> elemet selected value you can use select.onchange event.
the element value is selected option value.
html Code:
<select id='select'>
<option value='11'>
testOption
</option>
</select>
js Code:
var select = document.getElementById('select');
select.onchange = function () {
var value = this.value;
alert(value);
}
you can use <select> outside the form but its not the standard way.
you can access the value using javascript jquery. give it an id like
<select id="select-box">
<option value = "something"></option>
....
and then on jquery use:
$("#select-box").on("change",function(){
var value = $(this).val()
})
but it is not the standard way to use select box outside the form
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/
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.