Page 1 of 1

post form vars to php without redirect?

Posted: Sun May 09, 2010 9:51 am
by iansane
Hi,

I am making a "password reset" page and want the submitted data to run through php but don't want the page to redirect to the php page.
Here's the code so far. My script will check that the old password exists in the database and the two new ones match. Then it will replace the one in the database with the new one (md5 encrypted). I have all the code worked out as far as the actual processing but don't know any other way to get the form vars to the php without post. I don't want the browser to redirect though. I just want it to display the success or error message under the "change password" and "cancel" buttons.

I can't have the changeProfile.php redirect back to this page because it's a redirect loop.

Code: Select all

<?php
include '../includes/changeProfile.php';
session_start();
if ($_SESSION['userName']!= 'valid' or $_SESSION['password'] != 'valid'){
//redirect
header("location: ../login.html");
}

$user = $HTTP_SESSION_VARS ["name"];



?>





<html>

<head>
<link rel="stylesheet" type="text/css" href="profile.css" media="screen" />
<title>User Profile</title></head>


<body>
<!--Wrap Start-->
<div id="wrap">


<div class="header"></div>

<!--Content Start-->
<div id="content">

<form action="../includes/changeProfile.php" method="POST">

User Name:<input type="text" name="un" size="25" maxlength="25" value="<?php echo $user;?>"></input>
<br />
<br />
Enter Current Password:<input type="password" name="pw" size="25" maxlength="25"></input>
<br />
Enter New Password:<input type="password" name="npw" size="25" maxlength="25"></input>
<br />
Confirm New Password:<input type="password" name="cpw" size="25" maxlength="25"></input>
<br />
<input type="submit" name="submit" value="Change Password"></input>
<input type="reset" name="reset" value="Cancel"></input>

</form>

<?php
if($changeSuccess == 'true'){
echo '<h3>Password Succesfully Changed!!</h3>';
}
else {echo 'There was a problem and your password did not change. Please try again';}
?>
</div>
<!--Content End-->


</div>
<!--Wrap End-->



</body>

</html>


Re: post form vars to php without redirect?

Posted: Sun May 09, 2010 10:30 am
by iansane
I'm about to try making a function to do all the work in the page its self and take out the "changeProfile.php" all together.

Can I use the "onClick" attribute to call the function and then in the function get the variable like this?

Code: Select all

function changepw(){
$oldPass = 'pw';
$newPass = 'npw';
$confirmPass = 'cpw';

//processing code here?
}
Will that get the form data from the <input>'s (pw, npw and cpw)?

And what would be the syntax for onClick? Would it just be onclick="changepw()"?

Re: post form vars to php without redirect?

Posted: Thu May 13, 2010 10:12 pm
by iansane
OK Im getting confused as to what is html and what is java. Didn't try the onclick thing because a friend told me to just include another php page that puts all the html in variables and then echo it out on the current page. The problem is that it still acts as if I had only one page since all I'm doing is echoing.

So to rephrase the question,

I don't want my form page to change or redirect. I want it to stay on the form page but display a success message. But I have to post the form data to php somewhere to be able to process it.

Here's what I have so far and it is still redirecting of course because even in the variables being echoed there is a 'action="profileInc.php"'. Can anyone tell me the correct way to do this? Am I doing it backwards?

Form page:

Code: Select all

<?php
include '../includes/profileInc.php';
session_start();
if ($_SESSION['userName']!= 'valid' or $_SESSION['password'] != 'valid'){
//redirect if no session
header("location: ../login.html");
}
?>


<html>

<head>
<link rel="stylesheet" type="text/css" href="profile.css" media="screen" />
<title>User Profile</title></head>


<body>
<!--Wrap Start-->
<div id="wrap">


<div class="header"></div>

<!--Content Start-->
<div id="content">
<?php
echo $chPassword1;
echo $chPassword2;
echo $chPassword3;
?>
<br />
<?php echo $passwordMessage;?>
<br />
<?php
echo $chEmail;
?>
<br />
<?php echo $emailMessage;?>
</div


</div>
<!--Wrap End-->



</body>

</html>
Includes page:

Code: Select all

<?php

session_start();

$userName = $HTTP_SESSION_VARS['name'];

$chPassword1 = '<form action="../includes/profileInc.php" method="POST">
<h3>Change Password</h3>
<br />';

$chPassword2 = 'User Name:<input type="text" name="un" size="25" maxlength="25" value='.'"'.$userName.'"'.'READONLY></input>';



$chPassword3 = '
<br />
<br />
Enter Current Password:<input type="password" name="pw" size="25" maxlength="25"></input>
<br />
Enter New Password:<input type="password" name="npw" size="25" maxlength="25"></input>
<br />
Confirm New Password:<input type="password" name="cpw" size="25" maxlength="25"></input>
<br />
<input type="submit" name="password" value="Change Password"></input>
<input type="reset" name="passwordReset" value="Cancel"></input>
</form>
';


$chEmail = '<form action="../includes/changeProfile.php" method="POST">
<h3>Change Email</h3>
<br />
Email: <input type="text" size="25" maxlength="25" name="currentEmail" value="ian@metrotechpc.com" READONLY></input>
<br />
New Email: <input type="text" size="25" maxlength="25" name="ne"></input>
<br />
Confirm Email: <input type="text" size="25" maxlength="25" name="cne"></input>
<br />
<input type="submit" name="email" value="Change Email"></input>
<input type="reset" name="emailReset" value="Cancel"></input>
</form>
';



$currentPassword = $_POST['pw'];
$newPassword = $_POST['npw'];
$confirmedPassword = $_POST['cpw'];

$newEmail = $_POST['ne'];
$confirmedEmail = $_POST['cne'];

if($newEmail != $confirmedEmail){
$emailMessage = "The emails don't match";
}
if($newEmail != "" & $confirmedEmail !="" & $newEmail == $confirmedEmail){
$emailMessage = "Succsess!";
}

if($newPassword != $confirmedPassword){
$passwordMessage = "The passwords don't match";
}
if($newPassword != "" & $confirmedPassword != "" & $newPassword == $confirmedPassword){
$passwordMessage = "Success!";
}
Thanks

Re: post form vars to php without redirect?

Posted: Thu May 13, 2010 10:25 pm
by iansane
don't know why but I hadn't tried posting back to the form itself.

form action="../app/profile.php" solved the problem.

Only now every time I refresh the page firefox gives me the pop up saying it needs to resend information. IE6 doesn't give this popup. It just refreshes with no problem.