How to pass the href variable to the switch case? - php

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';
}
}

Related

based on the status column using ifelse how to display diff background colors in td

in the while loop data based on the statusid value how to display different background colors
Code show here
echo "<tr>
<td>".$result['exportid']."</td>
<td>".$result['deb_nmr']."</td>
<td>".$result['cost_name']."</td>
<td>".$result['numb_pal']."</td>
<td>".$result['tot_weight']."</td>
<td>".$result['pb_s']."</td>
<td>".$result['date']."</td>
<td>".$result['statusid']."</td>
</tr>";
}
You can use switch inside looping and before echo statement to get color as below:
$color = "";
switch ($result['statusid']) {
case "1":
$color = "#ddd";
break;
case "2":
$color = "#eee";
break;
case "3":
$color = "#bbb";
break;
default:
$color = "#fff";
}
and then can use $color variable to apply color on TD as below:
echo "<tr>
<td>".$result['exportid']."</td>
<td>".$result['deb_nmr']."</td>
<td>".$result['cost_name']."</td>
<td>".$result['numb_pal']."</td>
<td>".$result['tot_weight']."</td>
<td>".$result['pb_s']."</td>
<td>".$result['date']."</td>
<td bgcolor='".$color."' >".$result['statusid']."</td>
</tr>";
Hope it helps you.

php form redirects to different file instead of uploading SQL query

I'm currently working on a webpage connected to a database for a football league. I constructed the page in the form of a table with a fixed header, menu, login bar and one cell (Main) which displays the page selected in the menu (for example, all the games' results when "Results" has been selected on the menu).
Everything works neatly, until we arrive to the point where a page displayed in the main has two forms linked with the database. Every time, one of the submit buttons redirects my page to another position from the menu without performing the SQL query... What is odd is that if I load the concerned file alone on a webpage, all actions are performed... But when it is part of a bigger whole, it doesn't work properly.
Here is the code for the cell which displays the Main content:
<?php
$id = $_REQUEST["id"];
$lang=$_REQUEST["lang"];
switch($id)
{
case 1:
switch($lang){
case pl: include "Main/PL/index.php"; break;
case en: include "Main/EN/index.php"; break;
default: include "Main/PL/index.php"; break;
}; break;
case 2:
switch($lang){
case pl: include "News/PL/index.php"; break;
case en: include "News/EN/index.php"; break;
default: include "News/PL/index.php"; break;
}; break;
case 3:
switch($lang){
case pl: include "Tables/PL/index.php"; break;
case en: include "Tables/EN/index.php"; break;
default: include "Tables/PL/index.php"; break;
}; break;
case 4:
switch($lang){
case pl: include "Results/PL/index.php"; break;
case en: include "Results/EN/index.php"; break;
default: include "Results/PL/index.php"; break;
}; break;
case 5:
switch($lang){
case pl: include "Fixtures/PL/index.php"; break;
case en: include "Fixtures/EN/index.php"; break;
default: include "Fixtures/PL/index.php"; break;
}; break;
case 6:
switch($lang){
case pl: include "Teams/PL/index.php"; break;
case en: include "Teams/EN/index.php"; break;
default: include "Teams/PL/index.php"; break;
}; break;
case 7:
switch($lang){
case pl: include "Contact/PL/index.php"; break;
case en: include "Contact/EN/index.php"; break;
default: include "Contact/PL/index.php"; break;
}; break;
default:
switch($lang){
case pl: include "Main/PL/index.php"; break;
case en: include "Main/EN/index.php"; break;
default: include "Main/PL/index.php"; break;
}; break;
}
?>
And here is one example of forms which perform badly - forms for Adding and Deleting players from a team:
<!DOCTYPE html>
<html>
<body>
<!-- First form for adding a player -->
<h3>Add a Player to Your Team</h3><br>
<form method='POST' action='index.php?id=6&lang=en'>First name: <input type=\"text\" name=\"imie\"><br>
Last name: <input type=\"text\" name=\"nazwisko\"><br>
<input type='submit' name='dodaj' value='Add'></form><br>
<?php
//what happens when button add player is selected
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (isset($_POST['dodaj'])){
//checks if data is input
if (empty($_POST['imie']) || empty($_POST['nazwisko'])){
echo "Provide the name of the player!";
}
else {
//verify that the player has not already been added
$imie=$_POST['imie'];
$nazwisko=$_POST['nazwisko'];
$sql="SELECT * FROM `Zawodnik` WHERE `Zawodnik_Imie`='$imie' AND `Zawodnik_Nazwisko`='$nazwisko'";
$result=mysqli_query($conn, $sql);
$czyjest=mysqli_num_rows($result);
if ($czyjest >= 1){
echo "The player has already been added!";
}
else {
//add the player
$sql="INSERT INTO `Zawodnik`(`Zawodnik_Imie`, `Zawodnik_Nazwisko`, `Druzyna_Id_Druzyna`) VALUES ('$imie', '$nazwisko', '$iddruzyna')";
$result=mysqli_query($conn, $sql);
if (!$result){
die ("Could not enter data ".mysqli_error($conn));
}
else {echo "Player added!";}
}
}
}
}
?>
<!-- Second form for deleting a player -->
<h3>Remove Player</h3><br>
<form method='POST' action='index.php?id=5&lang=en'>ID nr: <input type='number' name='id'><br>
<input type='submit' name='usun' value='Delete'></form><br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (isset($_POST['usun'])){
//verify if the player ID has been input
if (empty($_POST['id'])){
echo "Provide Player ID!";
}
else {
//Delete player
$id=$_POST['id'];
$sql="DELETE FROM `Zawodnik` WHERE `Id_Zawodnik`=$id AND `Druzyna_Id_Druzyna`=$iddruzyna";
$result=mysqli_query($conn, $sql);
if (!$result){
die ("Could not delete data ".mysqli_error($conn));
}
else {
$sql="SELECT * FROM `Zawodnik` WHERE `Id_Zawodnik`=$id";
$result=mysqli_query($conn, $sql);
$czyusuniety=mysqli_num_rows($result);
if($czyusuniety<1){
echo "Player removed!";}
else {
echo "You can't remove a player from a different team!";
}
}
}
}
}
}
mysqli_close($conn);
?>
</body>
</html>
Thanks for your help!
it looks like you add wrong page id (once it is 6, and once it is 5) in the forms action attribute.
For adding you have:
<form method='POST' action='index.php?id=6&lang=en'>
and for deletes:
<form method='POST' action='index.php?id=5&lang=en'>
Oh and also you use the same name for GET and POST so the $_REQUEST['id'] will actually have the posted value not the url parameter.

Switch Statements not working

I'm trying to use the switch statements but it's not working. My problem is for example i input LazyBoy in the textbox, that should echo LazyBoy else echo another string.
<?php
$classmap = $_POST['classmap'];
switch ($classmap) {
case "LazyBoy":
echo "You're Lazy!";
break;
case "GrayHounds":
echo "You're Gray!";
break;
}
?>
Here is the form -
<form action="checkout.php" method="post" >
<input type="hidden" name ="classmap" value="<?php include('db.php');
$origin = $_POST['origin'];
$class = $_POST['class'];
$daten = $_POST['daten'];
$result = mysql_query("SELECT * FROM route WHERE route LIKE '%$origin%' AND type LIKE '%$class%' AND date LIKE '%$daten%' ");
while($row = mysql_fetch_array($result))
{
echo $row['type'];
} ?>">
</form>
You just need to check if the post variable is set and/or add a default case :
if(isset($_POST['classmap'])) {
$classmap = $_POST['classmap'];
switch ($classmap) {
case "LazyBoy":
echo "Your Lazy!";
break;
case "GrayHounds":
echo "Your Gray!";
break;
default:
echo "Something";
break;
}
}
else {
echo "Something";
}
Make sure you post to the right script.
Try to put default statement to debug.
$classmap = $_POST['classmap'];
switch ($classmap) {
case "LazyBoy":
echo "You're Lazy!";
break;
case "GrayHounds":
echo "You're Gray!";
break;
default:
echo "does not match with previous cases";
}
First you have to post values to define the classmap index.
<form method="post">
<input type="text" name="classmap" value="LazyBoy"/>
<input type="submit" value="submit"/>
</form>
<?php
$classmap = (isset($_POST['classmap']) ? $_POST['classmap'] : null);
switch ($classmap) {
case "LazyBoy":
echo "You're Lazy!";
break;
case "GrayHounds":
echo "You're Gray!";
break;
}
?>
Your Code is correct.
I guess there is a Problem with the $_POST.
Try to verify it.
$classmap = "LazyBoy";

Calling and Declaring a select box in 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";
}

How to pass the value of a <select> drop-down list to function in another file using javascript

FIXED; it was a case of adding another include into the fancount.php file!
Initial code:
<div class="l-title">
<div class="t-left"></div>
<div class="t-mid">Popularity Update</div>
<div class="t-right"></div></div>
<?php makechoice($cat_array); ?>
Function code:
function makechoice($cat_array) {
global $djnames;
global $djids;
global $djurl;
$dbz = new db();
$sim = new simple();
echo '<div class="choose-section">';
echo '<select class="selbox" onchange="categoryAjaxData(\'facebook\',\'/includes/fancount.php\',this.value);">';
foreach($cat_array as $cat) {
echo('<option value="'.$cat[0].'">'.$cat[1].'</option>');
}
echo '</select>';
echo 'Choose a section';
echo '</div>';
echo '<div id="facebook"><div class="facebook-midcut">',showfans($djnames, $djids, $djurl),'</div><div class="l-botcut"></div></div>';
}
Code from fancount.php
<?php
include_once("../_inc/global.php");
include_once("../_inc/dbmysql.php");
include_once("../_inc/simple.php");
$dbz = new db();
$sim = new simple();
switch ($_GET['catid']) {
case 1:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($barnames, $barids, $barurl),'</div><div class="l-botcut"></div></div>';
break;
case 2:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($beachrestaurantnames, $beachrestaurantids, $beachrestauranturl),'</div><div class="l-botcut"></div></div>';
break;
case 3:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($clubnames, $clubids, $cluburl),'</div><div class="l-botcut"></div></div>';
break;
case 4:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($clubnightnames, $clubnightids, $clubnighturl),'</div><div class="l-botcut"></div></div>';
break;
case 5:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($djnames, $djids, $djurl),'</div><div class="l-botcut"></div></div>';
break;
case 6:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($hotelnames, $hotelids, $hotelurl),'</div><div class="l-botcut"></div></div>';
break;
case 7:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($liveshownames, $liveshowids, $liveshowurl),'</div><div class="l-botcut"></div></div>';
break;
case 8:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($restaurantnames, $restaurantids, $restauranturl),'</div><div class="l-botcut"></div></div>';
break;
case 9:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($sunsetnames, $sunsetids, $sunseturl),'</div><div class="l-botcut"></div></div>';
break;
case 10:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($villanames, $villaids, $villaurl),'</div><div class="l-botcut"></div></div>';
break;
default:
echo '<div id="facebook"><div class="facebook-midcut">',showfans($djnames, $djids, $djurl),'</div><div class="l-botcut"></div></div>';
}
?>
Ajax code:
function categoryAjaxData(div,str,value)
{
var url = str+'?catid='+value;
ajaxData(div,url);
}
function ajaxData(div,str)
{
xmlHttp=GetXmlHttpObject();
document.getElementById(div).innerHTML='<center><img src="images/loader.gif"></center>';
if(xmlHttp==null)
{
alert("Browser does not support HTTP Request")
return
}
var url = str;
xmlHttp.onreadystatechange = function(){ DescriptionstateChanged(div); };
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function DescriptionstateChanged(div)
{
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById(div).innerHTML=xmlHttp.responseText;
}
}
I feel it in my waters that I'm almost there... Can anyone offer a solution... MarioVW, Mr Font of Knowledge - can you bestow enlightenment upon my head?
I'm assuming this question is a follow-up to Changing the contents of a div using a dropdown, javascript and PHP
That being said, you just need to pass the variable as a GET parameter in your AJAX request, which it seems you are already doing (see categoryAjaxData() in the other question). Create a new PHP script with basically just the switch statement (and any other setup code), but instead of using $_POST['fandropdown'] use $_GET['catid'].
WHile not an expert in it, sounds like a job for AJAX
It sounds like you want arrays.
showfans($names[$_POST['selectname'...

Categories