Help with validating web Form from ajax

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 = '[email protected]';
	$subject = 'Thank you for your info';
	$name = $_POST['name'];
	$email = $_POST['email'];
	$message = $_POST['message'];
	$message1 = $_POST['message'];
	$headers = $name;
	$headers = 'from: [email protected]';
	$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:

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');
    }
  }
});

Any guidance with validating the input and then sending some response back, will be appreciated.

So, filter_var to validate the email format, insure that the headers are getting injected, check that there actually are values, and because you are using AJAX, print a json response back to the caller.

Thanks for your reply.
Can you provide an example of “filter_var to validate the email format, insure that the headers are getting injected, check that there actually are values, and because you are using AJAX, print a json response back to the caller”, please? I don’t know how to add that.
I look forward to any additional guidance.

Look up filter_var PHP

Has your book gone over any of this?

Any additional help besides 'look it up or ‘google it’ is appreciated

https://www.php.net/manual/en/function.filter-var.php

I am not seeing a whole lot of effort on your part? I would think after 4 years, you would have gained some sort of understanding on how to use things like isset, !empty, something, rather than asking for examples that you can also look for and only ask when there is something you don’t understand rather than having someone else do it all, or 90%, for you?

Sponsor our Newsletter | Privacy Policy | Terms of Service