Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JelvisChan

macrumors member
Original poster
Jan 9, 2009
77
0
Hello everyone,

I am trying to create a form handling php script that, if a certain radio button is checked, you will be redirected to another location depending on which radio button was submitted.

This is sort of what I have, just changed text:

HTML CODE:
HTML:
<form action="redirect.php" method="post">
	
	<input type="radio" name="item" value="books"
>Barnes & Noble

<input type="radio" name="item" value="clothes"
>American Eagle
	
	
    <input type="submit" name="ok" value="OK" />
    <input type="submit" name="cancel" value="Forget This Transaction" />
</form>


And then the PHP CODE that handles the form:

redirect.php
PHP:
<?php

	$radio_selected = $_POST['item'];
	
	if ($radio_selected == "books") {
		
		header("Location: http://www.barnesandnoble.com");
	}
	
	else {
		
                header("Location: http://www.ae.com");
		
	}




?>

The solution would be greatly appreciated.

Thanks.
 
My problem is that I tried testing it and it doesn't work. It only sends me to the last link, even if I check the first radio button.

The site you posted doesn't help me.

My question:

How do you use PHP to redirect visitors to different URLs depending on which radio button you checked?

Thanks
 
The code you posted works fine for me, that's why I asked about errors. It redirects as you would expect. Do you have access to the PHP error log file? I would look there for clues. If you don't know where it is try adding this to the top of your PHP code.

PHP:
error_reporting(E_ALL);
ini_set('display_errors', 'On'); // on for development, off otherwise
ini_set('log_errors', 'On');
ini_set('error_log', 'path/phperr.log');
Be sure to change the error log path on that last line. It needs an absolute path too. These lines turn on logging and display errors to the screen so you don't need to check the log files.
 
Try this to debug with:

PHP:
ini_set('display_errors', 'On');
if(isset($_POST['item'])) {

$radio_selected = $_POST['item'];
if($radio_selected == "books") {
header("Location: http://www.barnesandnoble.com");
} elseif ($radio_selected == "clothes") {
header("Location: http://www.ae.com");
} 
} else {
echo "Error: The radio fields did not have a value of books or clothes. It had: $radio_selected";
} else {
die("POST request not specified!");
}

I didn't test it but I am confident it works :)
By the way it's good practice to check if the post request was sent.
 
No, none of your solutions worked.

I put in the path to the php error file and tested the script, but it only went to my 2nd URL even when I ticked the first radio button.

For the php script you provided, It only gave me a blank screen when I submitted the form.

Anyone have any last minute scripts that I could try or something?

Thanks again!
 
How about just trying to print the value of $_POST['item'] and/or $_POST?

PHP:
<?php

    print($_POST['item']);
    print_r($_POST);

?>
 
this worked for me:

PHP

PHP:
<?php

if(filter_has_var(INPUT_POST, 'ok')){
	$selected = $_POST['item'];
	
	switch($selected){
		case "books" :
			header("Location: http://www.barnesandnoble.com");
			break;
		case "clothes" :
			header("Location: http://www.ae.com"); 
			break;
	
	}
}

?>
 
this worked for me:

PHP

PHP:
<?php

if(filter_has_var(INPUT_POST, 'ok')){
	$selected = $_POST['item'];
	
	switch($selected){
		case "books" :
			header("Location: http://www.barnesandnoble.com");
			break;
		case "clothes" :
			header("Location: http://www.ae.com"); 
			break;
	
	}
}

?>



BLANK SCREEN
 
Everyone I figured out what I did wrong (sort of wrong):

Instead of using the if..else statement, I used both if...if

PHP:
<?php

if ($selected_radio == "books") {
header("Location: www.url.com");
}

if ($selected_radio == "clothes") {
header("Location: www.otherurl.com");
}

Thanks for all the help -- much appreciated!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.