Jump to content

Ajax Contact Form


mrkhan
 Share

Recommended Posts

Hello,

i am trying to built a contact form using Ajax. here is my code 

<form method="post" action="<?=$config->urls->root;?>subscribe/contact.php" id="contactForm1">
<input type="text" id="fullname" name="fullname" class="textbox" placeholder="Your name" required="">
<input required="" type="email" id="email" name="email" class="textbox" placeholder="Your email" >
<input type="submit" value="Send">
</form>
 
here is my Java Script
<script>
$(function() {
    var frm = $('#contactForm1');
   frm.submit(function (ev) {
ev.preventDefault();
var fullname = $('#fullname').val();
var email = $('#email').val();

        $.ajax({
            type: 'post',
      url: frm.attr('action'),
     data: 'fullname=' + fullname+'&email=' + email,

            success: function(results) {
                if(results==1)
              {
            $("#suscess").show(); 
            }

        if(results==2)
      {
      $("#fail").show(); 
      }
            }
        }); // end ajax
    });
});

</script>
and here is my PHP file

<?php
$emailTo = "myemail@domain.com";

// sanitize form values or create empty
    $form = array(
    'fullname' => $sanitizer->text($input->post->fullname),
    'email' => $sanitizer->email($input->post->email),
    ); 

$msg = "Full name: $form[fullname]\n" . 
               "Email: $form[email]" ;

 if(mail($emailTo, "Website contact form submission", "$msg", "From: $form[email]"))
{
echo "1";
}else{
echo "2"
}

?>
 
the error i am getting is on 
 
$sanitizer->text

mail(
 
Undefined variable: email
 
i am thinking may be this PHP file in root/subscribe thats why processwire variables are not loading.
 
also i can't access the PHP from Template folder.
 
how to fix this issue.
 
Thanks
 
 
 
 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...