PHP IF statement - getting a varible to show on another page

Thread Solved

Join Date: Aug 2009
Posts: 12
Reputation: Enthused is an unknown quantity at this point 
Solved Threads: 1
Enthused Enthused is offline Offline
Newbie Poster

PHP IF statement - getting a varible to show on another page

 
0
  #1
Sep 2nd, 2009
Hi all,

Ive created a pretty simple user system its all working well users can sign up and so on. Though i wanted to add extra security to this system so when the user wants to modify their password it asks for their old password otherwise they cant sign up for a new password.

Im using two files for this the modify.php file where the user can fill in their password and old password. And the User.php containing the if statement.

I think ive done the IF statement but im not sure because the variable which will show the error message i cant seem to get working so that it displays the message on the other page.

Heres the code which powers the form(sorry for the messy coding its late and i just really want to get this finished!):

  1. function modifyUser()
  2. {
  3. $userId = (int)$_POST['hidUserId'];
  4. $password = md5($_POST['txtPassword']);
  5. $oldpass = md5($_POST['txtOldPass']);
  6. $oldpassdata = mysql_query("SELECT user_password FROM tbl_user WHERE user_id = '$userId'");
  7.  
  8. if ($oldpass==$oldpassdata)
  9. $sql = "UPDATE tbl_user
  10. SET user_password = '$password'
  11. WHERE user_id = '$userId'";
  12. dbQuery($sql);
  13. header('Location: index.php');
  14. else
  15. $errorpass= "Oldpassword does not match!";
  16.  
  17.  
  18. }


The $errorpass varible is what i want to display on the form page, so that the error is displayed on the form.

I have tried using using the echo function to display the varible but its not picking it up from the otherpage
  1. <td class="content"> <input name="txtOldPass" type="password" class="box" id="txtOldPass" size="20" maxlength="20"><?php echo $errorpass; ?></td></tr>

I hope you can help, thanks for your time.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 796
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: PHP IF statement - getting a varible to show on another page

 
0
  #2
Sep 3rd, 2009
There are a few ways you can do this, but the simplest is probably to pass the variable via GET. To do this add the following line of code after you set $errorpas:
  1. header("Location: User.php?errorpas=$errorpas");

Then at the top of your User.php script add the following lines:
  1. if(isset($_GET["errorpas"]))
  2. $errorpas = $_GET["errorpas"];
  3. else
  4. $errorpas = "";
  5.  
  6. // may need to perform some validation of what has been passed before you use it

If you google $_GET for php, you will get a better understanding of how to pass variables between php pages.

EDIT: I have assumed that you are trying to display the error message on the page User.php. If you are trying to display on Modify.php, change where I have written User.php to Modify.php.
Last edited by darkagn; Sep 3rd, 2009 at 12:19 am.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 49
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster

Re: PHP IF statement - getting a varible to show on another page

 
0
  #3
Sep 3rd, 2009
I'm not sure if I completely understand your problem, but I'm gonna put this out there and hope it helps.

If you want to keep your form method as POST, and "Oldpassword does not match!" is the only possible value that $errorpass can have, you can just add a hidden field to your HTML form:

  1. <input type="hidden" name="errorpass" value="Oldpassword does not match!" />
Last edited by EvolutionFallen; Sep 3rd, 2009 at 5:34 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: Enthused is an unknown quantity at this point 
Solved Threads: 1
Enthused Enthused is offline Offline
Newbie Poster

Re: PHP IF statement - getting a varible to show on another page

 
0
  #4
Sep 3rd, 2009
Ok thanks a lot darkagn i did see this method just got confused if it was only used for forms.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC