PHP Form Problem

Hi all,

I have a php page (http://www.whatyouseeyoucannotbe.com/inves/freewill/fwa_test.php) that I need to make a couple of alterations to but don't know how to do it.

If the user selects a radio button anwser they are directed to a certain page. I also have a "add different answer" text field where the user can send an email to admin to suggest a different answer. The only problem is when the user sends another answer the page reloads (try it on the test page link above) and when they select a radio box answer the page does not go on to the appropriate page like it should but reloads the same page and displays the message "Your request has been successfully submitted."

Can anyone suggest an alteration to this code to fix this?

Thanks,

D
<?php 
session_start();
 
//if the user refreshes browser, you will get a duplicate email. This check avoid
//that duplicate email
if( isset($_SESSION['completed']) && 1==$_SESSION['completed'] )
 
 
{
	//you could redirect the user to some other page upon completion:
	//header("Location: otherpage.php");
 
	//or if wish, instead user is notified of the successful submission
	echo "Your request has been successfully submitted.";
 
	exit;
}
elseif(isset($_POST['go'])){
	$pagetogo = $_POST['radio'];
	if($pagetogo == 1){
		header("Location: fwb.php");
		exit;
	}
	if($pagetogo == 2){
		header("Location: fwb.php");
		exit;
	}
	if($pagetogo == 3){
		header("Location: fwb.php");
		exit;
	}
}
elseif(isset($_POST['Submit'])){
 
$comments = isset($_REQUEST['comments']) ? stripslashes(htmlspecialchars($_REQUEST['comments'])) : "";
 
	$to      = 'info@whatyouseeyoucannotbe.com';
	$subject = 'Additions';
  	$headers = 'From: "Comments from Site"<info@yourcompany.com>' . "\r\n" ;
 
 
    $message = "Comments:\n\n$comments\n\nURL: " . $_SERVER['PHP_SELF'];
	mail($to, $subject, $message, $headers);
	$_SESSION['completed'] = 1;
	//exit;
}
 
?>
 
 
<?php
 if( isset($_REQUEST['Submit']) && "Send Comment" == $_REQUEST['Submit'])
{
   echo ("<div>Your answer has been submitted successfully. Please now continue with the most appropiate answer.</div>");
}
?>
<p><br>
  Please select an option
</p>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label>
  <input type="radio" name="radio" id="radio" value="1" />
  </label>
  Maybe
  <br />
  <label>
  <input type="radio" name="radio" id="radio2" value="2" />
  </label>
  Yes
  <br />
  <label> 
  <input name="radio" type="radio" id="radio3" value="3">
  No
  <br>
  <br>
  <br>
  <input type="submit" name="go" id="submit" value="Continue" />
</label>
  <p>&nbsp;</p>
  <p onClick="document.getElementById('another').style.display='block';">Want to suggest another answer? <u>Click Here</u> &lt; </p>
  <p id="another" style="display: none;">Your Answer:
    <input name="comments" type="text" id="comments" <?php echo $comments;?>>
    <input type="submit" name="Submit" value="Send Comment">
  </p>
</form>

Open in new window

jones_bonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nplibCommented:
try removing the exits;

from all your if statements.

stuff inside an if statement aren't actually executed until the end of the if,

and if it's exited then the header will never fire.
0
jones_bonesAuthor Commented:
Hi,

So if I remove all of the exits: when I click on the continue button after selecting a radio button (after sending an email through the "Send Comment" already) it reloads the same page where I want it to go to the appropriate page listed in the php:

elseif(isset($_POST['go'])){
      $pagetogo = $_POST['radio'];
      if($pagetogo == 1){
            header("Location: fwz.php");
            //exit;
      }
      if($pagetogo == 2){
            header("Location: fwv.php");
            //exit;
      }
      if($pagetogo == 3){
            header("Location: fwb.php");
            //exit;
      }

see updated page for example: http://www.whatyouseeyoucannotbe.com/inves/freewill/fwa_test.php


Thanks,

D
0
nplibCommented:
did it work?
0
jones_bonesAuthor Commented:
No :(

As I explained in the previous post I want the page to go to the page as defined by the selection of the radio buttons. (Ie if radio button 1 is selected it goes to pg 1, radio button 2 pg 2 etc) This works fine if the "send comment" button is not pressed. However when the "send comment" button the page is reloaded and when I make a radio box selection and press "continue" this time it redirects me back to the same page instead of sending me on to the correct page.

I know the problem has something to do with this section of the code:

if( isset($_SESSION['completed']) && 1==$_SESSION['completed'] )


{
      //you could redirect the user to some other page upon completion:
      //header("Location: otherpage.php");
 
      echo "Your request has been successfully submitted.";
 
      //exit;
}

As I no longer want to display the successfully submitted message when the user is going on to the next page This has already been displayed when the user sends a comment and the page reloads using this piece of code at the top of the form:

?php
 if( isset($_REQUEST['Submit']) && "Send Comment" == $_REQUEST['Submit'])
{
   echo ("<div>Your answer has been submitted successfully. Please now continue with the most appropiate answer.</div>");
}
?>
<p><br>
  Please select an option
</p>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label>
  <input type="radio" name="radio" id="radio" value="1" />
  </label>
  Maybe
  <br />
  <label>
  <input type="radio" name="radio" id="radio2" value="2" />
  </label>
  Yes
  <br />
  <label>
  <input name="radio" type="radio" id="radio3" value="3">
  No
  <br>
  <br>
  <br>
  <input type="submit" name="go" id="submit" value="Continue" />
</label>
  <p>&nbsp;</p>
  <p onClick="document.getElementById('another').style.display='block';">Want to suggest another answer? <u>Click Here</u> &lt; </p>
  <p id="another" style="display: none;">Your Answer:
    <input name="comments" type="text" id="comments" <?php echo $comments;?>>
    <input type="submit" name="Submit" value="Send Comment">
  </p>
</form>

Therefore the echo statement at the top of the php code is now useless and I need a way to change this code around so that the when the "continue" button is pressed it always goes to the page as  defined by the radio button selection.

Sorry if I'm not explaining this very well :)

0
nplibCommented:
when I test it, It works,

it is sending me to fwb.php

the problem you might be having is when continue is clicked, regardless of which radio you have selected it selects the third one for you,
so when it gets to
elseif(isset($_POST['go'])){
      $pagetogo = $_POST['radio'];
      if($pagetogo == 1){
            header("Location: fwz.php");
            //exit;
      }
      if($pagetogo == 2){
            header("Location: fwv.php");
            //exit;
      }
      if($pagetogo == 3){
            header("Location: fwb.php");
            //exit;
      }
$pagetogo will always = 3;

have the javeacript stop doing that, then it will solve your problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.