Jump to content

Question about sticky forms and same page script processing!


Recommended Posts

    Ok so I have a script where I want to show a header and then below that I start my code that will only execute if the submit button is hit. The functionality from here I want is that if there are any errors I want to output them to the screen and then redisplay the fields to type in a user name and password. The purpose of this form is to signup for a new user id and password.

    I want the user name to redisplay, this is simple enough with a line or so of code and the value parameter added to the input field. However I want it to not show the fields if the username setup is successful and instead show a message stating the success and the new username. Also I want to do all of the processing on the same page (of course using PHP_SELF).

    How do I get it to show the fields on the first display, show them again if any errors are found (like non-matching password vs. password confirms, or a field not filled out), and then show only a success message on the screen?

    My only idea is to echo the form when the submit button has not been pressed and when there are errors, instead of leaving the form outside the code. Is this the only way or is there an easier way?

Thanks,
Patrick
I have it in two seperate files.
contact.php
[code]
<?php
$pgTitle = "Contact Us";
include("header.inc");

//phpinfo();
?>

<?php include("contact.inc"); ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
  <tr>
<td align="right"><b>Name*:</b></td><td><input type="text" name="first_name" size="50" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name'];?>" ></td>
  </tr>
  <tr>
<td align="right"><b>Email Address*:</b> </td><td><input type="text" name="email" size="50" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" > </td>
  </tr>
  <tr>
    <td align="right"><b>Subject*:<b></td><td><input type="text" name="subject" MAXLENGTH="50" SIZE="50" value="<?php if (isset($_POST['subject'])) echo $_POST['subject']; ?>"></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>Comments:</b></td><td><TEXTAREA name="msgBody" VALUE="<?php if (isset($_POST['msgBody'])) echo $_POST['msgBody']; ?>" ROWS=10 COLS=40></TEXTAREA></td>
  </tr>
  <tr>
<td colspan="2"><div align="center"><input type="submit" name="submit" value="Submit">&nbsp;&nbsp;<input type="submit" name="reset" value="Start Over" /></div></td>
  </tr>
</table>


</form><!-- End of Form -->

<?php
include("footer.inc");
[/code]

contact.inc
[code]
<?php
function escape_data($data){
global $dbh;
if (ini_get('magic_quotes_gpc')){
$data = stripslashes($data);
}
return $data;
}

if (isset($_POST['submit'])) { // Handle the form.

$validForm = true;



// Check for a first name.
if (eregi ("^[[:alpha:].' -]{2,50}$", stripslashes(trim($_POST['first_name'])))) {
$fn = escape_data($_POST['first_name']);
} else {
$fn = FALSE;
$validForm = false;
echo '<p><font color="red" size="+1">Please enter your first name!</font></p>';
}


$email = true;
// Check for an email address.
if(!empty($_POST['email'])){
if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", stripslashes(trim($_POST['email'])))){
$e = escape_data($_POST['email']);
} else {
$validForm = false;
echo '<p><font color="red" size="+1">Please enter a valid email address!</font></p>';
}
}else{
$email = false;
}


// Check for a subject.
if (!empty($_POST['subject'])) {
$u = escape_data($_POST['subject']);
} else {
$u = FALSE;
$validForm = false;
echo '<p><font color="red" size="+1">Please enter a valid subject!</font></p>';
}


if ($validForm) { // If everything's OK.

$headers = "From:  " . $fn . " <" . $e . ">\r\n";
mail($webmaster_email, $u, $_POST['msgBody'], $headers);
echo '<br>Thank you for taking the time to contact me. I will respond as soon as possible.<br><br><br><br><br>';
include ('footer.inc'); // Include the HTML footer.
exit();

} else { // If one of the data tests failed.
echo '<p><font color="red" size="+1">Please try again.</font></p>';
}
} // End of the main Submit conditional.
[/code]
The problem with that is how do you handle the situation that they need to try again? Even if the second page has the form in it too the user could and eventually probably will mess up on the entry again. I need a way to be able to infinitely show the page again if they messed up.
Another question I want to know is if a page processes another page, only assigning variables on the submit action from the prior screen, and then this page has to redisplay because of processing itself, will it loose all of the variables and their data that were generated by the post of the prior screen, or will those variables remain intact.

Example:
[code]
if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $query = "INSERT //whatever";
    $query = "SELECT //what was just inserted";

    $id = //the row id num derived from the last query;
}

if(isset($_POST['dothenextthing']))
{
    //Do whatever, data comes from form on this page
}
[/code]

Will the $id and $name variables still exist once it gets done processing "donextthing" from the form on this page?

Thanks,
Patrick
[quote author=pthurmond link=topic=114879.msg467565#msg467565 date=1163464493]
The problem with that is how do you handle the situation that they need to try again? Even if the second page has the form in it too the user could and eventually probably will mess up on the entry again. I need a way to be able to infinitely show the page again if they messed up.
[/quote]

This does EXACTLY that.  Try it for yourself.

EDIT:  URL REMOVED.
to boil it down a little:

[code]
<?php
include('header');
if (isset($_POST['Submit'])){  //  <--if this is true, then the form was submitted
  $validForm = TRUE;//                <--later we'll see if this is still true
  validate the form--if something is wrong in any field, then
  tell visitor there was a problem and what field it's in and
  change $validForm to FALSE
 
  if($validForm){
    Perform the form's tasks (in this case, send the message)
    Thank the visitor
    include('footer');
    exit();  //quit further processing
  }else{
    Tell visitor to try again.
  }
}

this runs if $_POST['Submit'] is not set OR if $validForm was FALSE;
HTML code of the form
include('footer');
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.