- This topic is empty.
-
AuthorPosts
-
September 22, 2014 at 9:30 am #184116
smarty.rockz
ParticipantHey,
I am building a website which contains a contact form. I want that when the user submit the form, all the details of the form gets sent it to the client’s email address. I tried searching the net but no sample form/php is working for me.
I also want that when the user press the submit button and the message gets sent, a dialog appears saying “Thank you for contacting, we will get back to you soon” and the form gets reset.Any help regarding this?
September 22, 2014 at 11:53 am #184139smarty.rockz
ParticipantI did this :
Html5:
<form role="form" action="contact.php" method="post">
But whenever I press the “Submit” button, it shows the whole php code that I saved in contact.php is this because it is local and not hosted?
September 22, 2014 at 11:45 pm #184184smarty.rockz
ParticipantOK, I am setting up xampp, and then it would probably work, but what about the dialog I want when the message has been successfully submitted, instead of a new page, I want a dialog to appear saying “Your message has been sent.Thank you for….” and then the contact form fields resets.
September 22, 2014 at 11:57 pm #184185chrisburton
ParticipantXamp and Mamp both have Windows and Mac versions.
“Your message has been sent.Thank you for….” and then the contact form fields resets.
AJAX would be what you’re looking for.
September 23, 2014 at 12:09 am #184186smarty.rockz
ParticipantSo I need to add this in my html?
<script> $(function () { $('form').on('submit', function (e) { e.preventDefault(); $.ajax({ type: 'post', url: 'contact.php', data: $('form').serialize(), success: function () { alert('Your message has been sent. Thank you for contacting.'); } }); }); }); </script>
September 23, 2014 at 2:37 am #184195chrisburton
ParticipantWell, what happens when you use that script? I’m not that great when it comes to Javascript.
September 23, 2014 at 6:51 am #184356Ilan Firsov
ParticipantTechnically, yes. At least if you are using jQuery.
But you will have to interpret the serialized data on the PHP side. I have not idea whatserialize()
function is doing and don’t know how to handle it in PHP, not until I try this on my own.Also I would recomment to return data back from PHP side to make sure to display the correct message. You can’t automatically asume that the message was sent.
If you output something in the PHP file (simpleecho
would be fine) you can read it in the success callback, so let’s assume we don’t get anything returned from the PHP file if the message is sent:success: function (data) { if(data.length > 0) { alert('something went wrong'); //I usually console.log(data) to see what I get from PHP } else { alert('Your message has been sent. Thank you for contacting.'); } }
September 23, 2014 at 6:55 am #184358Ilan Firsov
ParticipantOn the PHP side:
<?php $success = mail('[email protected]', 'this is a test', 'content'); if(!$success) { echo 'email not sent'; } ?>
September 23, 2014 at 12:30 pm #184381smarty.rockz
ParticipantI tried testing the php with xampp, but it is not working, whenever I click the “Submit” button, contact.php opens blank.
Any idea, what’s happening?September 24, 2014 at 3:18 am #184488Ilan Firsov
ParticipantMake sure that error reporting is eanbled in PHP and try to access the PHP file directly. If you have any syntax errors it should show it.
Check your web developer console (in Chrome it’s F12, no idea where it is on other browsers) for JS errors when you click submit. You probably have to enable “preserve log” option so it will not clear the console when you navigate to another page.Also note that WAMP/XAMPP on windows do not support sending emails by default. You have to configure it properly. Try following this tutotrial: http://yogeshchaugule.com/blog/2013/configure-sendmail-wamp (it is for WAMP, but for XAMPP it should be basically the same)
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.