Matthieu McLaren
Matthieu McLaren

Reputation: 224

Processing form data with PHP

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.

Upvotes: 0

Views: 124

Answers (5)

Sammitch
Sammitch

Reputation: 32232

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.

Upvotes: 2

Robert K
Robert K

Reputation: 30328

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>

Upvotes: 1

barbashov
barbashov

Reputation: 542

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

Upvotes: 1

pulsar
pulsar

Reputation: 987

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

Upvotes: 0

Sathishkumar
Sathishkumar

Reputation: 109

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

Upvotes: 0

Related Questions