Jump to content

Help with Form field response


Chrisj

Recommended Posts

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

 

Link to comment
Share on other sites

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 by Strider64
Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.