Jump to content

$_SERVER['PHP_SELF'] and validation & redirection


rbragg

Recommended Posts

I have pages 1 & 2. Page 1 contains a form that is posting to itself in order to validate after a user submits. If validation is successful, this page is directed to Page 2 (confirmation). I know you normally would not use sessions unless, for example, you needed to pass values from Page 2 to a Page 3. However, since the form action on Page 1 is not Page 2 (instead the action is to itself), my values are not being passed to Page 2. What is a simple way to solve this? I know it must be right under my nose.

Thanks in advance.

Link to comment
Share on other sites

Should be [code=php:0]$_SERVER['PHP_SELF'][/code] not [code=php:0]$_SESSION['PHP_SELF'][/code] by the way.

The following code will assign all your form values to session variables, and then echo them to check they're being stored correctly, give it a try.

[code]<?php
// Start the session
session_start();

// Loop through
foreach ($_REQUEST as $key => $value){
  $_SESSION[$key] = $value;
  echo "{$_SESSION[$key]}<br>\n";
}
?>[/code]

Regards
Huggie
Link to comment
Share on other sites

*slaps self for session/server mix up* Thanks.

Well, I already knew that they were being stored correctly b/c when I change the form action to my confirmation page the values display there correctly. But yes, when I added your code to the top of Page 1 all of my values were displayed there at the top.  :)
Link to comment
Share on other sites

I'll give you thorough examples b/c the page is about 300 lines of code.

[b]confirmation page:[/b]

[code]<form name="bc" method="POST" action="bc_process.php">

<?php
$fname=$_POST['fname']; echo "$fname";
$lname=$_POST['lname']; echo "$lname";

$rbBill=$_POST['rbBill'];
$cbSpeed=$_POST['cbSpeed'];
$cbFound=$_POST['cbFound'];
$speed=$_POST['speed'];
$found=$_POST['found'];

if(!empty($_POST['rbBill']))
{
echo "$rbBill";
}

if(!empty($_POST['cbSpeed']))
{
echo "$cbSpeed: $speed";
}

if(!empty($_POST['cbFound']))
{
echo "$cbFound: $found";
}
?>

<input name="submit" id="submit" type="submit" class="style1" value="Submit">
</form>[/code]
Link to comment
Share on other sites

[quote]echo the $_SESSION variables, not the $_POST variables[/quote]This was part of my original question ... I thought I wouldn't need to use sessions until I was ready to carry values from my 2nd to 3rd page.  :P

As you can tell, I am a sessions-beginner. I've done what you recommended on my 2nd page and still have a problem so I assume that I need to add sessions to my 1st page. Here is an example of a form object on Page 1:

[code]<?php
$sticky_speed = (isset($_POST['speed']))? ' value = "' . $_POST['speed'] . '" ':' '; # maintains user value after validation error
echo '<input type="text" ' . $sticky_speed . ' name="speed" id="speed" onClick="clear_found();" />'; # displays textfield
?>[/code]
Link to comment
Share on other sites

I'm assuming you have a logical page progression like this... I go to page 1, I fill in the form, I hit submit, it submits to itself to validate information, assuming that the information is valid it sends me to page 2 with my details displayed, if not, it shows me page 1 again with the erros on it?

Is this correct, if not, can you provide me with the logical flow?

Regards
Huggie
Link to comment
Share on other sites

No it doesn't, so here's an example...

page1.php
[code]<?php
// Start the session
session_start();

// If the form's been submitted
if (isset($_POST['submit'])){
  // Put the values into session variables
  foreach ($_POST as $key => $value){
      if ($key != "submit"){
        $_SESSION[$key] = $value;
      }
  }
  // Include your validate code
  include('validate.php');

  // This is just an example as I don't know how you're validating, but the header() part is the important part
  if ($validated == "yes"){
      header("Location: page2.php");
  }
}

// Echo the form
echo <<<FORM
<form name="register" action="{$_SERVER['PHP_SELF']}" method="post">
<input type="text" name="firstname" value="{$_SESSION['firstname']}">First Name<br>
<input type="text" name="lastname" value="{$_SESSION['lastname']}">Last Name<br>
<input type="text" name="username" value="{$_SESSION['username']}">Username<br>
<input type="submit" name="submit" value="submit">
</form>
FORM;
?>
[/code]

page2.php
[code]
<?php
// Start the session
session_start();

// Echo the code
echo "Welcome {$_SESSION['firstname']}<br><br>\n";
echo "The following details were added to our records:<br>\n";
foreach ($_SESSION as $key => $value){
  echo "$key: $value<br>\n";
}
?>
[/code]

I hope this helps.

Regards
Huggie
Link to comment
Share on other sites

You hoped and it did!  :) I thought you only had me use the foreach construct for testing purposes. This [b]was[/b] atop Page 1:

[code]<?php
session_start();
include 'bc_validate.php';
?>[/code]

and now I have this:

[code]<?php
session_start();

if( isset($_POST['confirm']) )
{
foreach ($_POST as $key => $value)
{
                if ($key != "confirm")
  {
            $_SESSION[$key] = $value;
}
}

include 'bc_validate.php'; # contains my redirect
}
?>[/code]

This works great when I change my code on Page 2 (confirmation) to this:

[code]<?php $fname=$_SESSION['fname']; echo "$fname";?>[/code]

Please explain the purpose of this part as I'm trying to understand the logic of what I have just done: if ($key != "confirm")

Thank you!

Link to comment
Share on other sites

ok, for ease of use and speed in the example, I used this code:

[code=php:0]foreach ($_POST as $key => $value){
  $_SESSION[$key] = $value;
}[/code]

Without the condition that would actually have given me 4 session variables, so when I echoed them all on the display page using a foreach loop:

[code=php:0]echo "The following details were added to our records:<br>\n";
foreach ($_SESSION as $key => $value){
  echo "$key: $value<br>\n";
}[/code]

I'd have got:
[pre]firstname: richard
lastname: jones
username: huggybear
submit: submit[/pre]
So this line [code=php:0]if ($key != "submit")[/code] says, if my $key is not equal (!=) to submit (The name of my button) then carry out the selected action, which in our case was to add the value to a session variable.  So by adding that line in, I didn't actually create a session valiable called submit.

I hope this makes sense.

Regards
Huggie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.