Sql and php if not set - php

I have a scrolling list with different names which i can choose from but if nothing is selected want all of them to be selected.
html:
<form action="" method="POST">
<select name="names">
<option selected disabled value="">Choose name</option>
<option value="name1">Name 1</option>
<option value="name2">Name 2</option>
</select>
<button type="submit" name="submit">OK</button>
</form>
php:
if(!isset($_POST['names'])) {
*what to put here if i want all if not set*
}
Sql:
WHERE name = '"($_POST['names'].'"

Put the WHERE clause in a variable. If there are no names, make it empty.
if (isset($_POST['names'])) {
$where = "WHERE name = '" . $_POST['names'] . "'";
} else {
$where = "";
}
$sql = "SELECT * FROM yourTable " . $where;
However, when a form has a <select> element, its value will always be sent in the POST, so isset() will always be true. Perhaps you should be checking if (!empty($_POST['names'])); if you have an empty default option, this will test if that option is selected.

Related

MYSQL SELECT with user selected OPTION values

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 ";
?>

Set a PHP variable based on first option in a form?

I have a form that has a list of school courses. I have a PHP variable that is passed to an SQL statement based on the value of whatever option is selected. My issue is that when the page first loads, the variable has no value. Ideally, I'd like to be able to set the variable to whatever the first option in the form is.
Form:
<form method="post">
<select name="courseID">
<option value = "204" selected>Course 1</option>
<option value = "205"> Course 2 </option>
<option value ="206"> Course 3 </option>
</select>
<input id="button" type="submit" value = "Go" />
</form>
PHP & SQL
//get posted value from form - returns false if no value is selected
$course_id = $_POST['courseID'];
//prepared statement
//test query "SELECT * FROM [database] WHERE course_id = ?";
$sql = "SELECT A.title as course_title, B.body, C.title
FROM db.course as A,
db.objective as B,
db.competency as C,
db.course_objective_competency as D
WHERE
A.course_id = ? AND
A.course_id = D.course_id AND
B.objective_id = D.objective_id AND
C.competency_id = D.competency_id";
//prepare the query
$q = $con->prepare($sql);
//bind parameter to query (index, variable, type)
$q->bindParam(1,$course_id,PDO::PARAM_INT);
//execute the statement
$q->execute();
You can always do it like this:
$course_id = $_POST['courseID'] ?: 204;
if php code is in a separate file you have to add it to the action attribute for the form. Otherwise the values will never be sent to it
<form method="post" action='yourPhpFile.php'>
<select name="courseID">
<option value = "204" selected>Course 1</option>
<option value = "205"> Course 2 </option>
<option value ="206"> Course 3 </option>
</select>
<input id="button" type="submit" value = "Go" />
</form>

Select from drop-down menu and reload page

I've got a table that populates data from a MYSQL database and populates a drop-down menu from the same database. I have the drop down menu and table just fine, I would like to be able to choose which data I show in the table however.
<select name = 'peer-id' method='post' style = 'position: relative'>
<?php
while ($content = mysql_fetch_array($peer)) {
echo "<option value='" . $content['Peer'] . "'>" . $content['Peer'] . "</option>";
}
$results = mysql_query("SELECT Destination FROM rate ");
?>
</select>
That's what I have for the select box. How can I get the choice from that and save that as a variable and refresh the table data?
I need to clarify that this will change that current data
#Data#Data#Data
#Data#Data#Data
#Data#Data#Data
Then choose drop down choice and I want it to show new data
#Data2#Data2#Data2
#Data2#Data2#Data2
#Data2#Data2#Data2
So it's going to need to load a new page or refresh some how because it's changing via PHP and not javascript.
I think form may be better, for example
<form id="myform" method="post">
<select name = 'peer-id' style = 'position: relative' onchange="change()">
<option value="1">12</option>
<option value="2">15</option>
<option value="3">16</option>
<option value="4">18</option>
</select>
</form>
<script>
function change(){
document.getElementById("myform").submit();
}
</script>
In the above code, whenever you change the value of select, it will post to the backend, then according to the posted value, you can do want you want, to get the peer-id in php, you can use the following code
$peer-id = $_POST['peer-id'];
Hope helps!
apply this code in select tag hope this works
<select onchange="location = this.options[this.selectedIndex].value;" style="text-decoration:none;">
<option value="customers.php"></font></option>
</select>
insted of the static options, you can do it like this :) here you get all the options from the database. Just replace it with the static options
$peer = mysql_query("SELECT Peer FROM rate Group By Peer Where peer = 'variable'");
$result_peer = mysql_query($peer);
if($result_peer){
while($row_peer = mysql_fetch_array($result_peer)){
echo'<option value='.$row_peer['Peer'].'>'.$row_peer['Peer'].'</option>';
}
I agree in using form, and with this you can echo back onto the page with a submit button (code tested):
<form id="myForm" method="POST">
<select name="select" onchange="<?php echo $_SERVER['PHP_SELF'];?>">
<option value="N">No</option>
<option value="Y">Yes</option>
</select>
<input type="submit" name="formSubmit" value="Submit" >
</form>
<?php
if(isset($_POST['formSubmit']) ){
$var = $_POST['select'];
$query = "SELECT * FROM table_name WHERE DesiredField='$var'";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$var2 = $row['FieldName'];
echo "First field: " . $var2 . "<br>";
// and so on for what you want to echo out
}
}
?>

Query Select Sql with Condition from a Value of Html combobox

I have a problem of my web programming using php. I have an input form where there are two combobox with several options.
In the form, there also a textfield of Code where the value is determined by what is selected from both combobox (as the condition) from mySql database using select query.
Like= Select code from tableA where condition1=itemComboA and condition2=itemComboB;
I wanna ask, how to retrieve the value of the textfield automatically when user select an item in both combobox?
The query above still not working, then I manage it to select the code in another page then I redirect it to the input form by using link with the Code Like this: http://localhost/data/input.php?code=6
I still can't Set the value='<?php echo $code ?>;' in the value of textfield because the select query isn't working.
Your html will look like this
<form action="input.php" action="GET">
<select name="code">
<option value="1"> option 1</option>
<option value="2"> option 2</option>
<option value="3"> option 3</option>
</select>
<select name="code2">
<option value="drp1"> option 1</option>
<option value="dpr2"> option 2</option>
<option value="drp3"> option 3</option>
</select>
<input type="submit" value="Submit" />
</form>
for testing purposes, in your php file, print this to show content of your array
print_r($_GET);
from there you can get the values to put into your mysql query
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_GET['code1']) . ' AND condition2 = ' mysql_real_escape_string($_GET['code2']) . ';';
At the top of your php file you can have:
<?php
if(!empty($_POST) {
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_POST['itemComboA']) . ' AND condition2 = ' mysql_real_escape_string($_POST['itemComboB']) . ';';
}
?>
It seems as though your form doesn't have method="post" defined so you may need use $_GET instead of $_POST.

differentiating identical select names

how can I differentiate the 'select name' of 'f1' and 'f2' currently both named 'subcat' while still having only 1 subcat variable? this code works accurately only if cat value=2, if cat value=1 then subcat value always =0
<?php
$cat=$_POST['cat'];
$subcat = $_POST['subcat'];
?>
<form action='submitsite.php' method='POST'>
<table>
<tr>
<td>category(optional)</td>
<td>
<select name='cat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "1">music </option>
<option value = "2">film </option>
</select>
<div id = "f1" style="display:none">
<select name='subcat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "3">pop</option>
<option value = "4">rock </option>
</select>
</div>
<div id = "f2" style="display:none">
<select name='subcat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "5">comedy</option>
<option value = "6">drama</option>
</select>
</div>
</form>
</div>
<script type = "text/javascript">
function showForm(){
var selopt = document.getElementById("opts").value;
if (selopt == 1) {
document.getElementById("f1").style.display="block";
document.getElementById("f2").style.display="none";
}
if (selopt == 2) {
document.getElementById("f2").style.display="block";
document.getElementById("f1").style.display="none";
}
}
</script>
Add [] to the name. The selects will then be interpreted as an array when submitted.
<select name="subcat[]">...</select>
With PHP it can be accessed (if POSTed) like this:
<?php
$subCatArr = $_POST['subcat'];
$firstIndex = $subCatArr[0];
$secondIndex = $subCatArr[1];
Oh yeah, and reusing IDs in HTML is not valid. They must be unique.
UPDATE After better understanding OP's intent:
If I understand the intent of this spaghetti code (that's a term of endearment, OP), the user may select one category and one subcategory, which is based on the category that he/she selected. Then the user submits the form and that selection is recorded in the database.
First of all, let's get rid of the use of table elements, because they're unnecessary. Secondly, you only need one select for the subcategory.
<form action='submitsite.php' method='POST'>
<label>category(optional)</label>
<select name='cat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "1">music </option>
<option value = "2">film </option>
</select>
<div id="subcatDiv" style="display:none;">
<select name='subcat' id='opts'></select>
</div>
.
.
.
<input type='submit' />
</form>
We can leave it blank, since it's not being displayed anyway.
Now, when the user makes a change, we'll either display the appropriate subcategories, or we'll just hide the div again:
<script type = "text/javascript">
function showForm(){
var selopt = document.getElementById("opts");
var seloptVal = selOpt.value;
var subcatDiv = document.getElementById("subcatDiv");
var options = "";
switch(seloptVal * 1) {
case 1:
options = "<option value='0'>Select</option>" +
"<option value='3'>pop</option>" +
"<option value='4'>rock</option>";
break;
case 2:
options = "<option value='0'>Select</option>" +
"<option value='5'>comedy</option>" +
"<option value='6'>drama</option>";
break;
default:
break;
}
if (options == "") {
subcatDiv.style.display = "none";
selopt.innerHTML = options;
} else {
selopt.innerHTML = options;
subcatDiv.style.display = "block";
}
}
</script>
Now you only have to deal with one select for the subcategory.
I have altered a lot of your code to make it valid html and also work as i "think" you want it as your question did not make it very clear.
Here is a list of amends I had to make:
Changed ID's to make it clear what they are for.
Removed duplicate ID's.
Closed off your table properly.
Completely changed your JavaScript to show and hide certain select's
Many more things that I have forgotten.
Please see this jsfiddle.
Fixed html:
<form action='submitsite.php' method='POST'>
<table>
<tr>
<td>category(optional)</td>
<td>
<select name="cat" id="selectType">
<option value="0">Select</option>
<option value="1">music</option>
<option value="2">film</option>
</select>
<div id="f1" style="display:none">
<select name='music' id="selectMusic">
<option value="0">Select</option>
<option value="3">pop</option>
<option value="4">rock</option>
</select>
</div>
<div id="f2" style="display:none">
<select name="type" id="selectFilm">
<option value="0">Select</option>
<option value="5">comedy</option>
<option value="6">drama</option>
</select>
</div>
</td>
</tr>
</table>
</form>
Fixed JavaScript:
$("#selectType").change(function() {
var selected = $(this).val();
$("#f1, #f2").hide();
switch (selected) {
case "1":
$("#f1").show();
break;
case "2":
$("#f2").show();
break;
}
});​​
document.forms[0].subcat will be an array.
document.forms[0].subcat[0] will be the first instance, etc.
p.s. IDs are supposed to be unique.
document.getElementById('f2').getElementsByTagName('select')[0]
You are violating HTML standards by reusing IDs, by the way.

Categories