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
}
Related
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'));
}
?>
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 ...
?>
I've been trying to post the value of two selected dropdown menu's using the SESSIONS method, however there seems to be an issue because when submitted the second page displays the PHP code only. Here are the relevant snippets of code:
shop.php
<form action="processing.php" method="post"> <div class="item">
<span class="caption">Essential Gray Suit<br />£459</span>
<select name="size1" class="size" id="size1" onchange="showDiv(this)">
<optgroup label="Size">
<option value="0">SELECT SIZE</option>
<option value="1">XS</option>
<option value="2">S</option>
<option value="3">M</option>
<option value="4">L</option>
<option value="5">XL</option>
</select>
<div id="quantity1" style="display: none;">
<select name="quantity1" class="size" onchange="showCart(this)">
<optgroup label="Quantity">
<option value="0">SELECT QUANTITY</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>
</select>
</div>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
processing.php
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['size1'] = $_POST['size1'];
}
echo $_SESSION['size1'];
?>
I have this select button:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
In the same page, few lines bellow, I have:
<div id="challenge">
<?php
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
?>
</div>
But when I change the option, it didn't change the post value.
$Ch = $_POST['challengeX'];
won't get value until you submit the form. Add a submit button to your form.
<form method="post" action="">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
<input type="submit" value="Submit">
</form>
And access the form values in the same page only if the form is submitted.
<div id="challenge">
<?php
if(isset($_POST)) // checks whether any value is posted
{
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
}
?>
</div>
you need to submit form on change of dropdown.
Change html like this:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;" onchange="this.form.submit()">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
for your php execution you can try like this:
<div id="challenge">
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
function showSomething($Ch,1,1){
//do your stuff
}
}
?>
</div>
PHP runs on the server and delivers HTML (or other data) to the browser.
If you want to process the form data with PHP then you must include it in a new HTTP request (typically by submitting the form) and have PHP generate a new page using that data.
Alternatively, if you want to process the data on the client then you will need to use a programming language that is supported in the browser. Unless you want to start using plugins (or browser specific technologies), that limits you to JavaScript. You can bind a change event listener to the select element and then manipulate the DOM with the results.
In the way you can use Javascript. If you dont want to use any server like PHP or something
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<input type="submit" /> /* add this line */
</label>
</form>
By Javascript
<select id="label" name="challengeX" style="margin-top:150px;" onChange="getSelect(this)">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<div id="resultSelect"></div>
<script>
function getSelect(thisValue){
document.getElementById('resultSelect').innerHTML=thisValue.options[thisValue.selectedIndex].text;
}
</script>
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!