MYSQL SELECT with user selected OPTION values - php

I am trying to filter out a MYSQL table according to user selected values in my php file list.php
My table users have field stdYr, in which elements have values of S00, S01, ... S40.
in html body, I have this chunk of code:
<form method="post" role="form" action="list.php">
<div>
<select multiple>
<option value="all">Select</option>
<option value="S00">74</option>
<option value="S01">75</option>
<option value="S02">76</option>
...
<option value="S40">114/option>
</select>
<button type="submit"></button>
</div>
</form>
And then, I want to select those with selected stdYr values.
The approach I could come up with is passing the values of option through $_POST:
if(isset($_POST['S00']) || isset($_POST['S11']) || ...
... || isset($_POST['S40'])) {
/*some code to get only those set values*/
$query="SELECT * FROM users WHERE stdYR = '/*some stuff*/'";
}
But with different possible selections of values, I couldn't figure out how exactly to do this...
And even with the solution, I see this approach very repetitive and silly.
How am I supposed to deal with such things?
I greatly appreciate your help! Thanks :D

Your form is incorrect. Your select MUST have a name attribute for it to get submitted to the server.
<select name="options[]" multiple>
^^^^^^^^^^^^^^^
<option ...>
...
</select>
Note the [] on the name field. That'll tell PHP to accept multiple values, making $_POST['options'] an array itself. Then it's a simple matter of:
foreach($_POST['options'] as $option) {
insert_into_db($option);
}

Use this way:
<form method="post" role="form" action="list.php">
<div>
<select name="MyOptions[]" multiple>
<option value="all">Select</option>
<option value="S00">74</option>
<option value="S01">75</option>
<option value="S02">76</option>
...
<option value="S40">114/option>
</select>
<button type="submit"></button>
</div>
</form>
print '<pre>';
print_r($_POST);
$selectOption = $_POST['MyOptions']; //get post result and then work as you wish

You could try this :
<select name="stdYR[]" multiple>
<?php
$stdYR = $_POST['stdYR'];
$stdYR_ct = count($stdYR);
$i=0;
while($i < $stdYR_ct){
$stdYR_SQL .= "'$stdYR[$i]',";
$i++;
}
if (preg_grep("/All/i", $stdYR) or ($stdYR_ct < 1)){
$stdYR_SQL = "";
}else{
$stdYR_SQL = eregi_replace(",$",'',$stdYR_SQL);
$stdYR_SQL = " AND stdYR IN($stdYR_SQL)";
}
$query = "SELECT * FROM users WHERE 1=1 $stdYR_SQL ";
?>

Related

multiple select value foreach in php

Ok i have the problem! :)
I have a two same name select in my page ( if statement handling ) and i forgot add value options for one.... my bad :) Thanks for replys!
I have a multiple selection in my page:
<select multiple="multiple" name="diff[]">
<option value="1">Easy</option>
<option value="2">Medium</option>
<option value="3">Hard</option>
</select>
And i want to get the option-s value so:
foreach($_POST['diff'] as $key) {
echo $key;
}
I want to option values but i get this values -> Easy, Medium, Hard.
How i can acces the value="" attribute ?
Thanks dudes!
Your code should work, I guess the problem is how you post it.
Here is a little sample code, tested on my server. It gives the desierd ouput:
Edited Code:
<form class="form-inline" method="POST" action="">
<select multiple="multiple" name="diff[]">
<option value="1">Easy</option>
<option value="2">Medium</option>
<option value="3">Hard</option>
</select>
<button type="submit" class="button" name="mentes" id="mentes" title="Mentés az adatbázisba">Send</button>
</form>
<?php
if(!empty($_POST["diff"])) {
foreach($_POST['diff'] as $key) {
echo $key;
}
}
?>
When I select "easy" and press "Go" -> Output: 1
This works for all the other inputs as well.

set values as a variable of select tag in php

I want to set both values as variable. Means if I want to use sales or training as a variable and if I want to use its value anywhere then I can use it.
<form action="code.php" method="post">
<select name="department">
<option>Please Choose Department</option>
<option value="sales">Sales</option>
<option value="training">Training</option>
</select>
<input type="submit" value="submit">
</form>
I am trying by below code but I can't.
code1.php
<?php
$stud = explode("_", $_POST['department']);
$sales = $stud[0];
$training = $stud[1];
echo "$sales";
echo "$training";
?>
$sales and $training are not working as variable.
Please help.
Probably you are looking for this:
<form action="code.php" method="post">
<select name="department[]" multiple>
<option>Please Choose Department</option>
<option value="sales">Sales</option>
<option value="training">Training</option>
</select>
<input type="submit" value="submit">
</form>
<?php
$sales = $_POST['department'][0];
$training = $_POST['department'][1];
echo $sales;
echo $training;
?>
select must be defined as multiple select and as array in name.
Update
<select name="department">
With
<select name="department[]" multiple>
PHP Part
<?php
$stud = $_POST['department'];
extract($stud);
echo $sales;
echo $training;
?>
Explanation:
We have used multiple attribute of <select> so that multiple options can be selected.
If we use multiple as select, the value getting posted as array. So, no need to explode it.
Then we extract() ed the posted array to get two desired elements.

Calculate total price of two books using PHP and HTML

I want to know what can be the way using php and html such that I am provided a dropdown of books, I need to select exactly two choices out of that drop down and then calculate sum of price of both the books.
Assuming I had hardcoded the Books say:
Book 1 - $5
Book 2 - $15
Book 3 - $50
I know with if it was to select only one book. But no idea for this one. Please help
Code :
<?php
if(isset($_POST['formSubmit']))
{
$varCurrentBook = $_POST['formBook'];
$errorMessage = "";
if(empty($varCurrentBook))
{
$errorMessage = "<li>You forgot to select a Book!</li>";
}
if($errorMessage != "")
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
else
{
switch($varCurrentBook)
{
//Can use here to find what option is clicked
}
exit();
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<label for='formBook'>Select a Book</label><br>
<select name="formBook">
<option value="0">Select a Book...</option>
<option value="15">The Secret</option>
<option value="10">The Fairy Tales</option>
<option value="5">All about words</option>
<option value="100">Pinaacle Studio</option>
<option value="120">Harry Potter</option>
<option value="200">Thinking in Java</option>
</select>
<input type="submit" name="formSubmit" value="Submit" />
</form>
You need to use a multiple select as:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<label for='formBook'>Select a Book</label><br>
<select name="formBook[]" multiple><!--The multiple attribute-->
<option value="0">Select a Book...</option>
<option value="15">The Secret</option>
<option value="10">The Fairy Tales</option>
<option value="5">All about words</option>
<option value="100">Pinaacle Studio</option>
<option value="120">Harry Potter</option>
<option value="200">Thinking in Java</option>
</select>
<input type="submit" name="formSubmit" value="Submit" />
</form>
Also note how I changed name from formBook to formBook[]. This changes $_POST['formBook'] to an array of options.
Then you can access it as:
<?php
foreach ($_POST['formBook'] as $names)
{
print "You have selected $names<br/>";
/*Your also need to change your code here accordingly.*/
}
?>
You have to use multiple select with jquery
see: HTML Multiselect Limit
<select name="formBook">
have to become:
<select name="formBook[]" multiple">
Then you can sum all selected elements
if(isset($_POST['formSubmit']))
{
$sum = array_sum($_POST['formBook']); // Sum of all selected elements
...
}
As ARBY answered correct you have to add the multiple attribute to your select element:
<select name="formBook" multiple>
This sends the value attribute. (E.g. 0 / 15 / 10)
So you can check if you have exactly two books like this:
//Check if the user has selected exactly two books
$formBooks = $_POST['formBook'];
if(count($formBooks) !== 2){
//TODO: return error message that user has to select two books
}
//Calculate the value
$result = array_sum($formBooks);
//TODO: return value

Using <select> tag to update database in PHP with a submit button

I tried to find ways how to update my database for hours now and it's hurting my brain.
Please, can anyone show me how to update data inside a database by using <select>?
If I select 'Checked-Out' in the form and then click update the StatusID in the database will be updated to '2' but I just can't seem to work it.
<?php
include 'connection.php';
if(isset($_POST['submit'])){
$_var1 = $_POST['status'];
$query3 = mysql_query("UPDATE reservation SET ReservationStatusID = '$_var1' WHERE `ReservationID` = '$id' ");
if($query3)
{
header('location:viewReservedroomsGuest1.php');
}
else
{
echo "Error";
}
}
?>
Here's my select.
<select id="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
I'm sorry, I don't make any sense. My brain is tired out already.
<form action="sampleupdate.php" method="post">
<select id="status" name="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
</form>
in sampleupdate.php
$val = $_POST['status'];
// and write and execute the update Query here..
Put the select in a <form> with method="post"
Give the select a name attribute
Read the data from $_POST['the name you used']
Use the database API of your choice

Filling a list box programmatically with php

This is my list box sample code
<form id="myForm" action="somePage.php" method="post">
<p>myList<br>
<select name="select">
<option value="Option 1" selected>---</option>
<option value="Option 2">sel1</option>
<option value="Option 3">sel2</option>
<option value="Option 4">sel3</option>
</select>
</p>
</form>
But the problem is that i would like to fill the list with the result of a query. Leaving out for a moment the query, the real point is "How can i print the <option value= ..." programmatically so that with a for cycle i can fulfill the list? For example i thought something like this
<form id="myForm" action="somePage.php" method="post">
<p>myList<br>
<select name="select">
<?php
for(i;i < myQueryResultArray length;i++){
$counter = i;
echo <option value="Option $counter">$myArrayValue[i]</option>
}
?>
</select>
</p>
</form>
This is for sure wrong but that's the idea i had. It may be correct with proper syntax? Or better other ways? Thanks
You'd want a for loop like this
for($i = 0; $i < sizeof($myQueryResultArray);$i++){ //note the changes here
//declare variables with $
//sizeof() will return length
echo "<option value='Option $i'>$myArrayValue[$i]</option>"; //notice the quotes
}
$res = $db->query($query);
foreach($res as $item) {
?>
<option value = "<?=$item['key1']?>"><?=$item['key2'] ?></option>
<?php
}

Categories