can't get value from select out of DB - php

I have a problem with getting a value out of a select which gets it's options out of the database. I've tried looking for a solution but I can't seem te find an answer because the options aren't hard coded.
This is the option select:
<select id="selectmission" name='missionselect' onchange='showoptions()'>
<?php while($mission = $allmissions->fetch_assoc())
{ echo "<option value='".$mission['missionid']."'>".$mission['missionname']."</option>";?>
<option value="other">Other</option>
</select>
This is the php:
if(isset($_POST['btnSubmit']))
{
try
{
$t->Description = $_POST['description'];
if($_POST['missionselect'] = 'other')
{
$m->Missionname = $_POST['missionname'];
$m->CreateNewMission();
}
else
{
$t->Missionid = $_POST['missionselect'];
}
I have tried to let it echo the value it identifies by using this code:
$select = $_POST['missionselect'];
echo $select;
It showed that it always detects the option 'other', the only hardcoded option.
I hope somebody can see what I've missed!
Thanks guys,
Jana

Perhaps this change:
if($_POST['missionselect'] = 'other')
if($_POST['missionselect'] == 'other')

Related

Altering PHP Variable Based Off Selection

I have a html select that holds values for all four of our offices, or the text All. If the user selects All, I want to echo "All Offices", but if the user selects a specific office, I want to echo that number. My problem is that when I run the syntax below, All remains All instead of All Offices.
Did I set this up the incorrect way?
Display Data For Which Office:
<select name="office">
<option value="All">All...</option>
<option value="one">One Quarter</option>
<option value="two">Two Quarter</option>
<option value="three">Three Quarter</option>
<option value="four">Four Quarter</option>
</select>
<?php
if ($officename != 'All') {
$officename = $_POST['officename'];
} else {
$officename = "All Offices";
}
echo $officename;
?>
You never initialized the $officename variable, so it should be null. As a result, won't $officename != 'All' always be true, so $officename = $_POST['officename']; will always be executed?
I think what you want instead is something like:
$officename = $_POST['officename'];
if ($officename == 'All') {
$officename = "All Offices";
}
echo $officename;
I assume you're wanting "All" to come from the value of the <select>, which is named "office". Therefore you need to assign that value to a variable before you can use it for comparison. Note the select is named "office" not "officename", so it's "office" you need to look for in $_POST.
$officeValue = $_POST['office'];
if ($officeValue == 'All') {
$officename = "All Offices";
}
else {
$officename = $_POST["officename"]; //I assume this is some other field in the form which you haven't shown, but which contains a human-readable office name.
}
echo $officename;

PHP drop down box not showing selected value

Hi I have a piece of php code which takes php variables from a database and then populates A drop down box. This works fine but once the page updates it doesn't display the selected value and instead just displays the original list again.
I have played around with some different ideas of using isset to displays the selected value, but then it just displays that value and nothing else.
I have attached the original code where it works but always only shows the original list of values from the database.
If any body could provide a solution or even a nudge in the right direction then it would be much appreciated.
Thanks
if (isset($select) && $select != "location") {
$select = $_POST['location'];
}
?>
<select name="location">
<?php
// Get records from database (table "name_list").
$list = mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");
// Show records by while loop.
while ($row_list = mysql_fetch_assoc($list)) {
$location = $_POST['location']; ?>
<option value="<?php echo $row_list['region_name']; ?>"
<?php if $row_list['region_name'] == $select) {
echo "selected";
} ?>><?php echo $row_list['region_name']; ?></option>
<?php } ?>
You are sure you send that form with a post request?
Your code is unreadable
Better do something like this:
<?php
//database request don't belong in the presentation layer
$list=mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");
$location = "";
$isSelect = "";
if(isset($_POST['location'])){
$location = $_POST['location'];
}
while($row_list=mysql_fetch_assoc($list)){
if($row_list['region_name'] == $location ){
$isSelect = "selected";
}
echo '<option value="'.$row_list['region_name'].'" '.$isSelect.' >'. $row_list['region_name'].'</option>';
}
?>
You are not checking the select value properly.
Change this:
if (isset($select) && $select != "location") {
$select = $_POST['location'];
}
TO
if (isset($_POST['location']) && $_POST['location'] != "location") {
$select = $_POST['location'];
}
While creating the <option> tag, you may use the following:
<?php if ($row_list['region_name'] == $select) { echo "selected='true'"; } ?>
You may also like to check if the condition $row_list['region_name'] == $select is returning true. You may try to echo their result separately as:
echo ($row_list['region_name'] == $select)
or something similar that would show if you get true or not.
The problem lies in the first line - actually the system will never execute 2nd line to assign $select because $select is never set:
if(isset($_POST['select']) && $select!="location") { $select=$_POST['location']; } ?>

How to dynamically get the value of a select tag from MySQL table

Thanks for dropping by and I'm sorry if this question seems to be easy as I'm kinda new to php. Im trying to do a school project and my problem now is that I cant seem to get the values of the dropdown. I have been trying to search in every forum and some similar questions in here but they seem to only show how i can populate the dropdown with database values.
The code for populating the dropdown menu is working, but recording the selected value is not working for me.
Here is my php code for the dropdown:
<div class="panel">
<label class="panelname">Category:</label>
<select name="category" class="signup_field">
<option>Select One</option>
<? // retrieve all the categories and add to the pull down menu
$query = 'SELECT category_id, name FROM category ORDER BY name ASC';
$request = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$cat_id = $row[0];
echo "<option value=\"row[0]\"";
//check for stickyness:
if (isset($_POST['category']) && ($_POST['category'] == $row[0])) {
echo ' selected="selected"';
//$_POST[]
} echo ">$row[1]</option>\n";
}
?>
</select>
<? if (array_key_exists('category', $add_product_errors)) echo '<label class="error">' . $add_product_errors['category'] . '</label>'; ?>
</div>
and here is my part of my code for validation: -
// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for a category:
if (!isset($_POST['category']) || !filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
$add_product_errors['category'] = 'Please select a category!';
}
..... // other codes for validation (Eg: if (empty($add_product_errors)) { proceed to query} )
I don't know what seems to be the problem but there are no values recorded in the $_POST['category'] for me to save into MySQL and I keep getting the Please select a category error.
Thank you all for your help. :)
your problem seems to be echo "<option value=\"row[0]\"";. You want to replace it with
echo "<option value=\"$row[0]\"";
echo "<option value=\"row[0]\""; just puts a String row[0] there
Thanks, I seem to have solve the problem ..
I have seemed to have deleted the ($)row in the row[0] for it to be read properly.
just put $ on the row[0]:
echo "<option value=\"row[0]\"";
I used to solve this problem like ... In your case.
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$cat_id = $row[0];
$str = null;
if (isset($_POST['category']) && ($_POST['category'] == $row[0])) {
$str = 'selected="selected"';
}
echo '<option value="'.$row[0].'" '.$str.'>'.$row[1].'</option>';
}

php select-option post back retain value

I have a form with input type-select
however when page submit and post back with error message
The select value is clean, I try to use radio button (checked unchecked) to retain the value,
yet it is not working.
//HTML///////////////////////////////////////////////////////////////////
<select name="age">
<option value="">Age</option>
<option value="14-17" <?PHP echo $age_14; ?>>14 - 17</option>
<option value="18-29" <?PHP echo $age_18; ?>>18 - 29</option>
</select>
//PHP////////////////////////////////////////////////////////////////////
$age_14="unchecked";
$age_18="unchecked";
$age=$_POST['age'];
if($age=='14-17'){$age_14="checked";}
else if($age=='18-29'){$age_18="checked";}
With select tag you have to use selected="selected" not checked.
Try with:
$age_14 = "";
$age_18 = "";
$age = $_POST['age'];
if ($age == '14-17') {
$age_14 = 'selected="selected"';
} elseif ($age == '18-29') {
$age_18 = 'selected="selected"';
}
With select boxes, the attribute is selected, not checked. You also do not need the unchecked attribute.
There is no checked attribute. You need to use selected:
$age=$_POST['age'];
$age_14 = $age=='14-17'?"selected":"";
$age_18 = $age=='18-29'?"selected":"";

<select><option> and action associated with them

I am writing a reservation system. On the main page I would give a choice of category, viewed the equipment available for booking.
For example I have code like this:
<select>
<option value = "a">A</option>
<option value = "b">B</option>
<option value = "c">C</option>
<option value = "d">D</option>
<option value = "e">E</option>
</select>
I wish that each choice was associated with a separate query to the database, and that was the result of a query dynamically displayed on the screen.
It would be great if you could show me some sample code.
Regards
$query = mysql_query("SELECT * FROM choices");
while($row=mysql_fetch_assoc($query)) {
echo '<option value="'.$row['value'].'">'.$row['value'].'</option>';
}
If you need separate query for each choice the code doesns't change much:
$query = mysql_query("(SELECT * FROM choices) UNION (SELECT * FROM choices1) [etc]");
while($row=mysql_fetch_assoc($query)) {
echo '<option value="'.$row['value'].'">'.$row['value'].'</option>';
}
There are two parts to your question; 1 - Detecting which query to run and 2 - Displaying the results dynamically.
Part 1: Detecting which query to run:
Given hard-coded choices and no parameters for the query, using your above code, you can determine which query to run using the following:
For the HTML part, as part of a form, create the select as you did above (but with a name)
<select name="querySelect">
<option value="a">A</option>
<option value="b">B</option>
</select>
And in the PHP:
$querySelect = $_GET['querySelect'];
switch($querySelect)
{
case 'a':
$sql = "SELECT * FROM TableA";
break;
case 'b':
$sql = "SELECT * FROM TableB";
break;
}
$results = mysql_query($sql);
Part 2: Displaying the results dynamically
With the $results, what you do with the data very much depends on what you want to achieve. At a very basic level, you can do the following to dynamically display a table of the results:
if(mysql_num_rows($results) > 0)
{
$header = false;
print "<table>"
while($row = mysql_fetch_assoc($results))
{
if(!$header)
{
$headings = array_keys($row);
print "<tr>";
for($i=0;$i<count($headings);$i++)
{
print "<th>".htmlspecialchars($headings[$i])."</th>";
}
print "</tr>";
$header = true;
}
print "<tr>";
foreach($row as $value)
{
print "<td>".htmlspecialchars($value)."</td>";
}
print "</tr>";
}
print "</table>"
}
else print "<h1>No Results Found!</h1>";
mysql_free_result($results);
There is still alot not covered in my answer because I can't say what level of detail is required. You will also need to cover things like your connection to MySQL, error handling, formatting of the table...
Update
Hmmm, very interested to know why this has been downvoted. If someone can please explain in comments where I have misinterpreted the question or misguided the user, I would appreciate it.
If you use jQuery the code might look like
<select id="select_id">
<option value = "a">A</option>
<option value = "b">B</option>
<option value = "c">C</option>
<option value = "d">D</option>
<option value = "e">E</option>
</select>
<script type="text/javascript">
$('#select_id').change(function(e) {
// this code send selected value to the server
$.post('url', {selected_value: this.value}, function(response) {
// handle the server's response
});
});
</script>
On server side take the value from $_POST and make a query. And remember - do not trust to data from client-side. You never know who is over there. Always check incoming data and DO NOT use such constructions
$sql = "SELECT * FROM table_name WHERE name = '{$_POST['selected_value']}'";
because there might be any string including those can drop all databases, clear data and so forth.

Categories