I have a Select/Combo Box that I use to select the number of records to display on a page. In order to cover the 'All' option I pass the ID and the Number Selected in the value field.
<?php //get info for results per page combo box //
$stmt = $db->prepare("SELECT resultspp.ID, resultspp.NumberResults FROM resultspp");
$stmt->execute();
$rows2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows2 as $row) {
if ($pageID !=$row['ID']) { ?>
<option value="<?php echo $row['ID']; ?>|<?php echo $row['NumberResults']; ?>"><?php echo $row['NumberResults']?></option>
<?php } else { ?>
<option value"<?php echo $row['ID'];?>|<?php echo $row['NumberResults']; ?>" selected><?php echo $row['NumberResults']?></option>
<?php }
}
?>
This all works fine and the correct values are posted, checked using print_r($_POST). However when another buttom is submitted on the page it posts the option displayed to the user, not the value in the value tag. I have checked this by change the selected option to a constant.
The values are unpacked like so after the post.
<?php
if($_POST['resultsPP']){
$pageresult = explode('|', $_POST['resultsPP']);
$pageID = $pageresult[0];
if ($pageresult[1] == 'All'){
$number_select = $totalRecords;
} else {
$number_select = $pageresult[1];
}
}
?>
Thanks in advance
You have missed "=" sign in your code. Below is the updated code. This causing the posting of option instead of posting of value.
.........
else { ?>
<option value="<?php echo $row['ID'];?>|<?php echo $row['NumberResults']; ?>" selected><?php echo $row['NumberResults']?></option>
<?php }
.......
Related
I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.
I have :
<select name="department" id="department" onchange="this.form.submit()">
<?php
do {
?>
<option value="<?php echo $row_department['dept_Name'] ?>"><?php echo $row_department['dept_Name'] == $department ? 'selected' : ''?><?php echo $row_department['dept_Name']
?></option>
<?php
} while ($row_department = mysql_fetch_assoc($department));
$rows = mysql_num_rows($department);
if($rows > 0) {
mysql_data_seek($department, 0);
$row_department = mysql_fetch_assoc($department);
}
?>
</select>
For the dropdown from the database named department.
Once dropdown selected, multiple file sets will viewed from database such as id, name, dept_name with next/previous button.
The problem is, whenever i clicked next/previous button, there no files viewed.
Dropdown must be selected for the 2nd time for view 2nd file set.
What should I do and how? I'm using Dreamweaver CS6.
Try this. You canno't search because you don't have a query to do that.
$deptName = $row['dept_Name'];
$sql = mysql_query("SELECT * FROM (table_name) WHERE (column_department_name) = '$deptName'");
while ($row_department = mysql_fetch_assoc($sql));
$rows = mysql_num_rows($row_department);
if($rows > 0) {
echo $rows['(your_column_name)'];
}else{
echo 'nothing';
}
I have a PHP select that dynamically populates based on a MySQL recordset and uses a in_array value to identify and select the value(s) found in the database like this:
<select name="StaffSetSelection[]" size="5" multiple="MULTIPLE" id="StaffSetSelection">
<option <?php if ($totalRows_StaffSetID == 0) { echo "selected"; } ?> value="">Choose a Staff Set</option>
<?php
do {
?>
<option <?php if (in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo "selected";
} ?> value="<?php echo $row_StaffSetChoices['EmpNumber'] ?>"><?php echo $row_StaffSetChoices['EmpFirstName'] ?></option><?php
} while ($row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices));
$rows = mysql_num_rows($StaffSetChoices);
if ($rows > 0) {
mysql_data_seek($StaffSetChoices, 0);
$row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices);
}
?>
</select>
Instead of displaying the selected values in a form option select I'd like to just display them as text.
I've tried to use the in_array again in a simple php echo but it does not display the data and there are no errors in my apache log.
This is what I've tried:
<?php if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) echo $row_StaffSetChoices['EmpFirstName']?>
I think you forgot the brackets,
Try this way:
<?php
if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo $row_StaffSetChoices['EmpFirstName'];
}
?>
HI i have two drop down boxes which are used to filter data from a mysql table, the problem is the boxes always show as if they are set when i use isset to determin if any of them have any values selected.
Here is the code for the drop boxes.
<?php
if (isset($_POST['referrer']) && $_POST['referrer'] != "referrer") {
$select = $_POST['referrer'];
}
?>
<select name="referrer">
<option value="">Referrer</option>
<?php
// Get records from database (table "name_list").
$list=mysql_query("select DISTINCT referrer from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(referrer,'')) <> '' order by referrer DESC");
// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
$referrer = $_POST['referrer']; ?>
<option value="<?php echo $row_list['referrer']; ?>" <?php if($row_list['referrer']==$select){ echo "selected"; } ?>><?php echo $row_list['referrer']; ?></option>
<?php
}
?>
</select>
<?php
if (isset($_POST['returning']) && $_POST['returning'] != "returning") {
$select2 = $_POST['returning'];
}
?>
<select name="returning">
<option value="">Referrer</option>
<?php
// Get records from database (table "name_list").
$list2=mysql_query("select * from repeater_drop_down order by id DESC");
// Show records by while loop.
while($row_list2=mysql_fetch_assoc($list2)){
$returning = $_POST['returning']; ?>
<option value="<?php echo $row_list2['value']; ?>" <?php if($row_list2['value']==$select2){ echo "selected"; } ?>><?php echo $row_list2['title']; ?></option>
<?php
}
?>
</select>
Now here are the isset elements of the code.
$returning = $_POST['returning'];
$referrer = $_POST['referrer'];
if (isset($returning, $referrer)){
//code //
elseif (isset($returning)){
///code //
elseif (isset($referrer)){
//code //
else {
//code
}
I haven't included the queries as they work fine but whether or not i select both drop down boxes the code automatically goes to the first isset for both variable even if only one of the boxes has a option selected.
Any advice or suggestions would be appreciated.
You should check the value that was submitted with the form, not only whether or not the select POST value is set.
The value of the referrer will be '' (an empty string) if it is submitted without changing the select. Check for that instead.
I have a problem right now [PHP]. I have a dropdown and its loading my database for the first page, when I proceed to the next page it also have a dropdown where its also loading the my database and also I can get the value of my dropdown in the first page using an echo only.
This is the scenario:
I choose in the dropdown first page the "Letter A" and when I click the button it will proceed to the next page. The dropdown in the second page loaded the items in the database but instead of "-select-" is the first index in the dropdown I want is "Letter A" will be the first index.
This is my code in first page for drowdown:
<select name="id">
<option value="" >- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
and this is my code in second page for dropdown:
if ($get_ID != "")
{
echo "<br/>";
echo $get_ID;
//echo "show()";
}
else
{
echo "No Network Selected";
echo "<br/>";
//echo "hide()";
}
?>
<option value="">- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
Thanks in advance!
On your second page, you need to check whether the value is the same as the one you received from the first page:
echo "<option value='".$row1[fldNetname]."' " . (($row1[fldNetname] == $get_id)?"selected":"") . ">".$row1[fldNetname]."</option>";
if $get_ID, is actually your $_GET['id'] value, then just do...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."'";
if($row1['fldNetname']==$get_ID){echo "selected='selected'";}
echo ">".$row1['fldNetname']."</option>";
}
Or as one line...
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1['fldNetname']."' " . (($row1['fldNetname'] == $get_ID)?"selected='selected'":"") . ">".$row1'[fldNetname']."</option>"
}
Have you tried?
changing (on the second page)
<option value="">- select -</option>
to
<option value="<?php echo $yourVar; ?>"><?php echo $yourVar; ?></option>
Then in the while loop, skip the value selected to prevent a duplicate choice.
Something like:
while ($row1 = mysql_fetch_array($q))
{
if($yourVar != $row1[fldNetname]){
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
}