ajax - PHP: print text without having to go to the top of the page -


i have function sends mails. here is. can see @ end if checks corrects echoes "email sent!" , there errors if user doesn't compile inputs.

if(isset($_post['submit'])) {      $name = $_post['name'];     $visitor_email = $_post['email'];     ... blahblah     // validation     if(empty($name)||empty($visitor_email))     {         $errors .= "\n field mandatory. ";     }     if(isinjected($visitor_email))     {         $errors .= "\n email format wrong!";     }       if(empty($errors))     {         // send email         $to = $your_email;         $subject="my subject";         ... blah blah          $headers = "from: $from \r\n";          mail($to, $subject, $body,$headers);          echo 'email sent';     } } 

email sending works fine. problem form inside jquery toggled div. , when message needs displayed (wheter error message field mandatory or email has been sent), user gets redirected top of page , toggled div (the 1 in form in) goes visibility:hidden. how can print messages without having user go top of page?

it seems need use jquery.ajax() don't know how..

we need see jquery code. however, being said, if submitting form same page displays , submission not handled asynchronously, page reloading, cause appear "jump" top. write ajax method handle form submission via post. can use event.preventdefault(); prevent page "jumping" when form submitted.

update based on op's reply:

  1. temporarily remove code shows , hides contact form.

  2. implement jquery validation plugin: http://bassistance.de/jquery-plugins/jquery-plugin-validation form handler:
    a. bind call plugin click event on submit button.
    b. use event.preventdefault(); stop form submitting.
    c. on success, validation plugin call ajax method post form.

  3. write ajax method post form. method should have 2 callbacks:
    .done() -- executes if form posts successfully
    .fail() -- executes if there error.

    in done method can hide form. fail method can display error.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -