rbragg Posted October 19, 2006 Share Posted October 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/ Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 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 sessionsession_start();// Loop throughforeach ($_REQUEST as $key => $value){ $_SESSION[$key] = $value; echo "{$_SESSION[$key]}<br>\n";}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111340 Share on other sites More sharing options...
rbragg Posted October 19, 2006 Author Share Posted October 19, 2006 *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. :) Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111346 Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 So is it all working now then?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111352 Share on other sites More sharing options...
rbragg Posted October 19, 2006 Author Share Posted October 19, 2006 No, none of the values are passed to Page 2. They only pass if I change the form action to Page 2. Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111361 Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 OK, post the code for page2...RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111365 Share on other sites More sharing options...
rbragg Posted October 19, 2006 Author Share Posted October 19, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111380 Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 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.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111424 Share on other sites More sharing options...
rbragg Posted October 19, 2006 Author Share Posted October 19, 2006 [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. :PAs 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 errorecho '<input type="text" ' . $sticky_speed . ' name="speed" id="speed" onClick="clear_found();" />'; # displays textfield?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111461 Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 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?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111470 Share on other sites More sharing options...
rbragg Posted October 19, 2006 Author Share Posted October 19, 2006 Yes, that is exactly right. I don't think it matters that my validation is an include? Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111476 Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 No it doesn't, so here's an example...page1.php[code]<?php// Start the sessionsession_start();// If the form's been submittedif (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 formecho <<<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 sessionsession_start();// Echo the codeecho "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.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111513 Share on other sites More sharing options...
rbragg Posted October 19, 2006 Author Share Posted October 19, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111526 Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 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: richardlastname: jonesusername: huggybearsubmit: 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.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111558 Share on other sites More sharing options...
rbragg Posted October 20, 2006 Author Share Posted October 20, 2006 It does and thanks for your help. :) Quote Link to comment https://forums.phpfreaks.com/topic/24450-_serverphp_self-and-validation-redirection/#findComment-111813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.