Page 1 of 1

How to stop Form resetting?

Posted: Sat Mar 07, 2009 5:14 am
by djlest
HI every time a wrong captcha code or blank field is left the form resets.

How does one go about solving that please?

If you dont mind explaining in laymans cos im not a coder, just a measley doctor. :banghead:

thanks guys

Code: Select all

<?php
 
session_start();
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["full_name"]) || empty($_POST["sender_email"])
    || empty($_POST["age"]) || empty($_POST["country"]) || $_POST['infotrue']=="no")
{
    //redirect back to form
    header("Location: ./form.php?error=not_enough_info"); //This should really be an absolute URL if you know it
    exit();
}
 
if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
//Copy into global variables
$name = $_POST["full_name"];
$age=$_POST['age'];
$dob=$_POST['dob'];
$nationality=$_POST['nationality'];
$country=$_POST['country'];
$phone=$_POST['phone'];
$height=$_POST['height'];
$weight=$_POST['weight'];
$address=$_POST['address'];
$sex=$_POST['sex'];
$marital_status=$_POST['marital_status'];
$religion=$_POST['religion'];
$ethnic_origin=$_POST['ethnic_origin'];
$contact_person_name=$_POST['contact_person_name'];
$contact_person_phone=$_POST['contact_person_phone'];
$contact_person_address=$_POST['contact_person_address'];
$contact_relationship=$_POST['contact_relationship'];
$surgery_required=$_POST['surgery_required'];
$surgery_results_expected=$_POST['surgery_results_expected'];
$surgeon_questions=$_POST['surgeon_questions'];
$recieved_surgery=$_POST['recieved_surgery'];
$previous_surgery_details=$_POST['previous_surgery_details'];
$cosmetic_surgery=$_POST['cosmetic_surgery'];
$whereandwhen=$_POST['whereandwhen'];
$medications=$_POST['medications'];
$dailydosage=$_POST['dailydosage'];
$implants=$_POST['implants'];
$implantdetails=$_POST['implantdetails'];
$allergies=$_POST['allergies'];
$allergydetails=$_POST['allergydetails'];
$alcohol=$_POST['alcohol'];
$alcoholhowmuch=$_POST['alcoholhowmuch'];
$cigar=$_POST['cigar'];
$cigarhowmuch=$_POST['cigarhowmuch'];
$diabetes=$_POST['diabetes'];
$heart=$_POST['heart'];
$bloodpressure=$_POST['bloodpressure'];
$lungs=$_POST['lungs'];
$kidneyliver=$_POST['kidneyliver'];
$blooddisorders=$_POST['blooddisorders'];
$cancer=$_POST['cancer'];
$aids=$_POST['aids'];
$anesthesia=$_POST['anesthesia'];
$medicalconditions=$_POST['medicalconditions'];
$birth=$_POST['birth'];
$hormone=$_POST['hormone'];
$pregnant=$_POST['pregnant'];
$lastdeliver=$_POST['lastdeliver'];
$lastfeed=$_POST['lastfeed'];
$brasize=$_POST['brasize'];
$waist=$_POST['waist'];
$chest=$_POST['chest'];
$desire=$_POST['desire'];
$arrivaldate=$_POST['arrivaldate'];
$departuredate=$_POST['departuredate'];
$daysinpattaya=$_POST['daysinpattaya'];
$thailand=$_POST['thailand'];
$infotrue=$_POST['infotrue'];
$email = $_POST["sender_email"];
 
 
$body = "<html>
            <head>
                <title>Confirmation form</title>
            </head>
        <body>
        <strong>Personal Details</strong>
        <ul>
            <li>Name : $name</li>
            <li>Age : $age</li>
            <li>DOB : $dob</li>
            <li>Nationality : $nationality</li>
            <li>Country : $country</li>
            <li>Phone : $phone </li>
            <li>Height : $height</li>
            <li>Weight : $weight</li>
            <li>Address : $address</li>
            <li>Sex : $sex</li>
            <li>Marital Status : $marital_status</li>
            <li>Religion : $religion</li>
            <li>Ethnic Origin : $ethnic_origin</li>
        </ul>
        <strong>Next of Kin or Contact Person</strong>
        <ul>    
            <li>Contact Person Name : $contact_person_name</li>
            <li>Contact Person Phone : $contact_person_phone</li>
            <li>Contact Person Address : $contact_person_address</li>
            <li>Contact Person Relationship : $contact_person_relationship</li>
        </ul>
        <strong>Surgery Details</strong>
        <ul>
            <li>Surgery Required : $surgery_required</li>
            <li>Surgery Results Expected : $surgery_results_expected</li>
            <li>Questions for Surgeon : $surgeon_questions</li>
        </ul>
        <strong>Medical History</strong>
        <ul>
            <li>Have you recieved medical care or surgery in 12 months : $recived_surgery</li>
            <li>Details : $previous_surgery_details</li>
            <li>Have you ever had surgery before, cosmetic or medical? $cosmetic_surgery</li>
            <li>Details : $whereandwhen</li>
            <li>Are you taking medications now? $medications</li>
            <li>If so provide names, Daily dosage : $dailydosage</li>
            <li>Do you have any implants or metal objects in your body? $implants</li>
            <li>Details : $implantdetails</li>
        </ul>
        <strong>Medical Conditions</strong>
            <li>Alcohol : $alcohol</li>
            <li>How much : $alcoholhowmuch</li>
            <li>Cigarettes : $cigar</li>
            <li>How much : $cigarhowmuch</li>
            <li>Diabetes : $diabetes</li>
            <li>Heart Problems : $heart</li>
            <li>Blood Pressure : $bloodpressure</li>
            <li>Lung problems : $lungs</li>
            <li>Kidney or Liver Problems : $kidneyliver</li>
            <li>Blood Disorders : $blooddisorders</li>
            <li>Previous History or Cancer : $cancer</li>
            <li>HIV or AIDS : $aids</li>
            <li>Anesthesia Problems : $anesthesia</li>
            <li>If any other conditions : $medicalconditions</li>
        </ul>
        <strong>For Women if Applicable</strong>
        <ul>
            <li>Birth control Pills : $birth</li>   
            <li>Hormone Pills : $hormone</li>
            <li>Pregnant? $pregnant</li>
            <li>Last Delivery : $lastdelivery</li>
            <li>Last Breast Feed : $lastfeed</li>
            <li>Bra Size : $brasize</li>
            <li>Waist Size : $waist</li>
            <li>Chest Circumference : $chest</li>
            <li>Desired Bra Size : $desire</li>
        </ul>
        <strong>Travel Details</strong>
        <ul>
            <li>Planned date of arrival : $arrivaldate</li>
            <li>Planned date of departure : $departuredate</li>
            <li>Total days in Thailand : $daysinpattaya</li>
            <li>Been to Thailand before? $thailand</li>
        </ul>
        <strong>Disclaimer</strong>
        <ul>
            <li>Agree? $infotrue</li>
        </ul>
            
            
            
        
        </body></html>
        ";
 
 
//Validate the email address using a regex (I suggest you use a better one than this!!)
if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
{
    header("Location: ./form.php?error=invalid_email");
    exit();
}
 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
$ftype=false;
 
$allowed = array('image/gif','image/jpeg','image/pjpeg','image/png','x-png'); 
 
foreach ($allowed as $type){
if($type==$_FILES['attachment']['type']){
$ftype=true;
break;  
}
}
 
foreach ($allowed as $type){
if($type==$_FILES['Attachment1']['type']){
$ftype1=true;
break;  
}
}
 
foreach ($allowed as $type){
if($type==$_FILES['Attachment2']['type']){
$ftype2=true;
break;  
}
}
 
foreach ($allowed as $type){
if($type==$_FILES['Attachment3']['type']){
$ftype3=true;
break;  
}
}
 
foreach ($allowed as $type){
if($type==$_FILES['Attachment4']['type']){
$ftype4=true;
break;  
}
}
 
 
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if($ftype){
    if ($_FILES["attachment"]["error"] || $_FILES['attachment']['size']>2097152)
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
}
 
 
if (!empty($_FILES["Attachment1"]["tmp_name"]))
{
    if($ftype1){
    if ($_FILES["Attachment1"]["error"] || $_FILES['Attachment1']['size']>2097152)
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path1 = $_FILES["Attachment1"]["tmp_name"];
    $file_name1 = $_FILES["Attachment1"]["name"];
    $file_type1 = $_FILES["Attachment1"]["type"];
}
}
 
 
if (!empty($_FILES["Attachment2"]["tmp_name"]))
{
    if($ftype2){
    if ($_FILES["Attachment2"]["error"] || $_FILES['Attachment2']['size']>2097152)
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path2 = $_FILES["Attachment2"]["tmp_name"];
    $file_name2 = $_FILES["Attachment2"]["name"];
    $file_type2 = $_FILES["Attachment2"]["type"];
}
}
 
 
if (!empty($_FILES["Attachment3"]["tmp_name"]))
{
    if($ftype3){
    if ($_FILES["Attachment3"]["error"] || $_FILES['Attachment3']['size']>2097152)
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path3 = $_FILES["Attachment3"]["tmp_name"];
    $file_name3 = $_FILES["Attachment3"]["name"];
    $file_type3 = $_FILES["Attachment3"]["type"];
}
}
 
 
if (!empty($_FILES["Attachment4"]["tmp_name"]))
{
    if($ftype4){
    if ($_FILES["Attachment4"]["error"] || $_FILES['Attachment4']['size']>2097152)
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path4 = $_FILES["Attachment4"]["tmp_name"];
    $file_name4 = $_FILES["Attachment4"]["name"];
    $file_type4 = $_FILES["Attachment4"]["type"];
}
}
 
//Everything looks ok, we can start Swift
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/Sendmail.php";
 
 
//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}
 
//Try to connect using /usr/sbin/sendmail -bs
$swift =& new Swift(new Swift_Connection_Sendmail());
 
 
//Let the connection try to work out the path itself (PHP5)
$swift = new Swift(new Swift_Connection_Sendmail(Swift_Connection_Sendmail::AUTO_DETECT));
 
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
//Create the message to send
$message =& new Swift_Message("SURGEON EVALUATION FORM","You have recieved a Medical Evaluation. The details are below!","text/html");
 
$message->attach(new Swift_Message_Part($body,"text/html"));
 
 
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
if ($file_path1 && $file_name1 && $file_type1)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path1), $file_name1, $file_type1));
}
if ($file_path2 && $file_name2 && $file_type2)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path2), $file_name2, $file_type2));
}
if ($file_path3 && $file_name3 && $file_type3)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path3), $file_name3, $file_type3));
}
if ($file_path4 && $file_name4 && $file_type4)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path4), $file_name4, $file_type4));
}
 
 
 
 
//Try sending the email
$sent = $swift->send($message, "info@**************.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{   
    unset($_SESSION['security_code']);
    header("Location: ./thankyouforcontacting.php");
    exit();
}
else
{
    unset($_SESSION['security_code']);
    header("Location: ./form.php?error=sending_failed");
    exit();
}
}else{
    //redirect back to form
    header("Location: ./form.php?error=captcha_wrong"); //This should really be an absolute URL if you know it
    exit();
}
 
 
?>

Re: How to stop Form resetting?

Posted: Sun Mar 08, 2009 8:17 pm
by Benjamin
Yes, you repolulate the values of each field with the original data received by the server when you redisplay the form.

Re: How to stop Form resetting?

Posted: Sun Mar 08, 2009 11:48 pm
by php_east
if it is blank you can just add a javascipt to block sending of the form.
only if it is not, then you would have to repopulate as said, if captcha is wrong.
that would save a lot of time and irritation.