943,844 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 790
  • PHP RSS
Sep 2nd, 2009
0

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

Expand Post »
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!):

PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Enthused is offline Offline
12 posts
since Aug 2009
Sep 3rd, 2009
0

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

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:
php Syntax (Toggle Plain Text)
  1. header("Location: User.php?errorpas=$errorpas");

Then at the top of your User.php script add the following lines:
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Sep 3rd, 2009
0

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

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:

HTML Syntax (Toggle Plain Text)
  1. <input type="hidden" name="errorpass" value="Oldpassword does not match!" />
Last edited by EvolutionFallen; Sep 3rd, 2009 at 5:34 am.
Reputation Points: 40
Solved Threads: 31
Junior Poster
EvolutionFallen is offline Offline
198 posts
since Aug 2009
Sep 3rd, 2009
0

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

Ok thanks a lot darkagn i did see this method just got confused if it was only used for forms.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Enthused is offline Offline
12 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Where does PHP work ?
Next Thread in PHP Forum Timeline: Blank php pages that have mysql connections





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC