HTML Form Select default value from PHP Echo _Post - php

I have a html form where people select to filter the mysql results, example
<select name="display">
<option value="9999" selected>Display all results</option>
<option value="10">10 results only</option>
<option value="20">20 results only</option>
<option value="50">50 results only</option>
<option value="100">100 results only</option>
</select>
But if people select lets say 50 results per page, after form submit I want the option 50 results only to get marked as selected like this
<option value="50" selected>50 results only</option>
The question is How to do this using PHP POST or GET from the form

As I said in comments, assign a variable to a POST array and set it in your LIMIT in the query.
// you can also use isset instead of empty here and GET instead of POST
if(!empty($_POST['display'])) {
$limit = (int)$_POST['display']; // ensure the value is an integer
}
Then place the $limit variable in your query for LIMIT.
I.e.: (and as basic MySQL)
SELECT col1, col2, col3 FROM TABLE WHERE col_x = 'xxx' LIMIT $limit
The question though is a bit unclear so there isn't much else I can add to this.
Sidenote: LIMIT (with SELECT) accepts additional parameters. I.e.: LIMIT 0, 50
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
http://dev.mysql.com/doc/refman/5.7/en/select.html
Edit:
"after form submit I want the option 50 results only to get marked as selected"
In order to keep the value selected, use a conditional statement in the select's options and check if it equals to something:
Sidenote: This will only work if your entire code is inside the same file.
<?php
if(!empty($_POST['display'])) {
$limit = $_POST['display'];
$selected = 'selected';
}
?>
<form action="" method="post">
<select name="display">
<option value="9999" <?php if(isset($selected) && $limit==9999 ) {echo $selected; } ?>>Display all results</option>
<option value="10" <?php if(isset($selected) && $limit==10) {echo $selected; } ?>>10 results only</option>
<option value="20" <?php if(isset($selected) && $limit==20){echo $selected; } ?>>20 results only</option>
<option value="50" <?php if(isset($selected) && $limit==50){echo $selected; } ?>>50 results only</option>
<option value="100" <?php if(isset($selected) && $limit==100){echo $selected; } ?>>100 results only</option>
</select>
<input type="submit" value="submit">
</form>
You can add in the rest of your code.

Related

How to $_GET values from sql to be inserted in <select type> PHP

I'm using xampp PHP
How to do I get values from MySQL in select type like getting value in
<input type="text" value="<?php $variable['valueinsql']; ?>">
But I want the value to be inserted in
<select type>
And I tried
If(isset($_GET['id'])){
<p> Banks:</p><select multiple="multiple" name="otherbanks[]" style=" width:190;" value = "<?php $list["Banks"] ?>"">
<option value="Banksone">One</option>
<option value="Bankstwo">two</option>
<option value="Bankthree">three</option>
I want to see it automatically highlighted.
Use in_array to check the value available in array or not. If available echo 'selected';
Try this ==>
<select multiple="multiple" name="otherbanks[]" style=" width:190;">
<option <?php if (in_array('Banksone',$list["Banks"])) {echo 'selected';} ?> value="Banksone">One</option>
<option <?php if (in_array('Bankstwo',$list["Banks"])) {echo 'selected';} ?> value="Bankstwo">two</option>
<option <?php if (in_array('Bankthree',$list["Banks"])) {echo 'selected';} ?> value="Bankthree">three</option>
</select>
To highlight particular options the selected attribute should be set on each option that is applicable - as below example.
<select name='otherbanks[]' multiple=true>
<option value=1 selected>one
<option value=2>two
<option value=3 selected>three
<option value=4>four
<option value=5 selected>five
</select>
Presumably you generate the select menu using PHP and a query to the database so to accomplish that ( highlighting ) in PHP you would need a loop to iterate through each value in $list["Banks"] and check the value against the value from sql. Without knowing exactly how you generate the select menu or knowing the value of $list['Banks'] it is hard to know exactly and I may be "barking up the wrong tree" as it were.

Form selector failing $_POST['medalCount'] !== '' test

In my post form, I have a selector for users to select the number of medals. In my getSearchFields() function, if the selector isn't empty (aka the selector is not on Select Medal Count), the function completes task 1. However, the selector is failing the $_POST['medalCount']!== '' test and is doing task 1. $_POST['medalCount'] is type string. How do I fix this?
<form action="index.php" method="post" name="search">
<select name="medalCount">
<option value=0 selected> Select Medal Count </option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
</select>
</form>
In index.php...
function getSearchFields(){
if($_POST['medalCount']!== ''){
//Task 1
$search_fields['medalCount'] =$_POST['medalCount'];
}
}
Change this
<option value=0 selected> Select Medal Count </option>
to this
<option value="" selected> Select Medal Count </option>
With all <option> elements having non-empty value attributes, you would never (normally) receive an empty string value in $_POST['medalCount'] and $_POST['medalCount']!== '' would always be true.
This line is wrong $_POST['medalCount']!== '', You need to try $_POST['medalCount']!= 0. As your selectors empty value is 0.

Filter search based on selected drop downs in PHP

I have a Search form to search products.
<form method="get" action="search.php">
<select name="minprice">
<option value="">Min price</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
<option value="500">500</option>
</select>
<select name="maxprice">
<option value="">Max price</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
<option value="500">500</option>
</select>
<select name="type">
<option value="">type</option>
<option value="electric">electric</option>
<option value="other">other</option>
</select>
<select name="brand">
<option value="">type</option>
<option value="asus">ASUS</option>
<option value="ms">MS</option>
<option value="other">other</option>
</select>
<input type="submit" name="search" value="search">
</form>
In search.php page, i need results based only on what selected.
If i selected single list, i need that result only.
Example : I selected min price=100 , then i need to select products with minimum price 100 only...
I have tried this method , but it is not easy in my case, i am sure there is another way to implement this.
Any experts??
Any help / Ideas greatly appreciated..
EDIT
if(isset($_GET['minprice'])
{
$minprice=$_GET['minprice'];
$query="SELECT * FROM products WHERE price>='$minprice'";
}
else if()....
{ }
I am currently doing this way, But on next conditions not taking...
My Questions:
Do only this way to get the results?
Any change needed on $_GET[''] method like !="" or empty().
First, change your form from get to post. While users can manipulate POST, it's far easier to manipulate GET.
<form method="post" action="search.php">
Second, evaluate all of the POST variables first, and then compose your database query.
$minprice = isset($_POST['minprice']) ? $_POST['minprice'] : null;
$maxprice = isset($_POST['maxprice']) ? $_POST['maxprice'] : null;
$type = isset($_POST['type']) ? $_POST['type'] : null;
$brand = isset($_POST['brand']) ? $_POST['brand'] : null;
NOTE: I'm not sure what database type you're using, but you should use prepared statements with these variables when building your query. It's also a good idea to limit the possible values in your variables -- so, for example, if you expect price to always be a decimal number, you should cast the string to float.
You can then use something like the following for your query. I'm going to assume you're using a mysql database.
"SELECT <columns> FROM products WHERE minprice >= ISNULL(<$minprice variable>,minprice) AND maxprice <= ISNULL(<$maxprice variable>,maxprice) AND type = ISNULL(<$type variable>,type) AND brand = ISNULL(<$brand variable>,brand)"
NOTE: the query above needs to be changed to handle parameter binding / prepared statements. Since I don't know your database flavor, I've given you the basic query structure. Also, you should SELECT the specific columns you need from your table, not SELECT * (which is a performance hit).

passing values to sql through check boxes and dropdowns

How can i pass Drop down values to sql database and also the check box for example if a user selects English and maths than the value inserted in to the database would be 1 or else the value would be 0
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<input type="checkbox" name="Math" value="Math">Math<br>
<input type="checkbox" name="English" value="English">English<br>
<input type="checkbox" name="HealthScience" value="HealthScience">Health Science<br>
<input class="sub_reg" type="submit" value="Register Subjects" />
</form>
this is how my database looks like
First, your <select id="year_sel"> needs a name attribute to post ->
<select id="year_sel" name="year_sel" >
Since you are using <form> the default is get, so you would get the selected value in $_GET
$year_sel = $_GET['year_sel'];
If you changed it to
<form method="post">
then you would get it in $_POST
$year_sel = $_POST['year_sel']
Second, checkboxes are only posted if checked, so you can use isset() to set a value using a ternary -
$math = isset($_GET['Math']) ? 1 : 0;
$english = isset($_GET['English']) ? 1 : 0;
...[rest of your checkboxes]...
swap $_GET/$_POST like the select
$math = isset($_POST['Math']) ? 1 : 0;
$english = isset($_POST['English']) ? 1 : 0;
<select id="year_sel" name="year_sel">
<option value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
on your form action something.php file use this to get the select value
$language = $_POST["year_sel"]; // chose whetever your form method
establish the database connection please do refer some tutorials (if you don't know )
write the mysqli insert command like
$SQL = "INSERT INTO yourtable (language) VALUES ('$language')";
mysqli_real_escape_string($sql);
$result = mysqli_query($sql) or die (mysqli_error());
now you can insert this value in your mysql or any database table
this is not tested
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel" name="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<?php
$variable = $_POST["year_sel"];
?>
Should mention the HTML ELEMENT name to get the value.

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

Categories