PHP-HTML passing multiple selection value in to session variables - php

I just start learn PHP, HTML thing, but got bumped in to select options and handling.
here is my code:
<select id="VarRole" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</Select>
<select id="VarAcct" class="form-control">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</Select>
if (isset($_POST['VarRole'] && $_POST['VarAcct'])) {
$_SESSION['VarAcct'] = $_POST['VarAcct'];
$_SESSION['VarRole'] = $_POST['VarRole'];
echo '<script type="text/javascript"> location.href = "success.php"; </script>';
}
My goal is to send 2 selected result to become session variables and if success it will redirect to success.php page.
Sorry if my explanation are not quite clear, feel free to ask.
Edit:
I try below solution but still fail to redirect to success.php page, im adding full page so i hope it can be clear. thank you so much guys!
content of P1.php
<?php
session_start();
if(!empty($_POST["add_record"])) {
if ( isset(
$_POST['VarAcct'],
$_POST['VarRole'])) {
$_SESSION['VarAcct'] = $_POST['VarAcct'];
$_SESSION['VarRole'] = $_POST['VarRole'];
ob_clean();
exit( header('location:P2.php'));
}
}
?>
<body>
<form class="form-group form-default" method="post">
<div class="form-group row">
<div class="col-sm-10">
<select name="VarRole" class="form-control">
<option value="1">1
<option value="2">2
<option value="3">3
</select>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<select name="VarAcct" class="form-control">
<option value="1">A
<option value="2">B
<option value="3">C
</select>
</div>
</div>
<input name="add_record" type="submit" value="Submit" class="btn btn-primary">
Back
</form>
</body>
this is the content of success.php
<?php
session_start();
$VarAcct = $_SESSION['VarAcct'];
$VarRole = $_SESSION['VarRole'];
echo $VarAcct;
echo $VarRole;
echo 'Back';
?>
Finally it works forgot to add session_start() thank you professor for this one!

Form elements do NOT send the ID attribute when the form is submitted - it is the name that forms the key in the request array.
<select name="VarRole" class="form-control">
<option value="1">1
<option value="2">2
<option value="3">3
</Select>
<select name="VarAcct" class="form-control">
<option value="1">A
<option value="2">B
<option value="3">C
</Select>
isset will accept multiple items to verify at once ( returns false if any item fails the test ) but you separate with a comma - there is no need to use && within the arguments.
One other thing - it would be cleaner to use header to redirect rather than use Javascript as you were trying to do.
<?php
if ( isset(
$_POST['VarRole'],
$_POST['VarAcct']
)) {
$_SESSION['VarAcct'] = $_POST['VarAcct'];
$_SESSION['VarRole'] = $_POST['VarRole'];
ob_clean();
exit( header('location: success.php'));
}
?>

Related

how to prevent from getting undefined index when using GET method

So I have 2 page. MainFile.php and login.php. In Mainfile.php there is an option where you have to choose what warehouse you are in. Every option have values or ID in it.
This is my MainFile.php:
<div class="container">
<div class="wrapper">
<h1>Department</h1>
<label>Choose Warehouse</label><br>
<select id="wh">
<option value=""></option>
<option value="login_WH.php?whID=1">Warehouse 1</option>
<option value="login_WH.php?whID=2">Warehouse 2</option>
<option value="login_WH.php?whID=3">Warehouse 3</option>
<option value="login_WH.php?whID=4">Warehouse 4</option>
<option value="login_WH.php?whID=5">Warehouse 5</option>
<option value="login_WH.php?whID=6">Warehouse 6</option>
<option value="login_WH.php?whID=7">Warehouse 7</option>
<option value="login_WH.php?whID=8">Warehouse 8</option>
<option value="login_WH.php?whID=9">Warehouse 9</option>
<option value="login_WH.php?whID=10">Warehouse 10</option>
<option value="login_WH.php?whID=11">Warehouse 11</option>
<option value="login_WH.php?whID=12">Warehouse 12</option>
<option value="login_WH.php?whID=13">Warehouse 13</option>
<option value="login_WH.php?whID=14">Warehouse 14</option>
<option value="login_WH.php?whID=15">Warehouse 15</option>
<option value="login_WH.php?whID=16">Warehouse 16</option>
<option value="login_WH.php?whID=17">Warehouse 17</option>
<option value="login_WH.php?whID=18">Warehouse 18</option>
<option value="login_WH.php?whID=19">Warehouse 19</option>
<option value="login_WH.php?whID=20">Warehouse 20</option>
<option value="login_WH.php?whID=21">Warehouse 21</option>
<option value="login_WH.php?whID=22">Warehouse 22</option>
<option value="login_WH.php?whID=23">Warehouse 23</option>
<option value="login_WH.php?whID=24">Warehouse 24</option>
<option value="login_WH.php?whID=25">Warehouse 25</option>
<option value="login_WH.php?whID=26">Warehouse 26</option>
<option value="login_WH.php?whID=27">Warehouse 27</option>
<option value="login_WH.php?whID=28">Warehouse 28</option>
<option value="login_WH.php?whID=29">Warehouse 29</option>
<option value="login_WH.php?whID=30">Warehouse 30</option>
</select>
</div>
In login.php, I will get the ID of the option in the MainFile.php that I will be using to determine if this user is on warehouse 1,2,3 etc., then redirect the user to the homepage if the username, password and ID match my record in the database. I don't get any error in my code unless I will input an incorrect username or password. It will be too inconvenient if user will go to Mainfile.php just to get the ID to not get error when login in.
This is my code in login.php:
<form method="post" action="login_WH.php" autocomplete="off">
<?php echo display_error(); ?>
<?php
$id = $_GET["whID"];
$_SESSION["whID"] = $id;
?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" maxlength="15">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password" maxlength="15">
</div>
<input type="text" name="whID" class="hide" value="<?php echo $id ?>">
<div class="input-group">
<button type="submit" class="btn" name="login_btn_wh">Login</button>
</div>
<p>
Not your department? Go back
</p>
</div>
</form>
I can't post the picture because of my low reputation. but I get the "Notice: Undefined index: whID in C:\xampp\htdocs\project2\login_WH.php on line 29" in the line in login.php "$id = $_GET["whID"];"
I've tried to use session but I think its not working (or I just used it wrong?). Any help will be appreciated! btw sorry for my english, I hope you understand my question.
Your $_GET parameter is not getting set. $_GET gets the whID parameter from the URL. Unless you are manually navigating to the pages such as login_WH.php?whID=1, login_WH.php?whID=2, etc., then the $_GET parameter will never get set.
What I believe you're intending to do is link off to each warehouse individually from MainPage.php:
Warehouse 1
Warehouse 2
Then on the login.php script you will be able to receive the $_GET["whID"] parameter. You can also additionally check this with isset():
<?php
if (isset($_GET["whID"] && !empty($_GET["whID"])) {
$id = $_GET["whID"];
} else {
// Redirect or throw an error
}

How to get value from combobox into next url

I wonder if anyone can help my problem, in which way I can get value from combobox selection into next url by clicking so I can make it as a parameter to the pdf report.
this is my code to add some combobox :
<form method="GET">
<label class="col-sm-3 control-label">Bulan</label>
<div class="col-sm-2">
<select name="bln" class="form-control">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Maret</option>
<option value="4">April</option>
<option value="5">Mei</option>
<option value="6">Juni</option>
<option value="7">Juli</option>
<option value="8">Agustus</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">Desember</option>
</select>
</div>
<br>
<br>
<label class="col-sm-3 control-label">Tahun</label>
<div class="col-sm-2">
<select name="thn" class="form-control">
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
</select>
</div>
</form>
<?php
if (isset($_GET['bln'])) {
$bln = $_GET['bln'];
$thn = $_GET['thn'];
}
?>
<a class="btn btn-success" href="rep-cuti-tahunan-all.php?bln="$bln"&thn="$thn"">Print</a>
so I would like to put $bln and $thn into <a class="btn btn-success" href="rep-cuti-tahunan-all.php?bln="$bln"&thn="$thn"">Print</a>
and get my report in pdf exactly according to that parameters
any suggestions?
You don't have to add it as a parameter.Just change your form like this.
<form method="GET" action ="rep-cuti-tahunan-all.php">
....
</form>
then on your php file, you fetch the selected value.
<?php
....
if (isset($_GET['bln'])) {
$bln = $_GET['bln'];
$thn = $_GET['thn'];
}
....
//Do your print method ...
?>

How to get value from select option and not the contents - PHP

I am pretty new to PHP and have looked for a solution for what I'd have thought would be a common question but I can't find the answer. Any help would be greatly appreciated.
So I have a form with a selector and various options:
<form class="form-horizontal" id="main-home-form" action="trip_search_process.php" method="post">
<div class="form-group">
<label>Select location:</label>
<select class="form-control" id="location_selector" name="location">
<option value="default">I Don't Mind...</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="aus_nz">Australia & New Zealand</option>
<option value="europe">Europe</option>
<option value="north_america">North America</option>
<option value="south_america">South America</option>
</select>
When this form is submitted, I have this PHP code which extracts the location selected:
$location_passed = $_POST["location"];
However, what is passed is the contents of the 'option'. What I would like is the value. I.e. for this 'option':
<option value="aus_nz">Australia & New Zealand</option>
I currently get 'Austraia & New Zealand', whereas I want aus_nz
Is this possible? Or am I going about this the wrong way?
<?php
if(isset($_POST['re'])){
if($_POST['location'] == 'default'){
// error
echo '<p>make a selection</p>';
}else{
$val = $_POST['location'];
echo '<p> selected value; '.$val.'</p>';
}
}
?>
<form class="form-horizontal" id="main-home-form" action="" method="post">
<div class="form-group">
<label>Select location:</label>
<select class="form-control" id="location_selector" name="location">
<option value="default">I Dont Mind...</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="aus_nz">Australia & New Zealand</option>
<option value="europe">Europe</option>
<option value="north_america">North America</option>
<option value="south_america">South America</option>
</select><br>
<input type="submit" name="re" id="re" value="value.">
</div>
</form>
the above code will give the value of the selected option.
Put the php code to your action page trip_search_process.php
<?php
if(isset($_POST['submit'])){
echo $value = $_POST["make"];
echo $text = $_POST["make_text"];
}
?>
<form method=post>
<select name='make' onchange="setTextField(this)">
<option value = '' > Please Select Value </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<br/>
<input type=submit name=submit>
<input id="make_text" type = "hidden" name = "make_text" />
<script type="text/javascript">
function setTextField(ddl) {
document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
</form>

Dependent dropdown getting ids not name

I am trying to get user selection from php form but instead of name I am getting id of elements. instead of ids I want to get names. Please help
here is the url
http://efinancec.com/d/drop4/index.php
and here is the code
<form action="../form/form1.php" method="post" enctype="multipart/form-data">
<div class="frmDronpDown">
<div class="row">
<label>Training:</label><br/>
<select name="training" id="training-list" class="demoInputBox" onChange="getCourse(this.value);">
<option value="">Select Training</option>
<?php
foreach($results as $training) {
?>
<option value="<?php echo $training["id"]; ?>"><?php echo $training["training"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>Course:</label><br/>
<select name="course" id="course-list" class="demoInputBox" onChange="getCountry(this.value);">
<option value="">Select Course</option>
</select>
</div>
<div class="row">
<label>Country:</label><br/>
<select name="country" id="country-list" class="demoInputBox" onChange="getCity(this.value);">
<option value="">Select Country</option>
</select>
</div>
<div class="row">
<label>City:</label><br/>
<select name="city" id="city-list" class="demoInputBox" onChange="getDates(this.value);">
<option value="">Select City</option>
</select>
</div>
<div class="row">
<label>Dates:</label><br/>
<select name="dates" id="dates-list" class="demoInputBox" onChange="getPrice(this.value);">
<option value="">Select Dates</option>
</select>
</div>
<div class="row">
<label>Online Onsite Price:</label><br/>
<select name="price" id="price-list" class="demoInputBox">
<option value="">Select Online or Onsite</option>
</select>
</div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
and this is how I am getting the values from sql database
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["training_id"])) {
$query ="SELECT * FROM course WHERE training_id = '" . $_POST["training_id"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select Course</option>
<?php
foreach($results as $course) {
?>
<option value="<?php echo $course["id"]; ?>"><?php echo $course["course"]; ?></option>
<?php
}
}
?>
Change this line:
onChange="getCourse(this.value);">
To this :
onChange="getCourse(this.text);">
You are passing id of the course as the value of dropdown and calling that value which is fine but you need to call the text of select option to get the course name.
I changed this.value to this.text as suggested and created the new page and it stoped working at all now it is not pulling the records from database except the first one. Here is the link
http://efinancec.com/d/drop4/index1.php
where as my earlier page is still working fine.
http://efinancec.com/d/drop4/index.php
I think I could not convey my problem earlier. My depended dropdown is working fine but when a user submits the form in the email I receive the ids instead of name. Thats the problem.
For example I myself made some selections on index.php and submitted form Here is the email I got
training: 1
course : 101
country: 1001
city: 5005
Basically I am getting the the respective ids instead of name.

IF POST Variables equals then redirect

Im trying to get form post values and if they match certain criteria redirect only my page doesnt seem to...
I have a form with multiple input fields
HTML
<form method="post" action="?page_id=151">
<div style="display: block;" id="Q1">
<label for="vehicleType">1. What type of vehicles do you have?</label>
<select id="vehicleType" name="vehicleType">
<option selected="selected" value="choose">Please choose</option>
<option value="hgv">HGV</option>
<option value="psv">PSV</option>
<option value="lgv">LGV</option>
<option value="car">Car</option>
<option value="mixed">Mixed Fleet</option>
</select>
</div>
<div id="Q2group1">
<label for="coverageRegion">2. Do you require national or international coverage?</label>
<select id="coverageRegion" name="coverageRegion">
<option selected="selected" value="choose">Please choose</option>
<option value="national">National</option>
<option value="international">International</option>
</select>
</div>
<div id="Q2others">
<label for="pricing">2. Do you prefer a fixed weekly price or a pump-related price?</label>
<select id="pricing" name="pricing">
<option selected="selected" value="choose">Please choose</option>
<option value="fixed">Weekly fixed price</option>
<option value="pump">Pump-related price</option>
</select>
</div>
<div id="Q3group1">
<label for="locationType">3. What type of site do you prefer?</label>
<select id="locationType" name="locationType">
<option selected="selected" value="choose">Please choose</option>
<option value="truckstops">Truck stops at lowest price</option>
<option value="motorway">Branded motorway sites</option>
</select>
</div>
<div id="Q3others">
<label for="siteCoverage">3. What site coverage do you need?</label>
<select id="siteCoverage" name="siteCoverage">
<option value="choose">Please choose</option>
<option value="countysites">County</option>
<option value="nationalsites">National</option>
</select>
</div>
<div id="Q4others">
<label for="county">4. Select your county:</label>
<select id="county" name="county">
<option value="choose">Please choose</option>
<optgroup label="England">
<option>Bedfordshire</option>
<option>Berkshire</option>
<option>Bristol</option>
<option>Tyrone</option>
</optgroup>
</select></div>
<div id="searchbuttondiv"> <input type="submit" id="searchbutton" onclick="_gaq.push(['_trackEvent', 'Fuel Card Search', 'Homepage Select Criteria'])"> </div>
</form>
PHP
if ($_POST['vehicleType'] == 'car' && $_POST['pricing'] == 'fixed' && $_POST['coverageRegion'] == 'national') {
echo '<script>alert("hi");</script>';
// header('Location: /car-weekly-national/');
}
One of your conditions is falling (and because they are all bound by AND, all of the if statement fails.
Have an else statement where you var_dump all relevant variables and see what's wrong.
Sorry everyone, oversight by me, I was checking to see if the wrong variable had been posted, wouldn't have found it if it wasn't for the suggestions to add an else statement, thanks!

Categories