Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Contact Me PHP Help

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #184116
    smarty.rockz
    Participant

    Hey,

    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?

    #184139
    smarty.rockz
    Participant

    I 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?

    #184184
    smarty.rockz
    Participant

    OK, 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.

    #184185
    chrisburton
    Participant

    Xamp 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.

    #184186
    smarty.rockz
    Participant

    So 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>
    #184195
    chrisburton
    Participant

    Well, what happens when you use that script? I’m not that great when it comes to Javascript.

    #184356
    Ilan Firsov
    Participant

    Technically, 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 what serialize() 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 (simple echo 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.');
        }
    }
    
    
    #184358
    Ilan Firsov
    Participant

    On the PHP side:

    
    <?php
        $success = mail('[email protected]', 'this is a test', 'content');
        if(!$success) {
            echo 'email not sent';
        }
    ?>
    
    
    #184381
    smarty.rockz
    Participant

    I 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?

    #184488
    Ilan Firsov
    Participant

    Make 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)

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘Back End’ is closed to new topics and replies.