How to get text from combobox in php in different page? - php

this is my code in the first page
<?php
session_start();
$fullname = $_SESSION['fullname'];
$success_page = './chart.php';
if(isset($_POST['Button1'])) {
$_SESSION['month'] = $_POST['Combobox1'];
$_SESSION['year'] = $_POST['Combobox2'];
header('Location: '.$success_page);
}
?>
And this is my 2nd page
session_start();
$fullname = $_SESSION['fullname'];
$month = $_SESSION['month'];
$year = $_SESSION['year'];
echo $month;
echo $year;
echo $fullname;
There are 2 combobox in my 1st page and or some reason I'm not able to echo the month and year in my 2nd page. Thanks for the help!
EDIT: this is the html part with my form
<select name="Combobox1" size="1" id="Combobox1" style="position:absolute;left:550px;top:580px;width:250px;height:50px;z- index:1;">
<option value="1">Janvier</option>
<option value="2">Février</option>
<option value="3">Mars</option>
<option value="4">Avril</option>
<option value="5">Mai</option>
<option value="6">Juin</option>
<option value="7">Juillet</option>
<option value="8">Août</option>
<option value="9">Septembre</option>
<option value="10">Octobre</option>
<option value="11">Novembre</option>
<option value="12">Décembre</option>
</select>
<select name="Combobox2" size="1" id="Combobox2" style="position:absolute;left:900px;top:580px;width:250px;height:50px;z-index:1;">
<option>2014</option>
<option>2015</option>
<form method='post' action='mois.php' enctype='multipart/form-data'>
<input type="submit" id="Button1" name="Button1" value="Submit" style="position:absolute;left:800px;top:700px;width:96px;height:25px;z-index:2;">
</form>

Related

PHP: How to carry over session variables between 3 pages?

So as of now, i can successfully get the results to move from page one to page two using post and get, but no matter what im doing it will not move the info to the 3rd page. Im trying to switch it over to sessions after reading its made exactly for this but for some reason im doing something wrong and after hours of searching i cant for the life of me figure out what it is. I've followed guides, followed videos, and other post related to the topic on this website. I have now come to the conclusion that it is just me and i need some assistance. Any help would be greatly appreciated.
Page 1 (Index Page | Input Your Variables):
<?php session_start();
$_GET['q'] = $q;
$_GET['s'] = $s;
?>
<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>
Page 2 (Search.php that will take you directly to page specified if its already been created):
<?php session_start();
$q = $_POST['q'];
$s = $_POST['s'];
?>
<?php
$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
echo "<a href='$dir/$s/$q.htm'>$file</a>";
}
}
closedir($res);
?>
<?php
echo $htmlHeader;
while($stuff){
echo $stuff;
}
echo "<script>window.location =
'http://www.somesite.com/$dir/$s/$q.htm'</script>";
?>
Page 3 (404 page for catch all that are not in the system):
<?php session_start();
?>
<form action="" method="" name="FormChoice">
<input name="q" maxlength="8" type="text" value="<?php echo $_POST['q']; ?>" id="q" required>
<select name="s" id="s" required aria-required="true">
<option value="" disabled>CHOOSE STATE</option>
<option value="AL" <?php if($_POST['s'] == al) {echo ' selected="selected"';} ?>>ALABAMA</option>
<option value="AK" <?php if($_POST['s'] == ak) {echo ' selected="selected"';} ?>>ALASKA</option>
<option value="AZ" <?php if($_POST['s'] == az) {echo ' selected="selected"';} ?>>ARIZONA</option>
<option value="AR" <?php if($_POST['s'] == ar) {echo ' selected="selected"';} ?>>ARKANSAS</option>
<option value="CA" <?php if($_POST['s'] == ca) {echo ' selected="selected"';} ?>>CALIFORNIA</option>
<option value="CO" <?php if($_POST['s'] == co) {echo ' selected="selected"';} ?>>COLORADO</option>
<option value="CT" <?php if($_POST['s'] == ct) {echo ' selected="selected"';} ?>>CONNECTICUT</option>
</select>
<input type="submit" id="submitbtn2" value="SEARCH" name="submit" OnClick="search()" />
<span id="or">OR</span>
<input type="submit" id="addbtn" value="ADD" name="submit" OnClick="add()" />
</form>
page1
<?php
session_start();
// next 2 lines do NOTHING remove them
// as you have not yet loaded any values into $q and $s
//$_GET['q'] = $q;
//$_GET['s'] = $s;
?>
<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>
Page 2 - Search - receives data from previous form
- Contains lots of unecessary <?php...?>
- Previous form uses method="get" so data will arrive in the $_GET array not the $_POST array
<?php
session_start();
//$q = $_POST['q'];
//$s = $_POST['s'];
// But this is silly as you have not yet tested these values exist
// but you do that in the next lines
//$q = $_GET['q'];
//$s = $_GET['s'];
$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : '';
$res = opendir($dir);
// Now if you want to pass the values of `q` and `s` on to the next form
// they now need to be added to the session
$_SESSION['q'] = $q;
$_SESSION['s'] = $s;
while(false!== ($file = readdir($res))) {
if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
echo "<a href='$dir/$s/$q.htm'>$file</a>";
}
}
closedir($res);
echo $htmlHeader;
while($stuff){
echo $stuff;
}
echo "<script>
window.location = 'http://www.somesite.com/$dir/$s/$q.htm';
</script>";
// added missing semi colon ^
?>
Page 3 (404 page for catch all that are not in the system):
Now the data will be available in the SESSION, when you get to this page.

Multiple search value using dropdown list in PHP and MySQL

Html Form:
<form>
<select name="country[]" id="country" multiple>
<option value="any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
</form>
PHP Code
<?php
$country = $_REQUEST['country'];
if($country=="")
$countrysql = "";
else
{
if($country == "Any") $countrysql = "";
else
{
$country = str_replace(",","','",$country);
$countrysql = " and Country in ('$country')";
}
}
$queryString = "SELECT * FROM register where $countrysql";
?>
I have created a form in PHP and I want to search multiple options. I already created table Register and a column Country. I am getting the result If I give single value. If I give multiple I am not getting the result. Please help.
You evaluate in if($country == "Any") the word Any is not equal to any in option <option value="any">any</option>
But I suggest this php code:
<?php
$country="";
$countryError="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["country"])){
$countryError = "Country is required";
}else{
$country = $_POST["country"];
}
if($country == "Any") {
$queryString = "SELECT * FROM register";
}else{
$queryString = "SELECT * FROM register where Country in ('$country')";
}
// Print the SQL string:
echo $queryString;
}
?>
The html tags:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="country" id="country" multiple>
<option value="Any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<span class="error"><?php echo $countryError;?></span>

how to change select tag values based on value coming from database

i want to change tag option based on values coming from database.if value is "1" i want to show different options and different for other values.
<<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
Because
No closing ; on here <option value='4'>Maths</option></select>"
and here too <option value='8'>Maths</option></select>"
First if() condition missing end }.
So final code is
<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number = $_POST['number'];
//echo $number;
if ( $number == "1" )
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
You are missing the ; off of the end of both echos:
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
Note: Sorry about poor layout, quick posting of answer!
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"; //missing ; here
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"; //missing ; here
}
} // missing } here

php code to get listbox selected value in variable for where clause

below is my select tag code..I need to get selected input in temp variable.
and use this variable to search.
FOR EX-
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
Now I selected Saab from listbox. then get this value in variable in temp
and use this temp variable in search query.
<?php
$temp = "Saab";
$book_no2 = $temp;
$receipt = $database->getRows("SELECT receipt_no FROM scheme_master WHERE book_no2 = :book_no2", array(':book_no2'=>$book_no2));
?>
<select name="book" onChange="showUser(this.value)">
<?php foreach ($book as $load_book){ ?>
<option value=""></option>
<option value="<?php echo $load_book["book_no"]; ?>"><?php echo $load_book["book_no"]; ?></option>
<?php }?>
</select>
<?php
$book_no2 = $load_book["book_no"];
$receipt = $database->getRows("SELECT receipt_no FROM scheme_master WHERE book_no2 = :book_no2", array(':book_no2'=>$book_no2));
?>
You should you form and get the selected book from global $_POST variable, but make sure you properly escaped all the variables coming from the client side (e.g. using mysql_real_escape_string PHP function).
<?php
$book_no2 = mysql_real_escape_string($_POST['book']);
$receipt = $database->getRows("SELECT receipt_no FROM scheme_master WHERE book_no2 = :book_no2", array(':book_no2'=>$book_no2));
?>
<form method="POST">
<select name="book" onChange="showUser(this.value)">
<?php foreach ($book as $load_book){ ?>
<option value=""></option>
<option value="<?php echo $load_book["book_no"]; ?>"<?php if ($load_book["book_no"] == $temp):?> selected="selected"<?php endif?>><?php echo $load_book["book_no"]; ?></option>
<?php }?>
</select>
<input type="submit" value="Send" />
</form>
Plz see ...this will resolve my problem....
<select name="book" onChange="showUser(this.value)">
<option value="<?php if(isset($_GET['edit'])){ echo $data['book']; }?>"><?php if(isset($_GET['edit'])){echo $data['book'];}?></option>
<?php foreach ($book as $load_book){ ?>
<option value=""></option>
<option value="<?php echo $load_book["book_no"]; ?>"><?php echo $load_book["book_no"]; ?></option>
<?php }?>
</select>
<?php
$book_no2 = $load_book["book_no"];
$receipt = $database->getRows("SELECT DISTINCT receipt_no FROM scheme_master WHERE book_no2 = :book_no2", array(':book_no2'=>$book_no2));
?>
Make your first file
<form action="second page" method="post"
<select name="car">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</ option>
</select>
<input type="submit">
</form>
Then pass this to your temp var
$temp=$_POST[`car`];

validate options[] with isset

Hey i am having some trouble getting an options[] array to work, if anyone can help that will be great
Form
<form method="post" action="array2.php">
<select name="options[]">
<option value=""></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>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
The array2.php
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options[]'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
Any help would be great, also what happens is even if it's an empty feild, the thing progress's to the rest of the script
rest of script
<?php
for($i=0; $i < count($checked); $i++){
echo "You have selected to recive " . $checked[$i] . " tickets<br/>";
}
for($i=0; $i < count($checked2); $i++){
echo "And you have selected to recive " . $checked2[$i] . " for accommodation are you sure? <br/>";
}
?>
Sorry I am unable to reply to people for now soon as I posted it a class came to the empty room so need to wait an hour :/
You need the change your options[] to option as it mean you are submitting multiple select with the same name
<form method="post" action="array2.php">
<select name="options">
<option value=""></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>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
in your array2.php file
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
if you really need to send options[]
<?php
session_start();
if(isset($_POST['submit'])){
if(is_array($_POST['options']){
if($_POST['options'][0] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'][0];
$_SESSION['checked'] = $checked;
}
}else{
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
}
?>
you can clean this if else as you like
You can use this example code
<?php
if($_POST) {
if(isset($_POST['state'])) {
if($_POST['state'] == 'NULL') {
echo '<p>Please select an option from the select box.</p>';
}
else {
echo '<p>You have selected: <strong>', $_POST['state'], '</strong>.</p>';
}
}
}
?>
and your html code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="NULL">-- Please select a state --</option>
<option value="AK">AK - Alaska</option>
<option value="AL">AL - Alabama</option>
<option value="WY">WY - Wyoming</option>
</select>
<input type="submit" name="submit">
</fieldset>
</form>
in your case options[] can be used for sending multiple fields as array, the index starts with 0, for example:
<input name="test[]"> : index 0
<input name="test[]"> : index 1
then, you can get these values in $_POST['test'] like this:
$input_one = $_POST['test'][0];
$input_one = $_POST['test'][1];
if you look at this inside $_POST it will be like this:
$_POST = array (
...,
'test' => array(0=> ..., 1 => ...)
)
for your form, if you only had one options[], then the value is the $_POST will be
if(isset($_POST['options'][0])){
}
So let's clean it up
<select name="options">
You do not need options[].
And then the result of $_POST['options'] will be the value of option. And add there <option value="0">Select</option> and then check whether the POSTed data is higher then 0
in html
<select name="options">
<option value="0"></option>
<option value="1">1</option>
.
.
in php
if(isset($_POST['options']) && !empty($_POST['options'])) {
$checked = $_POST['options'];
}

Categories