...

View Full Version : Processing a Form using php Question



gregs
06-20-2010, 06:05 AM
Hi - I am a new php coder, and need some advice to help me process a form using php.

What I have on the form are 4 radio buttons - and based on the user's selection, I need to jump to one of four different .php pages.

What happens is - it only jumps to the correct php page THE SECOND TIME the "Submit" button is hit. I'd like it to jump to the correct php page the very first time the user hits the button.

Here are the relevant portions of php code:



if (isset($_POST['send'])) {
switch ($_POST['radio']) {
case 'change_email':
$editFormAction = 'change-email_working.php';
break;
case 'Optout':
$editFormAction = 'change-optout_working.php';
break;
case 'Remove_Completely':
$editFormAction = 'change-remove_working.php';
break;
case 'No_changes':
$editFormAction = '';
break;
default:
$editFormAction = '';
break;
}
}


And here is what the form code looks like:


<form action="<?php echo $editFormAction; ?>" method="POST" name="form2" target="_blank" id="form2">
<input name="radio" type="radio" id="change_email" value="change_email" />
Change your Current Registered Email Address<br />
<input name="radio" type="radio" id="Optout" value="Optout" />
Opt Out from future Emailings<br />
<input name="radio" type="radio" id="Remove_Completely" value="Remove_Completely" />
Remove Yourself Completely from our database<br />
<input name="radio" type="radio" id="No_changes" value="No_changes" checked="checked" />
Not make any changes<br /><br />
<input name="send" type="submit" class="style51" value="Submit" id="send" />
</form>


If I echo the $editFormAction value on the top of the new _blank page, it displays the correct/anticipated value ... but it never opens the correct page. It is like I am always one iteration behind (since it will open the correct page if I hit Submit a SECOND time after re-selecting the same button).

Can anyone tell me what I am doing wrong, or how to fix this?

Thanks in advance for your wisdom!

gregs
06-20-2010, 06:23 PM
I have come up with a solution - and am wondering if this is the best way to handle this.

When the user hits the Submit button, I jump to a different php file that ONLY performs a redirect to one of four pages, based on which radio button that was selected.

In other words, I made the form action=choose-mod.php - and within choose-mod.php, I check the value of the $_POST['radio'], and then use a switch statement (same as above) to jump to one of four different php files using the header(sprintf("Location: %s", target_file)); statement.

The above works - but is there a better way? Any thoughts are definitely appreciated!

blankskull
06-20-2010, 06:29 PM
Hi, unfortunately I can't help you because I am a beginner. Anyway can you tell what is the meaning of

$something=' '

why is there a blank between the two inverted commas (' ')?

Again, are (! issetxxx) and (issetxxx !) the same?

_Aerospace_Eng_
06-20-2010, 06:29 PM
No that is how it should be. You get the user's checked radio and then redirect based on that. No need to resubmit the form. You probably don't need to use sprintf in this case as you have the possible values already defined. Your default case however should contain a value at least otherwise you will get a header with no location defined.


Hi, unfortunately I can't help you because I am a beginner. Anyway can you tell what is the meaning of

$something=' '

why is there a blank between the two inverted commas (' ')?

Again, are (! issetxxx) and (issetxxx !) the same?

There isn't a blank there, its just setting it to an empty string. And no !isset and isset()! are not the same thing. The second one will give you a parse error.

120
06-20-2010, 06:35 PM
It's not working because you are relying on the form action being changed which will require two clicks.

If you want to do it that way I'd use a redirect, something like this:


if (isset($_POST['send'])) {
switch ($_POST['radio']) {
case 'change_email':
header( 'Location: change-email_working.php' ) ;
break;
case 'Optout':
header( 'Location: change-optout_working.php' ) ;
break;
case 'Remove_Completely':
header( 'Location: change-remove_working.php' ) ;
break;
case 'No_changes':
$editFormAction = '';
break;
default:
$editFormAction = '';
break;
}
}

Personally I would do my processing in a single page with logic therein, but your system will work. It just requires you to maintain several pages.

gregs
06-20-2010, 07:09 PM
Hi, unfortunately I can't help you because I am a beginner. Anyway can you tell what is the meaning of

$something=' '

why is there a blank between the two inverted commas (' ')?

The answer is - I did not put the filename to redirect to in between the two 's. Just an oversight on my part.


Personally I would do my processing in a single page with logic therein, but your system will work. It just requires you to maintain several pages.

I tried to do this on a single php page, but there needs to be a bunch of HTML bounced to the user when the page first loads, and so I kept getting the "headers already sent" error.



... You probably don't need to use sprintf in this case as you have the possible values already defined. Your default case however should contain a value at least otherwise you will get a header with no location defined.

I'm struggling with the syntax of how to call this correctly:

This works: header(sprintf("Location: %s", $editFormAction));
This does not work: header('Location: %s', $editFormAction);

Can you give me a pointer to make the second of the above correct?

Thanks!

120
06-20-2010, 07:43 PM
Nothing wrong with being a beginner, so am I ;-)

If you need to dump HTML output on the first load, go ahead and do it using an 'else' for your 'isset($_POST['send']' set. That is, if it's the first load of the page without the form being submitted the 'else' will run (here using the HEREDOC wrappers to make the concept clearer I hope)


if (isset($_POST['send'])) {
switch ($_POST['radio']) {
case 'change_email':
header( 'Location: change-email_working.php' ) ;
break;
case 'Optout':
header( 'Location: change-optout_working.php' ) ;
break;
case 'Remove_Completely':
header( 'Location: change-remove_working.php' ) ;
break;
case 'No_changes':
$editFormAction = '';
break;
default:
$editFormAction = '';
break;
}
} else {
$html = <<<EOH
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Your Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Expires" content="Tue, 01 Jan 1980 1:00:00 GMT">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<link rel="stylesheet" type="text/css" href="yourcss.css">
<script type="text/javascript" src="yourjs.js"></script>
</head>
<body>
<p>your html here</p>
</body>
</html>
EOH;
echo $html;
}


Hope that helps or gives you some idea.

Personally I would break it down further into creating functions for displaying certain elements / scenarios - but don't run before you can walk :-)

gregs
06-20-2010, 08:28 PM
Thanks guys! I think I am over the hump at this point.

120 - for a beginner, you are a lot further down the learning curve than me. Thanks for the suggestions and help!

blankskull
06-21-2010, 05:24 AM
Hi, Gregs,

I used the later code. But it shows a warning:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\poll.php:3) in C:\xampp\htdocs\test\poll.php on line 15

The Code:

<?php


if (isset($_POST['send'])) {
switch ($_POST['radio']) {
case 'change_email':
header( 'Location: change-email_working.php' ) ;
break;
case 'Optout':
header( 'Location: change-optout_working.php' ) ;
break;
case 'Remove_Completely':
header( 'Location: change-remove_working.php' ) ;
break;
case 'No_changes':
$editFormAction = '';
break;
default:
$editFormAction = '';
break;
}
}

?>

<form action="<?php echo $editFormAction; ?>" method="POST" name="form2" target="_blank" id="form2">
<input name="radio" type="radio" id="change_email" value="change_email" />
Change your Current Registered Email Address<br />
<input name="radio" type="radio" id="Optout" value="Optout" />
Opt Out from future Emailings<br />
<input name="radio" type="radio" id="Remove_Completely" value="Remove_Completely" />
Remove Yourself Completely from our database<br />
<input name="radio" type="radio" id="No_changes" value="No_changes" checked="checked" />
Not make any changes<br /><br />
<input name="send" type="submit" class="style51" value="Submit" id="send" />
</form>

120
06-21-2010, 06:39 AM
Make sure you are not sending any html before you get to the chunk (isset($_POST['send'])) ....

May be better if you post the whole script here (please use the PHP button in the editor to wrap it in tags)

DJCMBear
06-21-2010, 07:48 AM
Try this.



<?php
if(isset($_POST['send']))
{
switch($_POST['radio'])
{
case 'change_email':
$ref = 'change-email_working.php';
break;
case 'Optout':
$ref = 'change-optout_working.php';
break;
case 'Remove_Completely':
$ref = 'change-remove_working.php';
break;
case 'No_changes':
$ref = '';
break;
default:
$ref = '';
break;
}

if(!empty($ref))
{
header("Location: {$ref}");
}
}
else
{
print <<<EOD
<form action="" method="post" name="form2" target="_blank" id="form2">
<input name="radio" type="radio" id="change_email" value="change_email" />
Change your Current Registered Email Address<br />
<input name="radio" type="radio" id="Optout" value="Optout" />
Opt Out from future Emailings<br />
<input name="radio" type="radio" id="Remove_Completely" value="Remove_Completely" />
Remove Yourself Completely from our database<br />
<input name="radio" type="radio" id="No_changes" value="No_changes" checked="checked" />
Not make any changes<br /><br />
<input name="send" type="submit" class="style51" value="Submit" id="send" />
</form>
EOD;
?>

gregs
06-22-2010, 12:16 AM
Thanks - I definitely appreciate the excellent input!!! You guys are awesome!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum