IF POST Variables equals then redirect - php

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!

Related

How to display back data in option value in option list after user select data

User already choose the option value and the data will entered database. I wanted to display back the data choose by user in option ways so when they wanted to edit just click on the option value.
`
<div class="row">
<div class="col-25">
<label for="sector">1. Type of Sectors</label>
</div>
<div class="col-75">
<select id="sector" name="sector" required>
<option value ="choose">Choose type of sector...</option>
<option value="National DefenceSecurity">National Defence and Security</option>
<option value="banking & Finance">Banking and Finance</option>
<option value="Information & Communication">Infomation and Communication</option>
<option value="Energy">Energy</option>
<option value="Transportation">Transportation</option>
<option value="Water">Water</option>
<option value="Health services">Health Services</option>
<option value="Government">Government</option>
<option value="Emergency Services">EmergencyServices</option>
<option value="Agriculture&food">Agriculture and Food</option>
<option value="Other">Other</option>
</select>
`
what should I change on this code ? or have to add other code to insert the function.
should be display the user option before want to exchange the data/Update
At the time of update you come with a previously selected value
like your previously selected value stored in a variable $slctr
you have to check all the option one by one with $slctr variable if value match any option make it select.
Then your edit code will be like.
<div class="row">
<div class="col-25">
<label for="sector">1. Type of Sectors</label>
</div>
<div class="col-75">
<select id="sector" name="sector" required>
<option value ="choose" <?= $slctr=="choose"?"selected":"" ?> >Choose type of sector...</option>
<option value="National DefenceSecurity" <?= $slctr=="National DefenceSecurity"?"selected":"" ?> >National Defence and Security</option>
<option value="banking & Finance" <?= $slctr=="banking & Finance"?"selected":"" ?>>Banking and Finance</option>
<option value="Information & Communication" <?= $slctr=="Information & Communication"?"selected":"" ?>>Infomation and Communication</option>
<option value="Energy" <?= $slctr=="Energy"?"selected":"" ?>>Energy</option>
<option value="Transportation" <?= $slctr=="Transportation"?"selected":"" ?>>Transportation</option>
<option value="Water" <?= $slctr=="Water"?"selected":"" ?>>Water</option>
<option value="Health services" <?= $slctr=="Health services"?"selected":"" ?>>Health Services</option>
<option value="Government" <?= $slctr=="Government"?"selected":"" ?>>Government</option>
<option value="Emergency Services" <?= $slctr=="Emergency Services"?"selected":"" ?>>EmergencyServices</option>
<option value="Agriculture&food" <?= $slctr=="Agriculture&food"?"selected":"" ?>>Agriculture and Food</option>
<option value="Other" <?= $slctr=="Other"?"selected":"" ?>>Other</option>
</select>
</div>
</div>
You can done it by using jquery also
var response = {};
response.val = "Energy";
$("#sector option[value='" + response.val +"']").attr("selected", "selected");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="sector" name="sector" required>
<option value ="choose">Choose type of sector...</option>
<option value="National DefenceSecurity">National Defence and Security</option>
<option value="banking & Finance">Banking and Finance</option>
<option value="Information & Communication">Infomation and Communication</option>
<option value="Energy">Energy</option>
<option value="Transportation">Transportation</option>
<option value="Water">Water</option>
<option value="Health services">Health Services</option>
<option value="Government">Government</option>
<option value="Emergency Services">EmergencyServices</option>
<option value="Agriculture&food">Agriculture and Food</option>
<option value="Other">Other</option>
</select>

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 ...
?>

multiple input have same name

Here i have two fields such as follows
<div class="left_2 two">
<select name="abc_type" id="abc_type_2" class="form-control">
<option value="AB">LSK-AB</option>
<option value="AC">LSK-AC</option>
<option value="BC">LSK-BC</option>
</select>
</div>
<div class="left_2 one">
<select name="abc_type" id="abc_type_1" class="form-control">
<option value="A">LSK-A</option>
<option value="B">LSK-B</option>
<option value="C">LSK-C</option>
</select>
</div>
i will be using only one field at a time and the remaining field will be hidden and the problem am always getting the value A which is present in second select tag while getting submitted.how can i pass the correct value which i choosed
Here is the solution what ever field will be hidden add disabled in the input field Check the code below.
<?php
if(isset($_POST['abc_type'])){
echo $_POST['abc_type'];
}
?>
<form action="" method="post">
<div class="left_2 two">
<select name="abc_type" id="abc_type_2" class="form-control">
<option value="AB">LSK-AB</option>
<option value="AC">LSK-AC</option>
<option value="BC">LSK-BC</option>
</select>
</div>
<div class="left_2 one">
<select name="abc_type" id="abc_type_1" class="form-control">
<option value="A">LSK-A</option>
<option value="B">LSK-B</option>
<option value="C">LSK-C</option>
</select>
</div>
<input type="submit" name="submit" value="submit"/>
</form>

Post selected option's value to second page using sessions

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'];
?>

select value not taking properly

i have one big form.. there have some so many select option. and its from to mail function. bellow is my code
<label>Type of Event? </label>
<select id="eventtype" title="" name="eventtype" class="required select">
<option value="">Select Your Event Type</option>
<option value="WEDDINGS">WEDDINGS</option>
<option value="CORPORATE">CORPORATE EVENT</option>
<option value="SPECIAL">SPECIAL EVENT</option>
<option value="OTHER">OTHER SINGLE SERVICES</option>
</select>
<div class="cls"></div>
<div class="container">
<div class="WEDDINGS">
<label>Package</label>
<select name="Package" id="Package">
<option value="Day Of Coordination">Day Of Coordination</option>
<option value="Week of Coordination">Week of Coordination</option>
<option value="Month of Coordination">Month of Coordination</option>
</select>
</div>
<div class="CORPORATE">
<label>Package</label>
<select name="Package" id="Package">
<option value="Meetings">Meetings</option>
<option value="Client Appreciation">Client Appreciation</option>
<option value="Conference & Conventions">Conference & Conventions</option>
<option value="Corporate Shows">Corporate Shows</option>
<option value="Employee Appreciation">Employee Appreciation</option>
</select>
</div>
<div class="SPECIAL">
<label>Package</label>
<select name="Package" id="Package">
<option value="Engagements">Engagements</option>
<option value="Bar Mitzvah">Bar Mitzvah</option>
<option value="Quincearas">Quincearas</option>
<option value="Birthdays">Birthdays</option>
<option value="Fashion Shows">Fashion Shows</option>
<option value="Showers">Showers</option>
</select>
</div>
<div class="OTHER">
<label>Package</label>
<select name="Package" id="Package">
<option value="Wedding Consultation and Guidance Only">Wedding Consultation and Guidance Only</option>
<option value="Venue Search Only">Venue Search Only</option>
<option value="Coordination of Engagement Party Only">Coordination of Engagement Party Only</option>
<option value="Providing Qualified and Screened Vendors Only">Providing Qualified and Screened Vendors Only</option>
</select>
js code for realtion with drop down
<script>
$(document).ready(function() {
$('#eventtype').bind('change', function() {
var elements = $('div.container').children().hide();
var value = $(this).val();
if (value.length) {
elements.filter('.' + value).show();
}
}).trigger('change');
});
</script>
my from is like if select WEDDING then show Wedding div so show there another drop down.. and if select CORPORATE EVENT then show CORPORATE EVENT div... but no matter what drop down i select when mail go correct data not pass. its goes always OTHER div 1st select value mean "Wedding Consultation and Guidance Only" ... i have no idea why... may be i mistake something... bellow is part of php mail code
if($violation)
{
$eventtype = $_POST['eventtype'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$datepicker = $_POST['datepicker'];
$datepicker2 = $_POST['datepicker2'];
$time = $_POST['time'];
$EMAIL = $_POST['EMAIL'];
$phone = $_POST['phone'];
$Package = $_POST['Package'];
$otherfound = $_POST['otherfound'];
if (isset($_POST['found_group'])) {
$found = $_POST['found_group'];
for ($i=0; $i<count($found); $i++) {
//echo $found[$i]."<br />";
$found=implode("<br />",$found);
}
}
all i want to correct data will go .. can you tell me where i am mistaking... thanks
you may need to do something like this
<label>Type of Event? </label>
<select id="eventtype" title="" name="eventtype" name="eventtype" class="required select">
<option value="">Select Your Event Type</option>
<option value="WEDDINGS">WEDDINGS</option>
<option value="CORPORATE">CORPORATE EVENT</option>
<option value="SPECIAL">SPECIAL EVENT</option>
<option value="OTHER">OTHER SINGLE SERVICES</option>
</select>
<div class="cls"></div>
<div class="container">
<div class="WEDDINGS">
<label>Package</label>
<select name="WEDDINGSPackage" id="WEDDINGSPackage">
<option value="Day Of Coordination">Day Of Coordination</option>
<option value="Week of Coordination">Week of Coordination</option>
<option value="Month of Coordination">Month of Coordination</option>
</select>
</div>
<div class="CORPORATE">
<label>Package</label>
<select name="CORPORATEPackage" id="CORPORATEPackage">
<option value="Meetings">Meetings</option>
<option value="Client Appreciation">Client Appreciation</option>
<option value="Conference & Conventions">Conference & Conventions</option>
<option value="Corporate Shows">Corporate Shows</option>
<option value="Employee Appreciation">Employee Appreciation</option>
</select>
</div>
.......
and at PHP end you have to do something like this
if (isset($_POST['eventtype'])) {
$subOptSelect = $_POST['eventtype'] . 'Package';
if (isset($_POST[$subOptSelect])){
$sub = $_POST[$subOptSelect];
}
}
I have prefixed the value of the first select to the sub selects and it becomes easier at php end.
Edit: Here is the code you may require.
$package='';
if (!empty($eventtype)) {
if (!empty($_POST[$eventtype.'Package'])){
$package = $_POST[$eventtype.'Package'];
//$package contains the option selected in the sub option.
}else{
//nothing selected in the sub dropdown. you have to add code to handle this
}
}else{
//nothing selected in the first dropdown. you have to add code to handle this
}
And your email can be:
<table width='95%' cellpadding='0' cellspacing='11'>
<tr>
<td width='189'>Event Type :</td>
<td>$eventtype</td>
</tr>
<tr>
<td>Package :</td><td>$package</td>
</tr>
<tr>
<td>First Name :</td><td>$firstname</td>
</tr>
<tr>
You can't reuse the name attribute in html, and you're html form has the same name for each select tag. What is happening is the markup is processed sequentially so it only uses the last declaration of the name="Package" select so the selected value from that will always be passed to the server as the $_POST['package']. Do something like this instead:
<label>Type of Event? </label>
<select id="eventtype" title="" name="eventtype" class="required select">
<option value="">Select Your Event Type</option>
<option value="WEDDINGS">WEDDINGS</option>
<option value="CORPORATE">CORPORATE EVENT</option>
<option value="SPECIAL">SPECIAL EVENT</option>
<option value="OTHER">OTHER SINGLE SERVICES</option>
</select>
<div class="cls"></div>
<div class="container">
<div class="WEDDINGS">
<label>Package</label>
<select name="Weddings-Package" id="Weddings-Package">
<option value="Day Of Coordination">Day Of Coordination</option>
<option value="Week of Coordination">Week of Coordination</option>
<option value="Month of Coordination">Month of Coordination</option>
</select>
</div>
<div class="CORPORATE">
<label>Package</label>
<select name="Corporate-Package" id="Corporate-Package">
<option value="Meetings">Meetings</option>
<option value="Client Appreciation">Client Appreciation</option>
<option value="Conference & Conventions">Conference & Conventions</option>
<option value="Corporate Shows">Corporate Shows</option>
<option value="Employee Appreciation">Employee Appreciation</option>
</select>
</div>
<div class="SPECIAL">
<label>Package</label>
<select name="Special-Package" id="Special-Package">
<option value="Engagements">Engagements</option>
<option value="Bar Mitzvah">Bar Mitzvah</option>
<option value="Quincearas">Quincearas</option>
<option value="Birthdays">Birthdays</option>
<option value="Fashion Shows">Fashion Shows</option>
<option value="Showers">Showers</option>
</select>
</div>
<div class="OTHER">
<label>Package</label>
<select name="Other-Package" id="Other-Package">
<option value="Wedding Consultation and Guidance Only">Wedding Consultation and Guidance Only</option>
<option value="Venue Search Only">Venue Search Only</option>
<option value="Coordination of Engagement Party Only">Coordination of Engagement Party Only</option>
<option value="Providing Qualified and Screened Vendors Only">Providing Qualified and Screened Vendors Only</option>
</select>
when the post data is sent to the server it is sent in the $_POST variable as an associative array with the names as the key, if you make the name of each select unique then you should be able to achieve what you want. You might want to pass the name of which select was used to the server so you know which variable to grab in php. As another note the id value for every tag should be unique, as that is the purpose of an id attribute.

Categories