One select does not post the selected option - php

Example... this query is displayed even though the ">" Greater Than option was selected
SELECT *
FROM sr_rounds
WHERE cir1 LIKE '%2.4%'
ORDER BY sr_id asc
Any ideas?
<form action="search_new.php" method="post" name="exchanges" id="exchanges">
<select name="circuits" id="circuits" onclick="searchIt()" onblur="searchIt()">
<option value="select" selected="selected">Select Search Field</option>
<option value="cir1">Circuit 1</option>
<option value="cir2">Circuit 2</option>
<option value="cir3">Circuit 3</option>
<option value="cir4">Circuit 4</option>
<option value="vch">VCH-1-5</option>
</select>
<select name="sorting" id="sorting" onclick="searchIt()" onblur="searchIt()">
<option value="select1" selected="selected">Sort By</option>
<option value="sr_id">Sector</option>
<option value="date">Date</option>
//This select always returns a "like" clause regardless of which option you select
</select>
<select name="clauses" id="clauses" onclick="searchIt()" onblur="searchIt()">
<option value="select2" selected="selected">Clause</option>
<option value="=">Equals</option>
<option value="like">Like</option>
<option value=">">Greater Than</option>
<option value="<">Less Than</option>
<option value="date">Date</option>
</select>
<select name="sortorder" id="sortorder" onclick="searchIt()" onblur="searchIt()">
<option value="select3" selected="selected">Order</option>
<option value="asc" name="1">Asc</option>
<option value="desc" name="2">Desc</option>
</select>
<?php
$select = $_POST['circuits'];
$searchdb = $_POST['searchdb'];
$select1 = $_POST['sorting'];
$select2 = $_POST['clauses'];
$select3 = $_POST['sortorder'];
//To display friendly field name instead of actual field name
if ($select == 'cir1')
{
$boxresult = 'Circuit 1';
}
if ($select == 'cir2')
{
$boxresult = 'Circuit 2';
}
if ($select == 'cir3')
{
$boxresult = 'Circuit 3';
}
if ($select == 'cir4')
{
$boxresult = 'Circuit 4';
}
if ($select == 'vch')
{
$boxresult = 'VCH-1-5';
}
//To use different queries while searching
if ($select2 == '=')
{
$queres = "SELECT * FROM sr_rounds WHERE $select = '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == 'like')
{
$queres = "SELECT * FROM sr_rounds WHERE $select LIKE '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == '>')
{
$queres = "SELECT * FROM sr_rounds WHERE $select > '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == '<')
{
$queres = "SELECT * FROM sr_rounds WHERE $select < '$searchdb' ORDER BY $select1 $select3";
}
else if ($select2 == 'contain');
{
$queres = "SELECT * FROM sr_rounds WHERE $select LIKE '%$searchdb%' ORDER BY $select1 $select3";
}
$query = $queres;
$result = #mysql_query($query);
$num = #mysql_num_rows($result);

It is because you have a semi-colon in the line:
else if ($select2 == 'contain');
Make it
else if ($select2 == 'contain')

You have a typo in your code:
else if ($select2 == 'contain');
// ----------------------------^
This ends the if block. The next {} block executes always.

write this in your search_new.php:
var_dump($_POST); exit();
Check what clauses value is. I'd also go for another representation of the values. I tend to use numbers and reduce the if/else structure to a switch block, but anyway you can stick to whatever. Just an advice. Instead of >, < and =, use "greater", "lower" and "equals", or another word, as < and > are part of the xml structure, and there may be a problem for your browser to understand that. If you check the codeblock you wrote, you will see that the > is actually not acting well. It's painted black, instead of brown/red.
To sum up:
Check the value of $_POST
Change the option values to numbers or words, not symbols.

Related

PHP multiple select form how to use: if (taskOption[0] is selected)

HTML:
<select name="taskOption[]" multiple>
<option>first<br /></option>
<option>second<br /></option>
<option>third</ option>
</select>
PHP:
<?php
foreach ($_GET['taskOption'] as $selectedOption){
echo "lesson:".$selectedOption."<br>";}
?>
This php code simply prints the selected options.
How can i separately do something if an option is selected ? for example
if (taskOption[0] is selected){
$x="1";
if (taskOption[1] is selected){
$y="1";
What i have tried with partial success so far is this:
$options = array("", "", "");
foreach ($_GET['taskOption'] as $selectedOption)
echo "".$selectedOption."<br>";
if($selectedOption == 'first'){
$options[0] = "11";
echo $options[0];
}
elseif($selectedOption == 'second'){
$options[1] = "22";
echo $options[1];
}
elseif($selectedOption == 'third'){
$options[2] = "33";
echo $options[2];
}
but i still have problem when i choose 2+ option..
(it only echo the last option)
You should add values to the options in the select tag first, so you can easy identify each one.
<select name="taskOption[]" multiple>
<option value="one">first<br /></option>
<option value="two">second<br /></option>
<option value="three">third</ option>
</select>
After it, you should remove the "elseif" and simply do ifs for all
$options = array("", "", "");
foreach ($_GET['taskOption'] as $selectedOption)
echo "".$selectedOption."<br>";
if($selectedOption == 'one'){
$options[0] = "11";
echo $options[0];
}
if($selectedOption == 'two'){
$options[1] = "22";
echo $options[1];
}
if($selectedOption == 'three'){
$options[2] = "33";
echo $options[2];
}
Im mantaining your code but it can be improveed a lot. Im not sure why are you adding the values to an array ($options).
You should add the value attributes to your option elements
<option value="first">first<br /></option>
<option value="second">second<br /></option>
<option value="last">third</option>
You are not wrapping your logic inside of the foreach.
foreach ($_GET['taskOption'] as $selectedOption){
echo "selected: ".$selectedOption;
if($selectedOption === 'first'){
$options[0] = "11";
echo $options[0]."<br>";
}
elseif($selectedOption ==='second'){
$options[1] = "22";
echo $options[1]."<br>";
}
elseif($selectedOption ==='third'){
$options[2] = "33";
echo $options[2]."<br>";
}
echo"end";
}
You need to put the swirly brackets around your logic to make sure everything beneath runs.

HTML/PHP - form selection box greater than database value, increment by 500 in each selection value

I am trying to create something similar to an auction/bidding table/form.
If a value (current bid) is 1000, I am trying to create a selection box that allows another user to bid in intervals of 500, so for this example the outcome would be like:
Current Bid: 1000
Buy Now: 5000
<select class="form-control">
<option value="1500">1500</option>
<option value="2000">2000</option>
<option value="2500">2500</option>
<option value="3000">3000</option>
<option value="3500">3500</option>
<option value="4000">4000</option>
<option value="4500">4500</option>
</select>
Something like this:
<?php
$query = $db->query('SELECT * FROM auctions WHERE available = 1 LIMIT 1');
$num = $query->num_rows;
if($num > 0) {
echo '<select class="form-control">';
foreach($query as $row) {
$currentBid = $row['currentBid']; // 1000
$buyNow = $row['buyNow']; // 5000
$bids = ?? // this is where I am stuck, how can I make the difference between $currentBid and $buyNow show as options divided by 500's
echo '
<option value="'.$bids.'">'.$bids.'</option>
';
}
echo '</select>';
}
else {
echo "No auctions available";
}
?>
...
$currentBid = $row['currentBid'];
$buyNow = $row['buyNow'];
...
$options = '';
for($p = $currentBid + 500; $p <= $buyNow; $p += 500) {
$options .= '<option value="'.$p.'">'.$p.'</option>';
}

How to get the selected value from dropdown on post edit

I have the database for categories where i have id and name rows
<select name="category_id">
<option value="<?php if($category_id === 1) { echo 'selected';} ?>">Electronics</option>
<option value="<?php if($category_id === 2) { echo 'selected';} ?>">Automotive</option>
</select>
for some reason this does not show which one was selected when trying to edit the submitted post
and i do retrieve the category_id like so:
$res6 = mysql_query("SELECT * FROM `posts` WHERE `id` = '" . $id . "' LIMIT 1");
if(mysql_num_rows($res6) > 0){
while($row6 = mysql_fetch_assoc($res6)){
$id = $row6['id'];
$category_id = $row6['category_id'];
$price = $row6['price'];
$cover = $row6['cover'];
$title = $row6['title'];
$description = $row6['description'];
}
}
I think the option argument for selected is
<option selected="selected"> </option>
I believe MySQL results are always returned as strings, so $category_id === 1 will be false.
Either try $category_id === "1" or $category_id == 1
Also, you need to echo the selected="selected outside the value attribute
You can also try like this as well.
<select name="category_id">
<?php
$cats = array("1"=>"Electronics", "2"=>"Automotive");
foreach($cats as $k=>$cat){
if( $k == $category_id )
echo "<option value='{$k}' selected='selected'>{$cat}</options>";
else
echo "<option value='{$k}' >{$cat}</options>";
}
?>
</select>

AM / PM value not displaying correctly in Select attribute

I have a list of items, each with a date/time option. I need for each list item to have the correct time displayed for it.
But this doesn't seem to work. It will return the correct 'am' or 'pm' value for $duedateA, but when I try to set the selected attribute value is gets wonky and will either show the wrong am or pm, or both.
Am I doing something wrong?
if(!empty($resInvoice[0]["due_date"])){
$duedateA = date("a",strtotime($resInvoice[0]['due_date']));
}
if($duedateA == "am"){
$selected_am = 'selected="selected"';
}
if($duedateA == "pm"){
$selected_pm = 'selected="selected"';
}
<select name="due_time[a]">
<option value="am" '.$selected_am.'>am</option>
<option value="pm" '.$selected_pm.'>pm</option>
</select>
date_default_timezone_set('America/New_York');
$resInvoice[0]["due_date"] = '2012-10-26 03:21:44';
$selected_am = '';
$selected_pm = '';
$duedateA = '';
if(!empty($resInvoice[0]["due_date"])){
$duedateA = date("a",strtotime($resInvoice[0]["due_date"]));
}
if($duedateA == "am"){
$selected_am = ' selected="selected"';
}
if($duedateA == "pm"){
$selected_pm = ' selected="selected"';
}
echo '<select name="due_date[a]">
<option value="am"'.$selected_am.'>AM</option>
<option value="pm"'.$selected_pm.'>PM</option>
</select>';
sorry for all the variables, i have my errors set to strict.

Create dropdown from multi array + PHP class

Well, I am writing class that creates a DOB selection dropdown.
I am having figure out the dropdown(), it seems working but not exactly. Code just creates one drop down, and under this dropdown all day, month and year data are in one selection. like:
<label>
<sup>*</sup>DOB</label>
<select name="form_bod_year">
<option value=""/>
<option selected="" value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
<option value="5">6</option>
..
<option value="29">30</option>
<option value="30">31</option>
<option value="1">January</option>
<option value="2">February</option>
..
<option value="11">November</option>
<option value="12">December</option>
<option selected="" value="0">1910</option>
<option value="1">1911</option>
..
<option value="98">2008</option>
<option value="99">2009</option>
<option value="100">2010</option>
</select>
Here is my code, I wonder that why all datas are in one selection. It has to be tree selction - Day:Month:Year.
//dropdown connector
class DropDownConnector
{
var $dropDownsDatas;
var $field_label;
var $field_name;
var $locale;
function __construct($dropDownsDatas, $field_label, $field_name, $locale)
{
$this->dropDownsDatas = $dropDownsDatas;
$this->field_label = $field_label;
$this->field_name = $field_name;
$this->locale = $locale;
}
function getValue(){
return $_POST[$this->field_name];
}
function dropdown(){
$selectedVal = $this->getValue($this->field_name);
foreach($this->dropDownsDatas as $keys=>$values){
foreach ($values as $key=>$value){
$selected = ($key == $selectedVal ? "selected" : "" );
$options .= sprintf('%s',$key,$value);
};
};
return $select_start = "$this->field_desc".$options."";
}
function getLabel(){
$non_req = $this->getNotRequiredData();
$req = in_array($this->field_name, $non_req) ? '' : '*';
return $this->field_label ? $req . $this->field_label : '';
}
function __toString()
{
$id = $this->field_name;
$label = $this->getLabel();
$field = $this->dropdown();
return 'field_name.'">'.$label.''.$field.'';
}
}
function generateForm ($lang,$country_list,$states_list,$days_of_month,$month_list,$years){
$xx = array(
'form_bod_day' => $days_of_month,
'form_bod_month' => $month_list,
'form_bod_year' => $years);
echo $dropDownConnector = new DropDownConnector($xx,'DOB','bod','en-US');
}
// Call php class to use class on external functionss.
$avInq = new formGenerator;
$lang='en-US';
echo generateForm ($lang,$country_list,$states_list,$days_of_month,$month_list,$years);
While this might or might not work for you, I would suggest taking a look at the Jquery UI Datepicker. It provides a very nice and highly customizable calendar that might be able to save you a lot of time.

Categories