0

So this is a basic form submission and processing, bare bones. There are three scripts, the form script, the processor script and the view page. Here are snippets from each:

Form:

 <form action="processor.php" method="post">
 <h4>Question # 1</h4>
 <p>What grade are you in?</p>
 <label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
 <label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
 <label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
 <label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
 <div class="button">
 <button class="btn btn-primary" input type="submit" name="submit" href="processor.php">Submit</button>
 </div>
 </form>

The Processor Script:

 <?php
 header("Location: viewpage.html");
 $grade = $_POST['grade'];
 function Grades () {
 if ($grade =="1") {
 echo "You're a freshmen";
} elseif ($grade == "2") {
 echo "You're a sophomore";
} elseif ($grade == "3") {
 echo "You're a junior.";
} elseif ($grade == "4") {
 echo "You're a senior.";
} else {
 echo "Something is wrong.";
}
}
?>

The View Page:

 <h4>Question # 1</h4>
 <p><?php Grades();
 ?>
 </p>

Essentially, I'm trying to get the user to fill out the form, process it and spit out input. For example, on the form they say they are a Freshmen, they click submit and are taken to the view page where it spits out "You are a freshmen." It's been a while since I coded and this is a pretty simple issue I can't seem to find a solution to. Any help would be greatly appreciated. Thank you.

As is, when I get to the view page it shows nothing.

asked Apr 5, 2013 at 15:00
3
  • 1
    And the problem is...? Other than your processor basically aborting processing by redirecting to another page? And some variable scoping issues... And some unclear "how do these scripts relate to each other" issues... Commented Apr 5, 2013 at 15:02
  • I made an edit after the original post. I created the processor page so it could be processed on the back end and I can keep PHP and HTML separate. Is there a more efficient way? The problem I have is when it gets to the view page script the function spits out nothing. Commented Apr 5, 2013 at 15:07
  • Could you clarify on how these scripts relate to each other? Do you mean how I have connected each one to the other? Commented Apr 5, 2013 at 15:08

5 Answers 5

2

The first problem is that the first thing you do in your processor script is issue a header redirect. The script might generate output after that, but the client will never see it. The second is that your program structure is all off. Ideally you'd want to use sessions to store the 'grade' value between the processor and view scripts, or combine them into one.

<?php
$grade = $_POST['grade'];
function Grades ($input) {
if ($input =="1") {
 echo "You're a freshmen";
} elseif ($input == "2") {
 echo "You're a sophomore";
} elseif ($input == "3") {
 echo "You're a junior.";
} elseif ($input == "4") {
 echo "You're a senior.";
} else {
 echo "Something is wrong.";
}
?><html>
<head></head>
<body>
<h4>Question # 1</h4>
<p><?php Grades($grade);?></p>
</body>
</html>

Or, alternatively:

<?php
session_start();
$_SESSION['grade'] = $_POST['grade'];
header('Location: viewpage.php');

and

<?php
session_start();
$grade = $_SESSION['grade'];
function Grades ($input) {
if ($input =="1") {
 echo "You're a freshmen";
} elseif ($input == "2") {
 echo "You're a sophomore";
} elseif ($input == "3") {
 echo "You're a junior.";
} elseif ($input == "4") {
 echo "You're a senior.";
} else {
 echo "Something is wrong.";
}
?><html>
<head></head>
<body>
<h4>Question # 1</h4>
<p><?php Grades($grade);?></p>
</body>
</html>

You also need to keep in mind is that unless you include() one script into another each script is completely oblivious to the others. They do not know what variables or functions were defined, with the sole exception of session variables.

answered Apr 5, 2013 at 15:13
Sign up to request clarification or add additional context in comments.

1 Comment

@buttonitup the first block would replace both the 'processor' and 'view' scripts with a single script. The other two blocks below the 'Or:' would replace the 'processor' and 'view' scripts respectively.
1

It should look something like that:

<?php
 function Grades () {
 $grade = $_POST['grade'];
 if ($grade =="1") {
 echo "You're a freshmen";
} elseif ($grade == "2") {
 echo "You're a sophomore";
} elseif ($grade == "3") {
 echo "You're a junior.";
} elseif ($grade == "4") {
 echo "You're a senior.";
} else {
 echo "Something is wrong.";
}
}
include('viewpage.php');
?>

Note that you should rename your viewpage.html to viewpage.php

answered Apr 5, 2013 at 15:10

1 Comment

Thanks man, that did it. Just a bit rusty. Thanks for the help.
1

It looks to me like you're receiving the post, then immediately redirecting to another page without sending any of the variables you just received. So you need to send along the grade field as a GET parameter. And you need to define your method/variable within the view file.

Processor:

<?php
 // If you're going to redirect, you MUST send along the grade
 header("Location: viewpage.html?grade=" urlencode($_POST['grade'));
?>

View:

<?php
 // In your view you only have access to variables in your immediate request.
 $grade = $_GET['grade'];
 function Grades ($grade) {
 if ($grade =="1") {
 echo "You're a freshmen";
 } elseif ($grade == "2") {
 echo "You're a sophomore";
 } elseif ($grade == "3") {
 echo "You're a junior.";
 } elseif ($grade == "4") {
 echo "You're a senior.";
 } else {
 echo "Something is wrong.";
 }
 }
?>
<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>

And unless you seriously need the processor to save data with:

 <form action="viewpage.html" method="get">
 <h4>Question # 1</h4>
 <p>What grade are you in?</p>
 <label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
 <label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
 <label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
 <label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
 <div class="button">
 <button class="btn btn-primary" type="submit" name="submit">Submit</button>
 </div>
 </form>
answered Apr 5, 2013 at 15:09

2 Comments

I tried this way but still got no output on the view page. You mentioned assuming I'm including the view from my processing script. I assume that's where the problem lies? Could you please elaborate?
@buttonitup Wow, I didn't even see the redirect. That's your problem!
0

You can use some functions to redirect for different inputs like fresher... for doing this you have to create seperate pages and use redirect("Pagename"); I think it might be a solution for your problem

answered Apr 5, 2013 at 15:09

Comments

0

You need to pass the POST variable as a parameter to your function.

answered Apr 5, 2013 at 15:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.