Christopher Bergin Posted January 7, 2013 Share Posted January 7, 2013 I went through the exercise for creating a simple form which, in theory, would be handled using an Ajax script that submits the form to a PHP script for mailing, and that in turn would confirm the success of the submission and reply back to the Ajax script. The mail process on the PHP side was omitted for the exercise but I decided to try to get it to work. I'm having a difficult time establishing values in the PHP script. I'll try and provide just the pertinent code without losing the focus of my question; // Add an event handler to the form's submission: document.getElementById('theForm').onsubmit = function() { // Create the data: var fields = ['name', 'email', 'comments']; var data = []; // Empty array. for (var i = 0, count = fields.length; i < count; i++) { data.push(encodeURIComponent(fields) + '=' + encodeURIComponent(document.getElementById(fields).value)); } // Open the request: ajax.open('POST', 'resources/contact.php', true); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // Send the request: ajax.send(data.join('&')); return false; }; // End of onsubmit function. as you can see, the values are appended to a string as name=value pairs and sent to the PHP script. I'm trying to isolate these values on the PHP side and include them in a mail. Do I have to parse the string and obtain the values that way or does PHP perform that automatically? <?php $body= $_POST['Comments']; echo 'the value of body is...' . $body; $email= $_POST['email']; mail($email, 'Contact Form Submission', $body); echo 'The message has been sent.'; ?> Link to comment Share on other sites More sharing options...
HartleySan Posted January 7, 2013 Share Posted January 7, 2013 What exactly is the problem? Is the email not being sent? Are you successfully getting the values on the PHP side? Is the message being correctly echoed back to the JS side? 1 Link to comment Share on other sites More sharing options...
Christopher Bergin Posted January 8, 2013 Author Share Posted January 8, 2013 The mail does not get sent, I'm not receiving values for my PHP variables but the return message IS echoed back. For instance, I tried to see if the $body variable was receiving a value: echo 'the value of body is...' . $body; but all I receive is the value of body is... so the PHP variables are not receiving values. When I remove the JS function and submit the form directly from the HTML page, the mail gets sent to it's destination. Link to comment Share on other sites More sharing options...
HartleySan Posted January 8, 2013 Share Posted January 8, 2013 Ah, I caught the error. Change $_POST['Comments'] to $_POST['comments']. That should do it. Link to comment Share on other sites More sharing options...
Christopher Bergin Posted January 8, 2013 Author Share Posted January 8, 2013 actually, I had caught that as well. Since you brought it up, the code from the book contains the ID discrepancies in the contact.html page and the contact.js page. It took me an hour to find it. Just an FYI. Anyway, I'm still working on it. I'll get back to you if I find a solution. Link to comment Share on other sites More sharing options...
benjamin.morgan Posted January 8, 2013 Share Posted January 8, 2013 It's also in the errata. "The name and id values of the comments input need to be lowercase." Link to comment Share on other sites More sharing options...
Christopher Bergin Posted January 11, 2013 Author Share Posted January 11, 2013 I had a syntax error that evaded the parser... ajax.setRequestHeader('Content-Type', 'application/x.www-form-urlencoded'); Link to comment Share on other sites More sharing options...
Recommended Posts