karlelizabeth
karlelizabeth

Reputation: 167

Can I reset a form with PHP?

is possible to use php to reset a form without refresh? how can i archive this?

or is possible to do it with js?

most important is do it without to refresh the page and dont add another button for this(that's because the design)

any idea? thanks am working with recaptcha on my form and once i submitted the information i want to send a message this is my js code

function validateCaptcha(){
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    nameField = $("input#name").val();
    emailField = $("input#email").val();
    phoneField =$("input#phone").val();
    reasonField =$("select#reason").val();
    messageField =$("textarea#message").val();
    var html = $.ajax({
    type: "POST",
    url: "ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField +"&name="+ nameField +"&email=" + emailField +"&phone="+ phoneField + "&reason=" + reasonField +"&message=" + messageField,
    async: false
    }).responseText;

    if(html == "success")
    {
        $("#captchaStatus").html("Success. Submitting form.");
        $("#thanks").html("Thank you, we going to keep in touch with you soon.  ");
        return false;

    }


    else
    {
        $("#captchaStatus").html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
}

this is my php code

    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $reason = $_POST['reason'];
    $message = $_POST['message'] ;

    if (empty($name)|| empty($email) || empty($message))
    {

    }
    else
    {
        $resp = recaptcha_check_answer (PRIVATEKEY, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
        if ($resp->is_valid) {

            $header = 'From: ' . $email . " \r\n";
            $msg = "Sent from: " . $name . "\r\n";
            $msg .= "Email: " . $email . " \r\n";
            $msg .= "Phone: " . $phone . " \r\n";
            $msg .= "Contact reason:" . $reason . " \r\n";
            $msg .= "Message: " . $message . " \r\n";

            $to = '[email protected]';
            $subject = 'Emailmakers contact page';

            mail($to, $subject, utf8_decode($msg), $header);

             ?>success<?


        }
        else 
        {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
           "(reCAPTCHA said: " . $resp->error . ")");

}
}

Upvotes: 0

Views: 308

Answers (3)

Qchmqs
Qchmqs

Reputation: 1805

Using PHP mean refreshing the page (only in case of ajax) so i suggest you try to use javascript instead of php to this task

Upvotes: 0

lovesh
lovesh

Reputation: 5411

Use the reset() function. If you have a form named frm then use can use frm.reset(). Or just write code to first get all textboxes and set its value attribute to "". this wont need to refresh your page

Upvotes: 0

Shaokan
Shaokan

Reputation: 7684

Well, once you sed the ajax request if the captcha fails then just reload the entire form.

Say you have only the form in myform.php and you include it in your main page, when the captcha does not match you can just reload the form. Like this:

if (html != "Success")
{
     $("#divThatHoldsTheForm").html(html)
}

and in your myform.php you have only:

<form action="..." method="POST">
  <!-- some inputs -->
</form>

Upvotes: 1

Related Questions