Linking a select form with a switch case PHP - php

I am trying to link a select form with a switch case statement.
booking.php
<?php
session_start();
echo $_SESSION['$usernamepassword'];
?>
<!DOCTYPE html>
<html>
<body>
<style>
body {
background-color: linen;
}
</style>
<h1 style='color:blue'>Book day!</h1>
<form method ="post" action='booking2.php'>
<select>
<option value="friday">Friday</option>
<option value="saturday">Saturday</option>
<option value="sunday">Sunday</option>
</select>
<input type="Submit" name="submit">
<br>
<br>
</form>
pbooking.php
<?php
session_start();
echo $_SESSION['$usernamepassword'];
?>
<!DOCTYPE html>
<html>
<?php
switch ($_POST["value"]) {
case "friday":
header('Location: friday.php');
break;
case "saturday":
header('Location: saturday.php');
break;
case "sunday":
header('Location: sunday.php');
break;
}
?>
I have created two documents.
The first document, booking.php contains the form.
In the form there is the dropdown.
On the second document, pbooking.php contains the switch case.
I am trying to link the switch off the value.
As the value changes the case changes, friday - saturday - sunday.
It doesn't work... returning undefined index line 7 ('value')
This would suggest to me that it needs to be linked off something else?
Any suggestions? Thanks.

Please use name attributes in <select> tag
like this:
<select name="value">

You need to give name attribute to your select type. Also change your action to pbooking.php
<form method ="post" action='pbooking.php'>
<select name="value">
<option value="friday">Friday</option>
<option value="saturday">Saturday</option>
<option value="sunday">Sunday</option>
</select>
<input type="Submit" name="submit">
<br>
<br>
</form>

Try this :
<form method ="post" action='booking2.php'>
<select name="value">
<option value="friday">Friday</option>
<option value="saturday">Saturday</option>
<option value="sunday">Sunday</option>
</select>
Your $_POST['value'] is not linked with any input

Put name="value"
<select name="value">

Related

php if statement from html option value error

I am trying to replicate the following:
php if statement html option value
and am following the directions as suggested:
Give name to your select.
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
In php
Example:
switch($_POST['selectedValue']) {
case 'Newest':
// do Something for Newest
break;
case 'Best Sellers':
// do Something for Best seller
break;
case 'Alphabetical':
// do Something for Alphabetical
break;
default:
// Something went wrong or form has been tampered.
}
However, I am getting the following error:
Notice: Undefined index: selectedValue in C:\xampp\trial.php on line 7
Please help
if you are sending form data then you have to set method post in your form tag or if you are using ajax send it by type post and in your php file put code inside if condition after form has been posted like isset($_POST['selectedValue'])
<form method="post" action="url to your php file">
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
<button type="submit">Submit</button>
</form>
That's happens because you don't always have the key selectedValue in your POST array.
if(isset($_POST['selectedValue'])){
// your code
}
My problem was solved after adding a submit button within the form tag of HTML
<form action="#" method="post">
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option
</select>
<input type="submit" style="float:left; margin-left: 2%;" name="submit" value="Search"/>
</form>

Setting PHP form action using IF

How can i conditionally set PHP form action?.
i have the following drop down option, when 2WD and 4WD selected i want to redirect or go to A.php otherwise the other one MFWD selected to open page B
<form action="page.php">
<select name="tractor">
<option value="2wd">2WD</option>
<option value="mfwd">MFWD</option>
<option value="4wd">4WD</option>
</select>
<input type="submit">
here is the whole code
<?php ob_start ?>
<!DOCTYPE html>
<html>
<body>
<form method="post">
<select name="tractor">
<option value="2wd">2WD</option>
<option value="mfwd">MFWD</option>
<option value="4wd">4WD</option>
</select>
<br><br>
<input type="submit" name="cmd_submit" />
</form>
<?php
if(isset($_POST['cmd_submit'])){
if($_POST['tractor'] == "2wd" || $_POST['tractor'] == "4wd"){
//redirect here
header('location:iar7.php');
}else{
header('location:iar-calculator.php');
}
}
?>
</body>
</html>
you can do it like this. omit the action attribute and add a method="post".. otherwise you need to add a get method there... and get the query string of the tractor to do a conditional statement on your action attribute....
HTML Code
<form method="post">
<select name="tractor">
<option value="2wd">2WD</option>
<option value="mfwd">MFWD</option>
<option value="4wd">4WD</option>
</select>
<input type="submit" name="cmd_submit" />
</form>
PHP Code:
<?php
if(isset($_POST['cmd_submit'])){
if($_POST['tractor'] == "2wd" || $_POST['tractor'] == "4wd"){
//redirect here
header('location:a.php');
}else{
header('location:b.php');
}
}

PHP: connect drop-down list with submit button and if/else statement

So, to explain my question. I have four .php codes on web server, and I have drop down list in php code also (it includes name of all four php files that are on server). I have also submit button.
I should do following: User choose one name from drop down list, click submit button, and than that php file should be called and shown on web page.
The problem is, I don't know hot to connect these three part of php code.
<p>
What Genre you want?
<select name="Ganre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" />
<if (Genre == "FPS") { ?>
<form method="get" action="FPS.php"}>
<else if (Genre == "JRPG") { ?>
<form method="get" action="JRPG.php"}>
<else if (Genre == "RPG") { ?>
<form method="get" action="RPG.php"}>
<else if (Genre == "Sports") { ?>
<form method="get" action="Sports.php"}>
</p>
There are several issues with the code you provided:
You have no opening <?php for those closing ?>.
You have lots of invalid syntax, like If (Genre == "FPS") { (What is Genre?) or like
<else if
You have two spellings of "Genre" (Ganre).
There are several ways to do what you want. As you did not mention Javascript, I propose a PHP-only solution. You would just let the form submit to the
same PHP file, and there detect which choice was made. Then let PHP redirect to the
page of choice:
<?php
if (isset($_GET["genre"])) {
// User submitted their choice, so redirect to that page
// Make sure not to echo anything when using header():
header("Location: " . $_GET["genre"] . ".php");
// Make sure to not execute any other code in this file
exit();
}
// User did not yet submit a choice, so present list
?>
<form method="get">
<p>
What Genre you want?
<select name="genre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit"/>
</p>
</form>
Add onchange into select tag and add below javasctipt code.
<form method="get" action="">
<select name="Ganre" onchange="actionChangdde(this)">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" />
</form>
<script>
function actionChangdde(sel){
var getSelVal = sel.value;
var repAction = getSelVal+'.php';
sel.parentElement.setAttribute("action", repAction);
}
</script>
Try this code :
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<form>
<p>
What Genre you want?
<select name="Ganre" id="genre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" id="submit_button"/>
</p>
</form>
<script type="text/javascript">
$(document).ready(function(){
var dropDownValue = $('#genre').val();
$('#submit_button').click(function () {
if($('#genre').val() == "FPS"){
window.location="FPS.php";
}
if($('#genre').val() == "JRPG"){
window.location="JRPG.php";
}
if($('#genre').val() == "RPG"){
window.location="RPG.php";
}
if($('#genre').val() == "Sports"){
window.location="Sports.php";
}
if($('#genre').val() == ""){
return false;
}
});
});
</script>

How to display text on dropdown select

I'm trying to do something very simple in PHP, but keep getting an error message. Essentially, when someone selects "Cat", I want "Miaow" to appear.
My idea was:
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<?php if ($_POST['demo'] == 'Cat') { echo 'Miaow'; } ?>
However, in PHPFiddle,
I get 'E_NOTICE : type 8 -- Undefined index...'
as soon as the code runs. Am I missing something basic? Thanks!
Your form might be passing data by $_GET instead of $_POST.
Did you specify the method ?
<form method="post" action="index.php">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" value="Submit">
</form>
You can var_dump($_POST); and var_dump($_GET); to see what those variables contains in your PHP file.
Or you can do it in javascript like this :
function animal(value) {
if(value == "Cat") {
document.getElementById("myAnimal").innerHTML = "Miaouw";
} else {
document.getElementById("myAnimal").innerHTML = "Rooooah";
}
}
<form action="#">
<select name="demo" onchange="animal(this.value)">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
</form>
<span id="myAnimal"></span>
Is it even possible to do this using PHP so that "Miaow" comes up automatically on select, rather than having to submit the form?
You are looking for JavaScript code, not PHP. Here is a jQuery example:
$(document).on('change', '.animals', function(){
$('.noise-target').html( $('option:selected', this).data('noise') );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="animals">
<option>Select</option>
<option data-noise="Woof">Dog</option>
<option data-noise="Meow">Cat</option>
</select>
<div class="noise-target"></div>
Maybe this will help, I write some block as far as I understand...
<form action="#" method="post">
<select name="demo">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Fish">Fish</option>
</select>
<input type="submit" name="sub" />
</form>
<?php
if(isset($_POST['sub'])){
if($_POST['demo'] == "cat"){
echo "Miao";
}
}
?>

Can you make a select option list not set a $_POST value?

So I have a select option list with values in it. But as with most select option lists the first one states dummy text for the user. The issue is that when the $_POST variable is carried over the isset value is true, but is there a way to make it false? Here is just an example. The bottom always echos true even if trying to use the dummy text. Frustrating or maybe I am doing this wrong? BTW THIS IS JUST EXAMPLE CODE
<?php
if(isset($_POST['curreny_pairs'])):
echo "true";
endif;
?>
<html>
<form action="blah" method="post">
<select name="currency_pairs" id="currency_pairs">
<option selected="selected">Currency Pair</option>
<option value="AUDCAD" >AUD/CAD</option>
<option value="AUDCHF" >AUD/CHF</option>
</select>
<input type="submit" value="submit">
</form>
</html>
Two Way
1) set value="0" for first option
<option selected="selected" value="0">Currency Pair</option>
and check with empty() instead of isset() because
empty() return false for "0"
2) use disabled attribute for first option
<option selected="selected" disabled="disabled" >Currency Pair</option>
and then you can check with isset()
<?php
if(!empty($_POST['curreny_pairs'])):
echo "true";
endif;
?>
<html>
<form action="blah" method="post">
<select name="currency_pairs" id="currency_pairs">
<option selected="selected" value="0">Currency Pair</option>
<option value="AUDCAD" >AUD/CAD</option>
<option value="AUDCHF" >AUD/CHF</option>
</select>
<input type="submit" value="submit">
</form>
you can set specific value in the form first option and then make sure in not selected
<?php
if(isset($_POST['curreny_pairs']) && $_POST['curreny_pairs']!="false")
echo "true";
endif;
?>
<html>
<form action="blah" method="post">
<select name="currency_pairs" id="currency_pairs">
<option value="false" selected="selected">Currency Pair</option>
<option value="AUDCAD" >AUD/CAD</option>
<option value="AUDCHF" >AUD/CHF</option>
</select>
<input type="submit" value="submit">
</form>
</html>

Categories