Jump to content


Photo

$_SERVER['PHP_SELF'] and validation & redirection


  • Please log in to reply
14 replies to this topic

#1 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 19 October 2006 - 03:10 PM

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.



#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 03:17 PM

Should be
$_SERVER['PHP_SELF']
not
$_SESSION['PHP_SELF']
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.

<?php
// Start the session
session_start();

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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 19 October 2006 - 03:34 PM

*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.  :)

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 03:42 PM

So is it all working now then?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 19 October 2006 - 04:05 PM

No, none of the values are passed to Page 2. They only pass if I change the form action to Page 2.

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 04:07 PM

OK, post the code for page2...

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 19 October 2006 - 04:21 PM

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

confirmation page:

<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>


#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 05:31 PM

Your echoing the wrong thing.

Echo the $_SESSION variables, not the $_POST variables and don't forget session_start() at the top of the page.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#9 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 19 October 2006 - 06:53 PM

echo the $_SESSION variables, not the $_POST variables

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:

<?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
?>


#10 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 07:02 PM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#11 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 19 October 2006 - 07:12 PM

Yes, that is exactly right. I don't think it matters that my validation is an include?

#12 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 07:58 PM

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

page1.php
<?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;
?>

page2.php
<?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";
}
?>

I hope this helps.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#13 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 19 October 2006 - 08:15 PM

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

<?php 
session_start();
include 'bc_validate.php';
?>

and now I have this:

<?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
}
?>

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

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

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!



#14 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 09:32 PM

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

foreach ($_POST as $key => $value){
   $_SESSION[$key] = $value;
}

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:

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

I'd have got:
[pre]firstname: richard
lastname: jones
username: huggybear
submit: submit[/pre]
So this line
if ($key != "submit")
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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#15 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 20 October 2006 - 01:15 PM

It does and thanks for your help.  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users