Jump to content

[SOLVED] Displaying input text variable before email is sent


Recommended Posts

Hi guys,

 

I'm not really sure how to title this but here goes...

 

My PHP code:

<?php
   function validate_email($email){
   $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
   if(eregi($exp,$email)){
   if(checkdnsrr(array_pop(explode("@",$email)),"MX")){
   return true;
   }else{
   return false;
   }
   }else{
   return false;
   }
}
    if (isset($_POST['Submit']))
                         {
    if (empty($_POST['name']))
    {
    $errors[] = 'Please enter your name.';
    }
    else if (!ereg("^[a-zA-Z ]+$",$_POST['name']))
    {
    $errors[] = 'Please enter your name with alphabets only.';
    }
    if (empty($_POST['email']))
    {
    $errors[] = 'Please enter your e-mail address';
    }
    else if (!validate_email($_POST['email']))
    {
    $errors[] = 'Please enter a valid e-mail address';
    }
    if (empty($_POST['country']))
    {
    $errors[] = 'Please enter your country name.';
    }
    if (empty($_POST['telephone']))
    {
    $errors[] = 'Please enter your telephone no. Please enter 0 if you do not wish to provide a telephone no.';
    }
    else if (!is_numeric($_POST['telephone']))
    {
    $errors[] = 'Please enter a valid telephone with a numeric value without any dash or brackets';
    }
    if (empty($_POST['message']))
    {
    $errors[] = 'Please enter your message';
    }
    else if (strlen ($_POST['message']) > 255)
    {
    $errors[] = 'Your message is too long, please do not submit more than 255 characters';
    }
    if (empty($_POST['spam']))
    {
    $errors[] = 'Please answer the basic math question';
    }
    else if ($_POST['spam'] != 9)
    {
    $errors[] = 'Please enter the correct answer';
    }
    if (count($errors) == 0)
    {
    function check_input($data)
    {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    $RepName = check_input($_POST["name"]);
    $RepMail = trim($_POST["email"]);
    $RepCom = check_input($_POST["message"]);
    
    require("./mail/class.phpmailer.php");

$mail = new PHPMailer();

//$mail->IsSMTP();
$mail->Host = "xxx";
//$mail->SMTPAuth = true;
//$mail->Username = "";
//$mail->Password = "";

$mail->From = "$RepMail";
$mail->FromName = "$RepName";
$mail->AddAddress("xxx", "xxx");
$mail->AddReplyTo("$RepMail", "$RepName");

$mail->IsHTML(true);

$mail->Subject = "xxx- $RepSubj";
$mail->Body    = "xxx";
$mail->WordWrap = 50;

if(!$mail->Send())
{
   echo "<div class=\"errorbox\">";
   echo "Message could not be sent.<br />";
   echo "Mailer Error: " . $mail->ErrorInfo;
   echo "</div>";
   exit;
}
    echo "<div class=\"noerrorbox\">";
    echo "Thank You ".$RepName." . Your Message has been sent.<br />We will contact you ASAP.";
    echo "</div>";
    }
    else
    {
    echo "<div class=\"errorbox\">";
    echo $errors[0];
    echo "</div>";
    }

}

?>
// Part of the contact from
<form action="" method="post" class="feedbackform">
<fieldset>
<legend>CONTACT INFORMATION</legend>
<div class="fieldwrapper">
<label for="name" class="styled">Your Name:</label>
<div class="thefield">
	<input type="text" id="user" name="name" value="<?php echo @$_POST['name']; ?>" size="30" />
</div>
</div>

Note: There are html codes above the php tags.

 

I have a contact form with validation and use phpMailer to send the email. The form and the error messages kinda works but i would like to know, how do i display the input text value in the text box from the $POST value like name, email and etc before the actual form is emailed.

 

At the moment, the form checks for any error in the name field and if there is no error it proceeds to the email field and the rest of error process goes on. I need to know the method to display the name field in the input text from the $POST value if there is no error. I tried

<input type="text" id="user" name="name" value="<?php echo @$_POST['name']; ?>" size="30" />

but im kinda lost on how to erase the value once the email is sent. This method displays all the value even after the email is sent.

 

Should session be used or is there a better way?

 

Thanks.

When sending the mail, use this:

 

$mail_sent = mail($to, $subject, $message, $headers);

 

If the mail is sent, $mail_sent becomes TRUE, and if the mail wasn't sent, it is FALSE.

 

Then, in your html output, you can use this:

 

<input type="text" id="user" name="name"<?php if(!$mail_sent) {echo ' value="' . $_POST['name'] . '"';} ?> size="30" />

 

The value of $_POST['name'] will only be set if the mail hasn't been sent. If it has, then the field will be left blank.

 

Side note: don't use @ marks in your code. It's sloppy programming. It hides errors, instead of fixing them.

 

No $_POST doesn't care about the email adn you shouldn't be working on it (i.e empty($_POST))

 

what you need to do is do all your processing before displaying the form (Which you do)

 

If the email is successful you do nothing

if it is not you run

<?php
$inputs = array();
foreach($_POST as $key=>$value){
$inputs[$key] = $value;
}
?>

 

And then instead of saying

<input type="text" id="user" name="name" value="<?php echo @$_POST['name']; ?>" size="30" />

say

<input type="text" id="user" name="name" value="<?php echo $inputs['name']; ?>" size="30" />

 

Also don't use that @ sign its an error suppressor you are learning php you want to see errors, errors are helpful.

Thank you very much haku & cooldude832,

 

I kinda used both your advice.

 

this is what i did:

<?php
if(!$mail->Send())
{
   echo "<div class=\"errorbox\">";
   echo "Message could not be sent.<br />";
   echo "Mailer Error: " . $mail->ErrorInfo;
   echo "</div>";
   exit;
}
    echo "<div class=\"noerrorbox\">";
    echo "Thank You ".$RepName." . Your Message has been sent.<br />We will contact you ASAP.";
    echo "</div>";
    }
    else
    {
    echo "<div class=\"errorbox\">";
    echo $errors[0];
    echo "</div>";
    $inputs = array(); // cooldude832 method
    foreach($_POST as $key=>$value){
    $inputs[$key] = $value;
}
    }

}

?>

 

and at the form code:

<input type="text" id="user" name="name" value="<?php if (isset($_POST['Submit'])) if (count($errors) != 0) {echo $inputs['name'];} // haku & cooldude832 method ?>" size="30" />

 

After testing it locally, it works fine for the first 2 error, because the 3rd error checks for checkdnsrr() , i need to upload it to a server.

 

Will post the results soon, because i don't know what the outcome would be.

 

Thanks guys.

;D ;D ;D Works like a charm...

 

cooldude832 :

The check for isset $_POST is trivial because you errors should be = 0 if you have not set any post data.

 

How should i do it then?

 

Now, i got to crack my head, to make it work on the drop down list, checkbox and radio button :)

 

Thanks.  ;)

 

for a drop down you need to build your options into an array and then run an if statement in a foreach and if it matches if statement set selected=selected

 

For checks its the same as input just instead of setting the value you set checked=checked

 

and radio its selected=selected same concept as check

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.