Chrisj Posted April 20, 2020 Share Posted April 20, 2020 I have this php file that processes Form field entries. Apparently I need to modify it, I'm told, to "validate the input and then send some response back": <?php if($_POST){ $to = 'chrisj...@.....com'; $subject = 'Thank you for your info'; $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $message1 = $_POST['message']; $headers = $name; $headers = 'from: info@.....com'; $message1 .= "\r\n\r\nName: ".$_POST['name']." \r\n Email: ".$_POST['email']." "; $message = "Hello {$_POST['name']}, ~ Thank you for your input\r\n\r\n"; mail( $to, $subject, $message1, $headers ); mail( $email, $subject, $message, $headers ); header('Location: https://....'); exit; } ?> The corresponding js looks like this: $.ajax({ url: 'https://...../submit.php', method: 'post', data: { name: $('#name').val(), email: $('#email').val() }, success: function (response) { console.log(response); if (response.success) { alert('Success'); } else { alert('Error'); } } }); I look forward to any guidance with adding "validate the input and then send some response back". Quote Link to comment Share on other sites More sharing options...
requinix Posted April 21, 2020 Share Posted April 21, 2020 That's not all you have to change... What input is the Javascript expected to send and what response is it looking for? To make sure we both understand the requirements. Quote Link to comment Share on other sites More sharing options...
Strider64 Posted April 21, 2020 Share Posted April 21, 2020 (edited) A person a long time ago help me out on the php portion and I am going to repay it back now. <?php /* Makes it so we don't have to decode the json coming from javascript */ header('Content-type: application/json'); /* Grab decoded incomming data from Ajax */ $incomming = $_POST['data']; $data['outgoing'] = 'stop'; if ( $incomming === 'proceed') { $data['outgoing'] = "send"; } if ( $data['outgoing'] === 'send') { output($data); } else { errorOutput('error'); } /* Something went wrong, send error back to Ajax / Javascript */ function errorOutput($output, $code = 500) { http_response_code($code); echo json_encode($output); } /* * If everything validates OK then send success message to Ajax / JavaScript */ function output($output) { http_response_code(200); echo json_encode($output); } Edited April 21, 2020 by Strider64 Quote Link to comment Share on other sites More sharing options...
Chrisj Posted April 22, 2020 Author Share Posted April 22, 2020 Thank you for your reply. I now have this: <?php /* Makes it so we don't have to decode the json coming from javascript */ header('Content-type: application/json'); /* Grab decoded incomming data from Ajax */ $incomming = $_POST['data']; $data['outgoing'] = 'stop'; if ( $incomming === 'proceed') { $data['outgoing'] = "send"; } if ( $data['outgoing'] === 'send') { output($data); } else { errorOutput('error'); } /* Something went wrong, send error back to Ajax / Javascript */ function errorOutput($output, $code = 500) { http_response_code($code); echo json_encode($output); } /* * If everything validates OK then send success message to Ajax / Javascript */ function output($output) { http_response_code(200); echo json_encode($output); } if($_POST){ $to = 'chrisj...@.....com'; $subject = 'Thank you for your info'; $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $message1 = $_POST['message']; $headers = $name; $headers = 'from: info@.....com'; $message1 .= "\r\n\r\nName: ".$_POST['name']." \r\n Email: ".$_POST['email']." "; $message = "Hello {$_POST['name']}, ~ Thank you for your input\r\n\r\n"; mail( $to, $subject, $message1, $headers ); mail( $email, $subject, $message, $headers ); header('Location: https://....'); exit; } ?> But I am now seeing this Error Code in the Console: jquery.min.js:6 POST https://web-site-name/submit.php 500 Any additional assistance with resolving this error is appreciated Quote Link to comment Share on other sites More sharing options...
requinix Posted April 22, 2020 Share Posted April 22, 2020 3 hours ago, Chrisj said: But I am now seeing this Error Code in the Console: jquery.min.js:6 POST https://web-site-name/submit.php 500 Have you considered that might have something to do with the part of your code that deliberately returns a 500? Quote Link to comment Share on other sites More sharing options...
Chrisj Posted April 22, 2020 Author Share Posted April 22, 2020 Thanks for your reply, yes I have considered that - this section: function errorOutput($output, $code = 500) { http_response_code($code); echo json_encode($output); } but I'm not sure what with that or what to do next - any suggestion/solution is appreciated Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2020 Share Posted April 22, 2020 What does the highlighted code do? Quote Link to comment Share on other sites More sharing options...
Chrisj Posted April 22, 2020 Author Share Posted April 22, 2020 Thanks for your reply. I don't know the answer to your question, it was provided to me, in this thread, by Strider64. Any clarification/resolution will be welcomed. Quote Link to comment Share on other sites More sharing options...
Chrisj Posted April 23, 2020 Author Share Posted April 23, 2020 Ok, I have removed his code and am back to this: <?php if($_POST){ $to = 'chrisj...@.....com'; $subject = 'Thank you for your info'; $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $message1 = $_POST['message']; $headers = $name; $headers = 'from: info@.....com'; $message1 .= "\r\n\r\nName: ".$_POST['name']." \r\n Email: ".$_POST['email']." "; $message = "Hello {$_POST['name']}, ~ Thank you for your input\r\n\r\n"; mail( $to, $subject, $message1, $headers ); mail( $email, $subject, $message, $headers ); header('Location: https://....'); exit; } ?> and this: var myConfirm; $(document).ready(function() { myConfirm = new jBox('Confirm', { content: $('.my-jbox-form'), width: 830, height: 205, cancelButton: 'Return Home', confirmButton: 'Continue', closeOnConfirm: false, closeOnEsc: false, confirm: function() { $.ajax({ url: 'https://...../submit.php', method: 'post', data: { name: $('#name').val(), email: $('#email').val() }, success: function (response) { console.log(response); if (response.success) { alert('Success'); } else { alert('Error'); } } }); I look forward to any guidance with adding "validate the input and then send some response back". Quote Link to comment Share on other sites More sharing options...
Chrisj Posted April 25, 2020 Author Share Posted April 25, 2020 The goal it to close the jBox upon success and not close it upon Error. Which I believe needs validation from the submit.php to successfully close the Form/jBox. I attempted adding this call back validation code ( everything above if($_POST) ) in the submit.php file, without success: <?php header('Content-type: application/json'); $errors = array(); $data = array(); if (empty($_POST['name'])) $errors['name'] = 'Name is required.'; if (empty($_POST['email'])) $errors['email'] = 'Email is required.'; if ( ! empty($errors)) { $data['success'] = false; $data['errors'] = $errors; } echo json_encode($data); if($_POST){ $to = 'chrisj....@hotmail.com'; $subject = 'Thank You'; $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $message1 = $_POST['message']; $headers = $name; $headers = 'from: support@web-site-name.com'; $message1 .= "\r\n\r\nName: ".$_POST['name']." \r\n Email: ".$_POST['email']." "; $message = "Hello {$_POST['name']}, ~ Thank you\r\n\r\n"; mail( $to, $subject, $message1, $headers ); mail( $email, $subject, $message, $headers ); //header('Location: https://web-site-name.com'); exit; } ?> And I have the .js: var myConfirm; $(document).ready(function() { myConfirm = new jBox('Confirm', { content: $('.my-jbox-form'), width: 830, height: 205, cancelButton: 'Return Home', confirmButton: 'Continue', closeOnConfirm: false, closeOnEsc: false, confirm: function() { $.ajax({ url: 'https://...../submit.php', method: 'post', data: { name: $('#name').val(), email: $('#email').val() }, success: function (response) { console.log(response); if (response.success) { alert('Success'); } else { alert('Error'); } } }); And I’ve tried to add: myConfirm.close(); into the .js code, but not sure where is the best place to put it. After excuting the Form, still get a dialog box, on the Form page, that shows “web-site-name.com says Error”, yet the Form’s field info gets sent successfully. And after Form submit, in the dev tools > Console it only shows one line, [ ] upload.html:78 And, of course, the Form (jBox) doesn’t close. Any additional guidance/suggestions with call back and validation, and close() is appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.