jquery - How do I change the simple form below run to Ajax with PHP? -


I need to change this form whose markup and php code are viewed below to operate without loading the page. So this will be an Ajax PHP form. I am very new to Ajax. Somebody has guided me to guide me in this. I will be very grateful.

Please note that a copy of the data of the form filling the visitor should be emailed to the visitor.

The markup is below IS:

   & Lt; Title & gt; Contact form with email & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div id = "myemailform" & gt; & Lt; Form action = "contact.php" method = "post" name = "myemailform" & gt; Enter name: & lt; Input name = "name" type = "text" / & gt; Enter email address: & lt; Input name = "email" type = "text" / & gt; Enter message: & lt; Textarea name = "message" & gt; & Lt; / Textarea & gt; & Lt; Input type = "submit" value = "send form" / & gt; & Amp; Nbsp; & Lt; / Form & gt; & Lt; / Div & gt; & Lt; Script type = "text / javascript" src = "myform.js" & gt; & Lt; / Script & gt; & Lt; Script src = "// ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> & Lt; / Body & gt; & Lt; / Html & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Meta charset = "UTF-8" & gt; & Lt; Title & gt; Contact form with email & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form method = "post" name = "myemailform" action = "contact.php" & gt; Enter name: & lt; Input type = "text" name = "name" & gt; Enter email address: & lt; Input type = "text" name = "email" & gt; Enter message: & lt; Textarea name = "message" & gt; & Lt; / Textarea & gt; & Lt; Input type = "submit" value = "send form" & gt; & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

is the PHP code (contact.php):

  & lt; Php $ name = $ _POST ['name']; $ Visitor_email = $ _POST ['email']; $ Message = $ _POST ['message']; $ Email_from = 'me@mysite.com'; $ Email_subject = "new form submission"; $ Email_body = "You've received a new message called user $. \ N". "Here's the message: $ message. \ N"; $ AEmails = array (); $ AEmails [] = 'me@mysite.com'; $ AEmail [] = $ visitor_email; $ Headers = "to: $ email_from \ r \ n"; $ Headers = "Reply: $ visitor_email \ r \ n"; Foreign exchange ($ aEmails $ aEmail) {mail ($ aEmail, $ email_subject, $ email_body, $ headers); }? & Gt; ('Submit', function (e) {$ .ajax ({/ type: 'post', url: 'contact.php', data: $ ('form # myemailform'). Serialize (), success: function Res) {warning (res);}}); e.preventDefault ();});  

Thanks in advance.

ID in your form and it will be Ajax

  $ ('form # myemailform'). ('Submit', function e) {$. Ajax ({type: 'post', url: '/ your / php / page / path', data: $ ('form # myemailform'). Serialize (), success: function (res) {alert (res);} }); E.preventDefault ();}); You can access all your  POST  variables in your  PHP  page and you are ready to go ...  

/ P>

PS: This script should have the imported JQuery library before running.


Comments