0

Does anyone know about a good tutorial where submiting a form from a sing page is explained? I have a few page views in my html code and one of them is a form with three fields (Name, Email and Message) what I am trying to achieve is to submit the form data via Ajax without using a process.php directly.

This is the Form:

<section class="hidden" id="view-forms">
            <header>
                <button class="left arrow" data-vin="view-home" data-sd="sr">
                    <div class="label">All Contacts</div>
                </button>
                <h1>Message</h1>
                <button class="right bold green" data-vin="view-done" data-sd="sl">
                    <div class="label">Send</div>
                </button>
            </header>
            <div class="scrollMask"></div>
            <div class="scrollWrap">
                <div class="scroll">
                <div class="content">
                    <input placeholder="Name" type="text" />
                    <input placeholder="Email" type="email" />
                    <textarea placeholder="Your Message" rows="5"></textarea>
                </div>
            </div>
            </div>
        </section>

This is the confirmation page after message has been sent:

<section class="hidden" id="view-done">
            <header>
                <h1>That's it!</h1>
                <button class="right bold" data-vin="view-home" data-sd="popout">
                    <div class="label">Done</div>
                </button>
            </header>
            <div class="scrollMask"></div>
            <div class="scrollWrap">
                <div class="scroll">
                <div class="content">
                    <h2>Message sent!</h2>
                </div>
            </div>
            </div>
        </section>

Please, let me know if any of you have already implemented something like this. Thank you.

xzibit
  • 3,197
  • 2
  • 17
  • 19
  • This should help: http://stackoverflow.com/questions/20150130/ajax-and-php-to-enter-multiple-forms-input-to-database/20150474#20150474 – MonkeyZeus Feb 19 '14 at 19:21

1 Answers1

0

You could submit to the same PHP page. You just need an if statement to separate the code that generates the page from the code that submits your form. That being said, you should probably just create a second PHP script to handle the form submission. If you wanted to implement it using the if statement, I would add a piece of information to your GET/POST request which would be something like:

'formSubmission='+true

Okay, for the more details, look at this tutorial, it goes over the basics. In your case, try this (NOTE: I haven't tested any of this). Also, add an ID to each of the elements (I assumed they would be the same as your current name attributes and that the textarea would have the ID message).

function()submitForm(){
    var name = document.getElementById('name').value;
    var email = document.getElementById('email').value;
    var message = document.getElementById('message').value;
    var requestData = 'name='+name+'&email='+email+'&message='+message+'&formSubmission='+true;
    //I'm assuming that you're using a POST request (this depends on the length of the message
    var AJAXObj = new XMLHttpRequest();
    //don't forget to replace currentURL.php with the name of the page that will handle the submission
    AJAXObj.open('POST', 'currentURL.php', true);
    AJAXObj.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    AJAXObj.send(requestData);
    AJAXObj.onreadystatechange = function (){
        var AJAXObj = event.target;
        if(AJAXObj.readyState == 4 && AJAXObj.status == 200){
            var responseText = AJAXObj.responseText;
            //things that you might want to do with your responseText
        }
    }
}

Now here's the PHP:

if(isset($_POST['formSubmission'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    //code that handles the form data that has now been passed to PHP
}

Just a thought, don't submit to the same PHP page that you're currently on. Create a new file and paste the code into that. It'll be cleaner.

  • Thank you for following my question but I am new to using Ajax and I think I´ll need a more detailed example. – xzibit Feb 19 '14 at 22:09
  • Thanks for the code, I will try it and I let you know the outcome in order to mark your answer as accepted. – xzibit Feb 21 '14 at 07:59
  • Hello Superlizardmo... here is a link to the form I am trying to send via AJAX. I have followed your instructions. with the above code and I am not getting anywhere. please take a look at this link http://ios.axikmobile.com, What I am trying to achieve is that whe you click send the info gets sent via ajax to the server...and I also want to add some code for auto response when emails are sent. – xzibit Feb 27 '14 at 08:58
  • Thanks for any additional help you could provide me after seeing the link of the form I am working with. – xzibit Feb 27 '14 at 09:00
  • Hello Superlizardmo, did you see my last posting? – xzibit Feb 27 '14 at 21:54
  • It seemed to work to me, are yu still getting an error message? –  Feb 27 '14 at 22:04
  • Hey Thank you for replying. I don´t get any error message. I would like to see the way you have implemented it.. I want to find out what I am missing... did you check the link I showed you... I want to implement it in that design I am working with... your help is appreciated... Thank you – xzibit Feb 27 '14 at 22:31
  • Hello Superlizardmo... Can you please show me where you have it working properly and I can finally implement it on my end :-) Thank you. – xzibit Feb 28 '14 at 17:59
  • Hello Superliardmo, any help on this? – xzibit Mar 06 '14 at 18:55
  • Are you using an HTML page to submit the form, or are you having your HTML generated by a PHP script? –  Mar 07 '14 at 17:19
  • I am using an html page – xzibit Mar 12 '14 at 15:43
  • Are you submitting the form to a php script? –  Mar 12 '14 at 21:10
  • You need to submit your form to a PHP script. –  Apr 14 '14 at 16:49
  • I have tried this with no success but it's probably me not bein able to get the right code together... do you have an email when I could send you the files so you could take a glimpse at it. I have the example link on this url: http://ios.axikmobile.com – xzibit May 18 '14 at 20:09
  • At last, after trying 100 hundred times I made it work with your guidelineS Superlizardmo. Thank you. – xzibit Jun 02 '14 at 19:41