Jump to content

[SOLVED] URLs inbedded in php SWITCH stsatement


wejofost

Recommended Posts

I wish to "go to" a url as the result of a PHP SWITCH function.  Here is an example.  I reach this point via an HTML form which sends a value by POST like this

<form name="chooseroom" action="Calculate_values.php" method="post">

and I pick up the switch value like this in a php environment

 

<?php 
//set switch value
$switch_value= "$p_group1";

switch ($switch_value)
{
case "R1":
	echo "Go to Living room";
//  I want to put a url in here like <a href= "http://www.mysite.com/room1"></a>
?>
	 <a href = "http://www.yellohire.com/about.php"></a>
<?php 
	break;
case "R2":
	echo "Go to Room 2";
//  I want to put a url in here like <a href= "http://www.mysite.com/room2"></a>		
	break;
case "R3":
	echo "Go to room 3";
//  I want to put a url in here like <a href= "http://www.mysite.com/room3"></a>
	break;
default:
	echo "Go to any other room";
//  I want to put a url in here like <a href= "http://www.mysite.com/anyroom"></a>
	break;
}
?>

but I cannot get the transfer to the URL.

 

Help

 

(edited by kenrbnsn to put in the


tags)

Link to comment
Share on other sites

I found the "header (Location:" string in the online manual some time ago and have tried that several times and I get this error message.

Quote

Warning: Cannot modify header information - headers already sent by (output started at /home/yellohir/public_html/Calculate_values.php:14) in /home/yellohir/public_html/Calculate_values.php on line 47

 

The POST works OK as I get the correct "switch" value and  can process almost anything within the switch/case environment

 

???

Link to comment
Share on other sites

I think this is what you want:

<?php
//set switch value
$switch_value= "$p_group1";

switch ($switch_value)
{
case "R1":
	echo '<a href = "http://www.yellohire.com/about.php">Go to Living room</a><br>';
	break;
case "R2":
	echo '<a href= "http://www.mysite.com/room2">Go to Room 2</a><br>';
	break;
case "R3":
	echo '<a href= "http://www.mysite.com/room3">Go to room 3</a>';
	break;
default:
	echo "<a href= "http://www.mysite.com/anyroom">Go to any other room</a>";
	break;
}
?>

 

Ken

Link to comment
Share on other sites

???

Sorry to be a pain but that solution does not seem to work for me either???  Also that was the first solution I tried before I dug into the PHP header and location method that also doesn't work for me???

I am an old mainframe ex-programmer and trying to get to grips with PHP.

Perhaps I'm on a different planet or something.

I have put a test system together in a test environment.

Basically go to www.ychdb.co.uk/select_room_type.php and then select a room and submit.  You can see that the room numbers are POSTed correctly and the <A> tags are there via "view source" but interestingly the single quotes that top and tail them are not visible ( I have checked and I have put them in the original code on all cases ).

Absolutely infuriating to get stuck on such a silly point as the rest of the coding is going fine in a mixture of HTML and PHP.

Help!

Link to comment
Share on other sites

:-[  :-[ OK.  I have extracted the failing logic bits and put together 7 "pages" that should achieve what I want to with all the other logic removed and the fault is still there. ( These are all in www.ychdb.co.uk. )

The application is to allow users to calculate what space they require for the storage of their household goods.

I have attached the 7 pages.  You start at select_room_type.php and this will take you to go_to_room.php and then depending on your selection go on to living_room.php, dining_room.php, other_room.php and no_room.php. The end event is calculate.php

Thank you for your patience and time.

Wej Parry

 

[attachment deleted by admin]

Link to comment
Share on other sites

All that we need to see is the part of your code that processes the results from the form, not everything.

 

Here's a script I put together that uses your form and my switch statement. If this is not what you're looking to do, you will have to describe it better:

<?php
//set switch value
if (isset($_POST['submit']))
switch ($_POST['group1'])
	{
		case "R1":
			echo '<a href = "http://www.yellohire.com/about.php">Go to Living room</a><br>';
			break;
		case "R2":
			echo '<a href= "http://www.mysite.com/room2">Go to Room 2</a><br>';
			break;
		case "R3":
			echo '<a href= "http://www.mysite.com/room3">Go to room 3</a>';
			break;
		default:
			echo '<a href= "http://www.mysite.com/anyroom">Go to any other room</a>';
			break;
	}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Select room type</title>
</head>
<body>
<table width="100%" height="50%" border="2" bgcolor="green">
<tr>
<td></td>
<td>
<form name="chooseroom" method="post">
<div align="left"><br>
<input type="radio" name="group1" value="R1"> Living Room<br>
<input type="radio" name="group1" value="R2"> Dinning Room<br>

<input type="radio" name="group1" value="R3"> Kitchen<br>
<input type="radio" name="group1" value="R4"> Laundry/Utillity Room<br>
<input type="radio" name="group1" value="R5"> Bedroom<br>
<input type="radio" name="group1" value="R6"> Garage<br>
<input type="radio" name="group1" value="R7"> Other<br>
</div>
<input type="submit" value="submit" name="submit"><br />

</form>
</td>
<td bgcolor="white">Room for results
</td>
<td bgcolor="white"></td>
</tr>
</table>
</body>
</html>

 

Ken

Link to comment
Share on other sites

:-[  Ken Thank you for your help and patience.

I see that you have combined the select page code and the distribution page code into one page.

This completely covers the area where I have a problem.

I have taken the code you sent and just changed the destination urls to the correct destinations and it does not work for me.  See code below. Please try it?

<?php
//set switch value
if (isset($_POST['submit']))
switch ($_POST['group1'])
	{
		case "R1":
			echo '<a href = "http://www.ychdb.co.uk/living_room.php">Go to Living room</a><br>';
			break;
		case "R2":
			echo '<a href= "http://www.ychdb.co.uk/dining_room.php">Go to Room 2</a><br>';
			break;
		case "R3":
			echo '<a href= "http://www.ychdb.co.uk/other_room.php">Go to room 3</a>';
			break;
		default:
			echo '<a href= "http://www.ychdb.co.uk/no_room.php">Go to any other room</a>';
			break;
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Select room type</title>
</head>
<body>
<table width="100%" height="50%" border="2" bgcolor="green">
<tr>
<td></td>
<td>
<form name="chooseroom" method="post">
<div align="left"><br>
<input type="radio" name="group1" value="R1"> Living Room<br>
<input type="radio" name="group1" value="R2"> Dinning Room<br>
<input type="radio" name="group1" value="R3"> Kitchen<br>
<input type="radio" name="group1" value="R4"> Laundry/Utillity Room<br>
<input type="radio" name="group1" value="R5"> Bedroom<br>
<input type="radio" name="group1" value="R6"> Garage<br>
<input type="radio" name="group1" value="R7"> Other<br>
</div>
<input type="submit" value="submit" name="submit"><br />

</form>
</td>
<td bgcolor="white">Room for results
</td>
<td bgcolor="white"></td>
</tr>
</table>
</body>
</html>

Link to comment
Share on other sites

When I set the radio button on for the Living room selection and press Submit I know that the value "R1" is sent by the form and is visible to the SWITCH but the program flow does not go to the destination URL ( which is www.ychdb.co.uk/living_room.php ) and all other radio button selections are the same.

Thank you.

 

Link to comment
Share on other sites

At the beginning of the processing script put:

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
?>

 

This will dump what the processing script is being sent from the form.

 

I've modified my version to include that. I also modified the URLs in the switch to point to the same script:

<?php
//set switch value
if (isset($_POST['submit'])) {
echo '<pre>' . print_r($_POST,true) . '</pre>';
switch ($_POST['group1'])
	{
		case "R1":
			echo '<a href = "?room=living">Go to Living room</a><br>';
			break;
		case "R2":
			echo '<a href= "?room=room2">Go to Room 2</a><br>';
			break;
		case "R3":
			echo '<a href= "?room=room3">Go to room 3</a>';
			break;
		default:
			echo '<a href= "?room=anyroom">Go to any other room</a>';
			break;
	}
}
if (isset($_GET['room'])) echo 'Now in room [' . $_GET['room'] . ']<br>';
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Select room type</title>
</head>
<body>
<table width="100%" height="50%" border="2" bgcolor="green">
<tr>
<td></td>
<td>
<form name="chooseroom" method="post">
<div align="left"><br>
<input type="radio" name="group1" value="R1"> Living Room<br>
<input type="radio" name="group1" value="R2"> Dinning Room<br>

<input type="radio" name="group1" value="R3"> Kitchen<br>
<input type="radio" name="group1" value="R4"> Laundry/Utillity Room<br>
<input type="radio" name="group1" value="R5"> Bedroom<br>
<input type="radio" name="group1" value="R6"> Garage<br>
<input type="radio" name="group1" value="R7"> Other<br>
</div>
<input type="submit" value="submit" name="submit"><br />

</form>
</td>
<td bgcolor="white">Room for results
</td>
<td bgcolor="white"></td>
</tr>
</table>
</body>
</html>

 

You can see it in action at http://www.rbnsn.com/phpfreaks/rooms.php

 

Ken

Link to comment
Share on other sites

Ken.  You asked me to beter explain what I am trying to do.

How is this?

 

I am having a problem with the PHP Select/Case statement.

Background.  I am trying to create a series of web pages in PHP and HTML to allow users to calculate how much space they require for storage purposes of their household furniture.

I have created a series of web pages in a test environment at www.ychdb.co.uk.

The first page . ( see www.ychdb.co.uk/select_room_type.php ) presents a form to allow you to select a room type using radio buttons.  When the SUBMIT button is selected you go to www.ychdb.co.uk/go_to_room.php which has a PHP SELECT statement.  This is a series of CASE statements which I want to result in the user being transferred to different web-pages.  I am trying to use HTML anchor tags and URLs to direct the user to their selected web-page.  I can see that the correct value is being POSTed from the original form and that it is being interpreted correctly.  However the user is not directed to the new web page.  Everything else is working fine.

 

What am I doing wrong ( or perhaps is there a better way? )  Here is the code that is failing.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>


<title>Go To Room</title>


</head>

<body>
<font size = "5"> Room Selector </font>
<?php
// The next line shows me what the FORM data is for testing purposes
echo '<pre>' . print_r($_POST,true) . '</pre>';
$newline = "<br />";
echo $newline;
   import_request_variables('p', 'p_');
   echo $p_group1.$newline;

//set switch value
$switch_value= "$p_group1";

switch ($switch_value)
{
case "R1":
	echo '<a href = "http://www.ychdb.co.uk/living_room.php">Go to Room 1</a><br>';
	break;
case "R2":
	echo '<a href= "http://www.ychdb.co.uk/dining_room.php">Go to Room 2</a><br>';
	break;
case "R3":
	echo '<a href= "http://www.ychdb.co.uk/other_room.php">Go to room 3</a>';
	break;
default:
	echo '<a href= "http://www.ychdb.co.uk/no_room.php">Go to any other room</a>';
//	break;
}
?>
</body>
</html>

Link to comment
Share on other sites

Your problem is that you're not testing the correct variable. In your form you have

<input type="radio" name="group1" value="R1"> Living Room<br>

 

When you press the submit button, the value of the radio button selected is sent to to your processing program in $_POST['group1'], you are looking at the variable "$p_group1".

 

Change this:

<?php
$switch_value= "$p_group1";

switch ($switch_value)
?>

to

<?php
switch ($_POST['group1'])
?>

 

Ken

Link to comment
Share on other sites

Try this one on for size. It allows you to keep your layout a little easier to manage. You could even place your rooms into a separate folder to make things easier to manage.

 

<?php
if(isset($_POST['submitted']) && $_POST['submitted'] ==  true){
switch ($_POST['group1']){
	case "R1":
		include_once("living_room.php");
		break;
	case "R2":
		include_once("dining_room.php");
		break;
	case "R3":
		include_once("kitchen.php");
		break;
	case "R4":
		include_once("utility_room.php");
		break;
	case "R5":
		include_once("bedroom.php");
		break;
	case "R6":
		include_once("garage.php");
		break;
	case "R7":
		include_once("other_room.php");
		break;
	default:
		include_once("living_room.php");

}

}else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Select room type</title>
</head>
<body>
<table width="100%" height="50%" border="2" bgcolor="green">
<tr>
<td></td>
<td>
<form name="chooseroom" action="select_room_type.php" method="post">
<div align="left"><br>
<input type="radio" name="group1" value="R1"> Living Room<br>
<input type="radio" name="group1" value="R2"> Dinning Room<br>
<input type="radio" name="group1" value="R3"> Kitchen<br>
<input type="radio" name="group1" value="R4"> Laundry/Utillity Room<br>
<input type="radio" name="group1" value="R5"> Bedroom<br>
<input type="radio" name="group1" value="R6"> Garage<br>
<input type="radio" name="group1" value="R7"> Other<br>
</div>
<input type="hidden" name="submitted" value="true">
<input type="submit" value="submit" name="submit"><br />
</form>
</td>
<td bgcolor="white">Room for results
</td>
<td bgcolor="white"></td>
</tr>
</table>
</body>
</html>
<?php }?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.