passing varaible between pages ussing sessions

Thread Solved

Join Date: Feb 2008
Posts: 39
Reputation: mr_scooby is an unknown quantity at this point 
Solved Threads: 0
mr_scooby mr_scooby is offline Offline
Light Poster

passing varaible between pages ussing sessions

 
0
  #1
Oct 13th, 2009
Hello, I have 2 php forms, the first that takes the input and sends it to the second form which checks to see if the fields are empty.

What I have to do is if both fields are not entered is return to the main page and highlight the field/s that is not fill with an asterix, If one of the fields was entered I need to return that value to the textbox on the first page and then link back to the first page to enter in the missing field.

here is the first page input.php
  1. $asterixFirst = "";
  2. $asterixLast = "";
  3. // check for asterix value
  4. if ($checkFirst == false)
  5. {
  6. $asterixFirst = "* required";
  7. }
  8. else
  9. {
  10. $asterixFirst = "";
  11. }
  12. if ($checkLast == false)
  13. {
  14. $asterixLast = "* required";
  15. }
  16. else
  17. {
  18. $asterixLast = "";
  19. }
  20.  
  21.  
  22. echo "<form action=\"inputcheck1.php\" method=\"post\">";
  23. echo "<p>Enter first name: <input type=\"text\" name=\"first\" />", $asterixFirst, "</p>";
  24. echo "<p>Enter last name: <input type=\"text\" name=\"last\"/>", $asterixLast, "</p>";
  25. echo "<input type=\"submit\" value=\"Process\"/>";
  26.  
  27. echo "</form>";
  28. ?>
  29. </body>
  30. </html>

here is the 2nd page

  1. <?php
  2. if ((checkFirst() == false) && (checkLast() == true))
  3. {
  4. echo "First name needed";
  5. }
  6. elseif ((checkFirst() == true) && (checkLast() == false))
  7. {
  8. echo "Last name needed";
  9. }
  10. elseif ((checkFirst() == false) && (checkLast() == false))
  11. {
  12. echo "Both fields need filing";
  13. }
  14. else
  15. {
  16. echo "First Name: ", $_POST['first'];
  17. echo "<p>Last Name: ", $_POST['last'], "</p>";
  18. echo "<a href=\"emptyinput1.php\">return to main page</a>";
  19. }
  20. ?>
  21.  
  22. <?php
  23. function checkFirst()
  24. {
  25. $_POST['first'] = trim($_POST['first']);
  26. if (isset($_POST['first']) && $_POST['first'] !== '')
  27. {
  28. //echo "First Name: ", $_POST['first'];
  29. return true;
  30. }
  31. else
  32. {
  33. return false;
  34. }
  35. }
  36.  
  37. function checkLast()
  38. {
  39. $_POST['last'] = trim($_POST['last']);
  40. if (isset($_POST['last']) && $_POST['last'] !== '')
  41. {
  42. //echo "<p>Last Name: ", $_POST['last'], "</p>";
  43. return true;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. ?>

I have the checks working ok, just not sure how to pass the value of a entered field gathered by form 2 back to that field on the first form and then how to send the asterix check either.

I assuming it needs to be done using the $_SESSION command somehow.

any help much appreciated
Last edited by mr_scooby; Oct 13th, 2009 at 3:48 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 867
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 140
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark
 
1
  #2
Oct 13th, 2009
Insert this at the top of both pages:
  1. if (! session_id())
  2. session_start();

Then you can use
  1. $_SESSION['first'] = $_POST['first'];
in the second file. In the first file you can use:
  1. if (isset($_SESSION['first']))
  2. // first name was set by second page, you can use it
Last edited by pritaeas; Oct 13th, 2009 at 4:39 am.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 39
Reputation: mr_scooby is an unknown quantity at this point 
Solved Threads: 0
mr_scooby mr_scooby is offline Offline
Light Poster
 
0
  #3
Oct 13th, 2009
awesome, did the job exactly thank you.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC