I want to get the value of the option using PHP, and depending on the chosen option i want the page to display different results depending on what they choose.
I do not have a submit button as i want the data to change when the user clicks on an option from the drop down list.
Here is my form:
<form action="#" name="viewall" id="viewall" method="post">
<label for="date" id="date">Select a decade:</label>
<select name="date">
<option value="all" id="all">View All</option>
<option value="1920" id="1920">1920's</option>
<option value="1930" id="1930">1930's</option>
<option value="1940" id="1940">1940's</option>
<option value="1950" id="1950">1950's</option>
<option value="1960" id="1960">1960's</option>
<option value="1970" id="1970">1970's</option>
<option value="1980" id="1980">1980's</option>
<option value="1990" id="1990">1990's</option>
</select>
And i would like if the user picks 1920's that it only shows records in that decade, if the user doesn't pick anything it will automatically show all records.
I have been trying to use the following code and kind of got it working using submit button, but it would only show either 1920's or 1970's and wouldn't change.
if(isset($_POST['submit'])){
//sel_val stores selected value in a variable
//mysqli_real_escape_string() function escapes special characters in a string for use in an SQL statement.
$sel_val = mysqli_real_escape_string($con, $_POST['1920']);
$sql = mysqli_query($con, "SELECT * FROM details
WHERE year < '1930' AND year > '1919' ORDER BY year ASC;");
}
elseif(isset($_POST['submit'])){
//sel_val stores selected value in a variable
//mysqli_real_escape_string() function escapes special characters in a string for use in an SQL statement.
$sel_val = mysqli_real_escape_string($con, $_POST['1970']);
$sql = mysqli_query($con, "SELECT * FROM details
WHERE year < '1980' AND year > '1969' ORDER BY year ASC;");
}
else
{
$sel_val = mysqli_real_escape_string($con, $_POST['all']);
$sql = mysqli_query($con, "SELECT * FROM details ORDER BY year ASC;") or die(mysqli_error($con));
}
Any suggestions with or without a submit button would help.
if (isset($_POST['submit'])) {
if (is_numeric($_POST['date']) {
$sel_val = mysqli_real_escape_string($con, $_POST['date']);
$sql = mysqli_query($con, "SELECT * FROM details WHERE year = " . $sel_val);
} elseif ($_POST['date'] === 'all') {
$sql = mysqli_query($con, "SELECT * FROM details ORDER BY year ASC");
}
} else {
$sql = mysqli_query($con, "SELECT * FROM details ORDER BY year ASC;") or die(mysqli_error($con));
}
The reason you only get 20's or 70's is because that is all that is coded in the case statement. If those actually worked, you just need to code a few more conditions. Change the date values by 10 years for each until you get all the decades.
You will need AJAX to do this without a submit. To learn more about AJAX please see jQuery's AJAX or if you prefer Javascript and Mozilla Documentation You can also search on the numerous posts on StackOverflow to learn what other users have gone through!
Related
I have a form with a select field:
<select class="indexSearchLocationList" name="locationList">
<option value="allLocations">Anywhere in London</option>
<option value="barking_and_dagenham">Barking and Dagenham </option>
<option value="barnet">Barnet</option>
What im trying to do is if the user chooses the option allocations (Anywhere in London) then I need to use that to select all when querying the database below is the php I currently have but how do I specifiy if that particular option is choosen:
$choosenLocation = $_POST['locationList'];
Just try this, if it is not meet your requirement, then add your requirement briefly as comment.
$choosenLocation = $_POST['locationList'];
if($choosenLocation == 'allLocations')
{
$query = "select * from TABLE ";
}
else
{
$query = "select * from TABLE WHERE condition="any_condition";
}
I think that may be
if(isset($_POST['locationList']) & $_POST['locationList']=='allLocations')
{
$query = "select * from TABLE ";
//based on the mysqli_ or PDO you execute
//fetch the row and display accordingly
}
Do you expect something similar,
$choosenLocation = $_POST['locationList'];
if(isset($choosenLocation) && $choosenLocation == 'allLocations'){
$query = "select * from TABLE ";
//based on the mysqli_ or PDO you execute
//fetch the row and display accordingly
}else{
$query = "select * from TABLE WHERE condition='your condition here' " ;
}
We are having troubles with our php session.
Trying to add a patient to a database, linked to the doctor that is curing the patient.
We have a select from the existing doctors in the database. They show up in the dropdown list.
But when we are trying to send the selected doctor to the database (the php session), there is no addition to the database. All the other inputs (patient name, patient birth date, etc) are put in the database, except the data from the dropdown list.
Add_patient.php
Doctor:<br>
<select name="doctor">
<option value="">--Select--</option>
<?php
$config = parse_ini_file("divkey.ini.php", true);
include("connect/connect_mysql.php");
$opdracht = "SELECT * FROM gebruiker ORDER BY id";
$resultaat = mysql_query($opdracht);
while ($rij = mysql_fetch_array($resultaat)) {
$id = $rij['id'];
$name = $rij['name'];
$fname = $rij['fname'];
?>
<option value ="<?php $id;?>"><?php echo"$name $fname" ?></option>
<?php
} ?>
</select>
Session_add.php
$doctor = $_POST['doctor'];
# query
Our query from session_add.php works. Just the not for the $_POST['doctor'].
# query
$opdracht = "INSERT INTO patient ( `name`, `fname`, `geslacht`, `doctor`, `straatnaam`, `huisnummer`, `postcode`, `gemeente`, `telefoonnummer`, `patientnummer`, `land`, `bloedgroep`, `gsmnummer`, `geboortedatum`, `geboorteplaats`, `taal`, `nationaliteit`, `rijksregisternummer`, `huisarts` )
VALUES ('".$name."', '".$fname."', '".$geslacht."', '".$doctor."','".$straatnaam."' ,'".$huisnummer."' , '".$postcode."' , '".$gemeente."', '".$telefoonnummer."',
'".$patientnummer."','".$land."', '".$bloedgroep."', '".$gsmnummer."', '".$geboortedatum."', '".$geboorteplaats."', '".$taal."', '".$nationaliteit."','".$rijksregisternummer."', '".$huisarts."')";
# other values are not important, it's in Dutch and these values are sent to the database
# doing query
$result = mysql_query($opdracht) or die(mysql_error());
# we use or die(mysql_error())
The query is executed, a 0 (zero) is added to the database instead of the selected doctor.
<option value ="<?php echo $id;?>"><?php echo $name.$fname; ?></option>
Try this.
We found the problem, it was in the sql query. With var_dump($_POST)
I checked the values that were sent to the session, these were the right ones.
In the SQL query, there was '".$doctor."'
This was wrong, it should be '.$doctor.' (recognised as id)
Thanks for the help!
I have a drop downmenu on a page, after users add a content to the db,
i do not want the specific value that was added
from the dorpdown menu to show in the list again.
I do not want to delete that specific value from the dropdown table.
Your help will do.
Here is my code below:
<?php
$query = "SELECT * FROM vreg_no order by vreg desc";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{{
$_SESSION['svregx'] = $row['vreg'];
}}
?>
<select name="svreg" class="bodytxt" id="svreg">
<option>Select Vehicle #</option>
<?php
$query = "SELECT * FROM vreg_no order by vreg desc";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{{
$vreg = $row['vreg'];
if($_SESSION['svregx'] == $vreg){
//do nothing
}
elseif($_SESSION['svregx'] != $vreg){
echo"<option value='$vreg'>$vreg</option>";
}else{}
}}
?>
</select>
You are executing the same query twice.
The first one should be something like:
$query = "SELECT * FROM vreg_no WHERE user_id = YOUR_USER_ID";
or probably a join depending on your database structure.
Than you can add all values to an array and use something like in_array to check if this value exists for a certain user.
And you should dump the deprecated mysql_* functions and switch to prepared statements with bound variables in PDO or mysqli.
If the issue is not producing duplicate user generated content, wouldn't it just be an issue of issuing a DISTINCT query?
$query = "SELECT DISTINCT vreg FROM vreg_no order by vreg desc";
Am facing troubles in this code, i just want to get all data from table row if the user selected "show all" from the select drop menu.
here is the select menu !
so, this menu grab data from this table, but if he selects All, what is the suitable code to echoing in between option value :)
<b>speciality:</b> <select id="main_mav" name="speciality">
<option value="none">Select speciality:</option>
<option value=""> All specialities </option>
<?php
$result = mysql_query('SELECT speciality FROM visits') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['speciality'].'">'.$row['speciality'].'</option>';
}
?>
</select><br />
That's the Submit form !
if ($region=="All regions" ){
$region=$_POST['""'];
}
else ( $region=$_POST['region']);
$date1 =$_POST['from_date'];
$date2 = $_POST['to_date'];
$product=$_POST['product'];
$speciality=$_POST['speciality'];
$type=$_POST['visit_type'];
sql="SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
$result=mysql_query($sql); ## This line is new.
$num=mysql_numrows($result);
$row = mysql_fetch_array($result);
What's the correct code to enter if user selected " show all in drop menu " ?!
You really need to sanitize your inputs, at least with mysql_real_escape_string!
On to your actual question: just check if $speciality is empty, and generate a different query without the (speciality ='$speciality') condition.
Since your HTML referenced 'specialties' and your PHP referenced 'regions' I'm gonna just stick with 'regions', but here's the idea.
if ($region=="All regions" ){
$sql = 'SELECT id, customer_name, seller_1_name, seller_2_name, FROM visits';
} else {
$region = mysql_real_escape_string($_POST['region']);
$date1 = mysql_real_escape_string($_POST['from_date']);
$date2 = mysql_real_escape_string($_POST['to_date']);
$product = mysql_real_escape_string($_POST['product']);
$speciality = mysql_real_escape_string($_POST['speciality']);
$type = mysql_real_escape_string($_POST['visit_type']);
$sql = "SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
}
$result = mysql_query($sql); ## This line is new.
$num = mysql_numrows($result);
$row = mysql_fetch_array($result);
I have this code to load the month of the birthday that corresponds to the id number:
<?php
$query = "SELECT DISTINCT BIRTHDAY FROM student WHERE IDNO='".$_GET['id']."'";
if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
?>
<select title="- Select Month -" name="mm" id="mm" class="" >
<?php
while ($row = mysql_fetch_array($result))
list($year,$month,$day)=explode("-", $row['BIRTHDAY']);
?>
<option value="<?php echo $month;?>"><?php echo $month; ?></option>\n";
And this is the form action:
$birthday = mysql_real_escape_string($_POST['mm']);
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
What should I do, when I click on the update button, it executes, but I see the undefined offset error is stored in the mysql database and not the month.
I'm just a beginner, can you give me some tips on how can I achieve updating the data
In cases like this... you will have to use 3 selects and then join them to update the database... so, in the form you have something like this:
<select name='month'>
<option value='1'>January</option>
<option value='xx'>etc</option>
</select>
<select name='day'>
<option value='1'>1</option>
<option value='xx'>etc</option>
</select>
<select name='year'>
<option value='1980'>1980</option>
<option value='xx'>etc</option>
</select>
Then... the PHP that receives that data should do something like:
$year = mysql_real_escape_string($_REQUEST['year']);
$month = mysql_real_escape_string($_REQUEST['month']);
$day = mysql_real_escape_string($_REQUEST['day']);
$birthday = $year.'-'.$month.'-'.$day;
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
Of course... you have to verify first whether all variables are set or not. You can do so by using the isset method.
Check your update query, It may be wrong in that.
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
See this year and firstname, in this year is assigned to null character for you.
Just assign like this,
$birthday = mysql_real_escape_string($_POST['mm']);