Calling and Declaring a select box in PHP! - php

I want to be able to change the color of my websites background depending on which option the user chooses.
I have this code for my select box:
<select name="change_date" >
<option value="1" id="1">1</option>
<option value="2" id="2">2</option>
<option value="3" id="3">3</option>
</select>
Using PHP, how would i get it so that it simply changed to red for 1, green for 2 and pink for 3?
This is the code I have tried (unsuccessfully and complete guesswork):
<?php
if(isset($_POST['change_date'])=='1' )
{
echo "<body style='background-color:red;'></body>";
}else{
echo "failed";
}
if(isset($_POST['change_date'])=='2' )
{
echo "<body style='background-color:green;'></body>";
}else{
echo "failed";
}
if(isset($_POST['change_date'])=='3' )
{
echo "<body style='background-color:pink;'></body>";
}else{
echo "failed";
}
?>
Any suggestions? methods? links?
UPDATE:
I have tried all methods and none seem to work guys. It must be something I am doing wrong.
What i want is when the user chooses and option ie 1,2 or 3 and clicks send, then the color will change.
Hope this helps more. I forgot to add before that I want a send button to have to be clicked then all the clever stuff happens.
Thanks

if (isset($_POST['change_date']))
{
switch ($_POST['change_date'])
{
case 1: $color = 'red'; break;
case 2: $color = 'green'; break;
case 3: $color = 'pink'; break;
default: die('failed');
}
echo "<body style='background-color:$color;'></body>";
}

Use a switch:
$color = !empty($_POST['change_date'])?$_POST['change_date']:0;
switch ($color) {
default:
case 1:
echo "pink";
break;
case 2:
echo "orange";
break;
}
Should do what you want. Plenty of other ways to do it with arrays etc. Just the way I chose to show you :)
EDIT:
Array Method
$colors = array(1 => 'pink', 2 => 'orange');
$color = !empty($_POST['change_date'])?$_POST['change_date']:1;
echo "<body style='background-color:" . $colors[$color] . ";'></body>";
Both should work, pending any errors I made.

your PHP code only works if the variable "change_date" comes from a query string via a POST method...
Do you need to set the color on the fly? or after sending a form?

You're problem is in your use of isset. This function simple returns a boolean value, not the value of the field. Try the below:
if(isset($_POST['change_date']))
{
switch($_POST['change_date'])
{
case 1:
echo "<body style='background-color:red;'></body>";
break;
case 2:
echo "<body style='background-color:green;'></body>";
break;
case 3:
echo "<body style='background-color:pink;'></body>";
break;
default:
echo "<body style='background-color:white;'></body>";
}
}
else
{
echo "<body style='background-color:white;'></body>";
}

Another way could be, if you dont wanna use switch statement ,
$color = isset($_POST['change_date']))?$_POST['change_date']:0;
if($color){
if($color == 1) echo "<body style='background-color:red;'></body>";
if($color == 2) echo "<body style='background-color:green;'></body>";
if($color == 3) echo "<body style='background-color:pink;'></body>";
}

Try a value map array, and as pointed out in one of the other answers it might be GET instead of POST, so I'm using $_REQUEST as example:
<?php
$colors = array(
1 => "red",
2 => "green",
3 => "pink",
);
if ($c = $colors[ $_REQUEST["change_date"] ])
{
echo "<body style='background-color: $c;'>body</body>";
}
else {
echo "failed";
}

Related

PHP: how to create a Story progressing "Case" ? and Debugging

This is rather simple: I still have some problems to create good, conclusive questions but i try my best.
I want to create a php Code that progresses while the user is submitting the information with some radio buttons. There is a script example i wrote so far.
Every Time the user clicks a radio button and submits the content the code should progress and $count gets a + 1. So far i created two steps of progression. Everytime the user chose one radio button and clicks the submit button the program should echo something else insteat what it does right now is it echos everything of the chosen radiobutton in one step. (what i don't want to have)
I don't know why that is so. Shouldn't break in Case stop the code and restart it?
I used CASE for this and i am pretty sure that is not the best way, if it is a way at all because it does not work. I think every time the user clicks the submit button the whole code resets instead of that the progression is saved.
What do i have to do? Do i have to use arrays to save the clicked information or do i have to use LOOPS? I tried around with both but i still have no solution for my problem.
What would be the simplest solution for the code i have in mind? One step further i want to create a simple text adventure with php. I have to learn PHP for a Test and i think to play around with the code a little will help me to understand how it works.
Here is the code i wrote but i think i have to rearrange it and to use other things istead of case to make it work.
<html>
<head>
<title>Abfrage</title>
</head>
<body>
<h4> Typentest</h4>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>"method="post">
<p>What would you chose?</p><br />
<input type="radio" id="a1" name="weapon" value="1" checked> <label for="a1"> Dagger</label><br>
<input type="radio" id="a2" name="weapon" value="2"> <label for="a2"> Sword</label><br>
<input type="radio" id="a3" name="weapon" value="3"> <label for="a3"> Saber</label><br>
<input type="submit" name="gesendet" value="ok"></button> <br />
</form>
<?php
if(isset($_POST['gesendet'])){
// Variablen deklararien und Werte zuweisen.
$weapon = $_POST['weapon'];
$count = 0;
//First chosing
if (!empty($weapon) && $count == 0){
switch($weapon){
case "1":
echo "You chose a crude dagger ";
$count = $count + 1;
break;
case "2":
echo "You chose a crude sword";
$count = $count + 1;
break;
case "3":
echo "you chose a crude saber";
$count = $count + 1;
break;
}
}
//second chosing after second time "submit"
if (!empty($alter) && $count == 1){
switch($alter){
case "1":
echo "you chose a fine dagger";
$count = $count + 1;
break;
case "2":
echo "you chose a fine sword";
$count = $count + 1;
break;
case "3":
echo "you chose a fine saber";
$count = $count + 1;
break;
}
}
else {
echo "Ihre Eingabe ist fehlerhaft ";
}
}
?>
</body>
Nope,
If I understand well, you want something like that (by registering your steps in the form $_POST['count']):
$count = 0;
if(isset($_POST['gesendet'])){
// Variablen deklararien und Werte zuweisen.
$alter = $_POST['alter'];
$count = $_POST['count'];
if (!empty($alter) {
if($count == 0){
switch($alter){
case "1":
echo " a";
$count = 1;
break;
case "2":
echo "b";
$count = 1;
break;
case "3":
echo "c";
$count = 1;
break;
default:
echo "whatever!<br>";//is this nescessary?
}
}
else
{
if($count == 1){
switch($alter){
case "1":
echo "d";
$count = 2;
break;
case "2":
echo "e";
$count = 2;
break;
case "3":
echo "f";
$count = 2;
break;
default:
echo "whatever<br>"; //is this nescessary?
}
}
}
}
But honestly, excepts if its an example and that your post values are not iterative, you can do it very easily with an array, something like that :
$answers = array("a","b","c");
$answer = "WhatEver";
if($alter < 4) $answer = $answers[$alter-1];
or
$answers = array("1" => "a","2" => "b","3" => "c");
$answer = "WhatEver";
if($answers[$alter]) $answer = $answers[$alter];
Very long time made php, so syntax to check but the logic is there.

PHP Background color change

i have an error with this code.
I want to change the background with the radio buttons, some help pls
<?php
if (!empty($_GET)) {
if ($_GET('kleur') == 'rood') {
echo '<body bgcolor="red">';
}
if ($_GET('kleur') == 'oranje') {
echo '<body bgcolor="orange">';
}
if ($_GET('kleur') == 'geel') {
echo '<body bgcolor="yellow">';
}
if ($_GET('kleur') == 'groen') {
echo '<body bgcolor="green">';
}
if ($_GET('kleur') == 'blauw') {
echo '<body bgcolor="blue">';
}
}
?>
Write a clean code.
Define array of possible colors:
$colorMap = [
'rood' => 'red',
'oranje' => 'orange',
'geel' => 'yellow',
'groen' => 'green',
'blauw' => 'blue'
];
And get color which you need (instead of DEFAULT_COLOR set default color):
if (isset($_GET['kleur']) && isset($colorMap[$_GET['kleur']])) {
$bgColor = $colorMap[$_GET['kleur']];
} else {
$bgColor = DEFAULT_COLOR
}
Than output your html:
echo '<body style="background-color: '.$bgColor.'">';
replace this,
if ($_GET('kleur') == 'blauw')
to
if ($_GET['kleur'] == 'blauw')
I think you have php syntax error
Here you are using if ($_GET('kleur') == 'rood') {
which is wrong it should be $_GET['kleur']
Make sure you do it in all your conditions.
for more on $_GET function click here
On your page body style you need to have a style attribute that will change based on clicked radio. and the easy way is to use switch.
<body style="<?php echo $body_style;?>">
</body>
<?php
$body_style=""; // so that we don't get undifined index on body style
if(isset($_GET['kleur'])){
switch ($_GET['kleur']) {
case 'rood':
$body_style = "background:red";
break;
case 'oranje':
$body_style = "background:orange"
break;
case 'geel':
$body_style ="background:yellow";
break;
case 'groen':
$body_style="background:green"
break;
case 'blauw':
$body_style="background:blue"
break;
default:
$body_style="";
break;
}
}
?>

How to pass the href variable to the switch case?

I have four links which are redirected on the same page only there variables are different for. So:
Square
Circle
Triangle
Rectangle
I want to write a switch case to pass the href variables so, that if the type is square, it should alert me, that it is a square and similarly for the others using jquery.
Hope your page is .php page
$optArr = array('Square','Circle','Triangle','Rectangle');
foreach($optArr as $opt)
{
echo "".$opt."";
}
You said all four links will redirect to the same page, but it shows tab1.php, tab1.php, tab2.php, and tab3.php. Anyway, you can do this:
Square
Circle
Triangle
Rectangle
tab1.php
<?php
$type = $_GET['type'];
switch ($type) {
case "square": echo "Square"; break;
case "circle": echo "Circle"; break;
case "triangle": echo "Triangle"; break;
case "rectangle": echo "Rectangle"; break;
default: echo "None";
}
?>
In PHP, you can do it like this,
if(isset($_GET['type'])){
$type = $_GET['type']; // get the type from the url
switch($type){
case 'square':
echo 'square';
break;
case 'circle':
echo 'square';
break;
case 'triangle':
echo 'square';
break;
case 'rectangle':
echo 'square';
break;
default:
echo 'not';
}
}

PHP get value from Drop down list

PHP get dropdown value and text
Hi I was following this post in the link above to get the users selected option from the drop down list and to do something depending on which is selected. I have a input box for the user to type a message and a drop down list of colors and if the user clicks on a certain color, it will take the users message and change it to that color but can't seem to work.
<select name="color" id="color">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
Here is the post
if($_POST['red'] = 'red' )
{
echo "<font color ='red'> Hi $name </font><br />";
} elseif ($_POST['submit'] = 'green')
{
echo "<font color ='green'> Hi $name </font><br />";
} elseif ($_POST['submit'] = 'blue')
{
echo "<font color ='blue'> Hi $name </font><br />";
}
All it does is make the font color red, no matter which drop down option was selected. I tried a variety of different things but can't seem to figure this out.
Take a look at the field you're POSTing:
<select name="color" id="color">
...
</select>
When accessing the $_POST array, you're not looking for the value that gets posted, you're looking for the field, which is, in this case, color, not red, green or blue.
You've got the right idea, but your if statement is looking for the wrong thing, and using the wrong = (should be == for comparison):
if($_POST['color'] == 'red'){
echo "<span style='color:red;'>Hi ".$name."</span>";
} else if ($_POST['color'] == 'blue'){
echo "<span style='color:blue;'>Hi ".$name."</span>";
} else if ($_POST['color'] == 'green'){
echo "<span style='color:green;'>Hi ".$name."</span>";
}
Fix those errors and your code should work. Also, look into using an IDE, which would show you these errors (especially the = vs == one).
You need to use the right variable in the array.
You also need to compare == and not assign =
if($_POST['color'] == 'red' ) {
echo "<font color ='red'> Hi $name </font><br />";
} elseif ($_POST['color'] == 'green') {
echo "<font color ='green'> Hi $name </font><br />";
} elseif ($_POST['color'] == 'blue') {
echo "<font color ='blue'> Hi $name </font><br />";
}
Or, as I would do it:
No if at all, just echo our the color.
echo "<font color='".$_POST['color']."'> Hi ".$name." </font><br />";
You can also try it as a switch statement:
switch($_POST['color']){
case "red":
echo "<font color='red'> Hi ".$name." </font><br />";
break;
case "blue":
echo "<font color='blue'> Hi ".$name." </font><br />";
break;
case "green":
echo "<font color='green'> Hi ".$name." </font><br />";
break;
default:
echo "Hi ".$name."<br />";
break;
}
You probably want to escape strings and such first. But that gives you an idea.
Change first line to:
if($_POST['color'] == 'red' )

Passing variable with URL but not able to make decision with Switch statement

On page1.php, i use
Download
On page2.php, i receive these variables correctly as iam able to echo those. But Switch statement doesn't work.
echo $_REQUEST["choice"];echo "<br>"; //printing variable to debug
echo $_REQUEST['item1'];echo "<br>"; //printing variable to debug
echo $_REQUEST['item2'];echo "<br>"; //printing variable to debug
switch((string)$_REQUEST["choice"]) {
case "Value1":
echo "Value 1 selected";
break;
case "Value2":
echo "Value 2 selected";
break;
case "Value3":
echo "Value 3 selected";
break;
default:
echo "No Value selected";
}
It always give: No Value selected . Please help, Thanks in advance.
<?php echo $value?> & item1=<?php echo $abs1?>
^ whitespace
better readability (tons of alternatives)
echo "Download";
You have an URL like the following:
page2.php?choice=VALUE & item1=VALUE & item2=VALUE
I noticed some spaces in there (before and after the &), so your choice value will have a trailing space. Either trim the choice string, or remove the whitespaces from the URL.
Use a var_dump($_REQUEST) to see the data you are receiving and also trim() value you are receiving.
switch(trim($_REQUEST["choice"])) {
case "Value1":
echo "Value 1 selected";
break;
case "Value2":
echo "Value 2 selected";
break;
case "Value3":
echo "Value 3 selected";
break;
default:
echo "No Value selected";
}

Categories