Question about 'On Submit code'

aarmstrong 05 Jan, 2008
We have a form that we have setup that works great using your product. The validation is excellent and it seems to have all the features we need minus one.

I have some very unique validation that I have to do that calls a remote server. I have written a wrapper function that I can import into my PHP code (or I could embed it if needed) that does some custom validation of my form data. I need this done before the email is sent. The code I have runs fine but I need to determine if it is possible to stop the form processing and return the user to the form page and display a custom error message.


require 'custom_validation.php';

if ($error = custom_validation_function($data_i_want_to_pass))) {
  /* 
   we had an error so I need to stop the form from 
   sending and return the user back to the form and 
   display a custom error
  */
  return false; // not sure what to do
} else {
  /* no error detected so do nothing*/
}


So does this functionality exist? If it does not I can see others taking advantage of this. It could come in handy with credit card validation etc... so that it will check and process the credit card prior to sending the email.
GreyHead 05 Jan, 2008
Hi aarmstrong,

Yes you should be able to do this. If you look at the validation code (around line 191 of chronocontact.php for J1.0.x) you'll see this
if ( md5($chrono_verification ) != $_SESSION['chrono_verification'] ) {
  showErrorMessage('Sorry, You have entered a wrong verification code');
  showform($_POST);
  return;
}
Replicating this code block with your condition should do what you want.

Note that for the data to be redisplayed on the form you have to set the values to $_POST['fieldname'] in the form html code.

Bob<br><br>Post edited by: GreyHead, at: 2008/01/05 00:16
aarmstrong 14 Mar, 2008
Thanks for your reply. Your answer was exactly what i was was looking for but unfortunately it did not work exactly as I hoped it would. I was able to call my function and send everything back to the form with my own error message however it did not end execution and went ahead and sent the email. The only way I could see to prevent the email was to modify the source code at line 379 of chronocontact.php adding the few extra lines of code to check for the return value from the eval function. If it found a return value I interpret that to mean there was an error and I pass the return value as my error message.


if ( !empty($rows[0]->onsubmitcodeb4) ) {
  if ($returnval = eval( "?>".$rows[0]->onsubmitcodeb4 )) {
    showErrorMessage($returnval);
    showform($_POST);
    return;
  }
}


After this modification I was able to write code blocks like this in the onsubmit before code.


<?php
  require($_SERVER['DOCUMENT_ROOT'] . "/authnet/authnet_wrapper.php"«»);

  if (!function_exists('authnet_wrapper')) {
    return 'Sorry, credit card auth broken, please notify sysadmin.';
  }

  if ($errmsg = authnet_wrapper()) {
    return $errmsg;
  }
?>


This now allows me to call credit card processing code and stop the form from emailing if it fails. Perhaps this is something you want to add into the core functionality of your product?
GreyHead 14 Mar, 2008
Hi aarmstrong,

You are right. I'd overlooked that return in an eval block only exits the eval block. I hope that Max picks up your suggestion for better flow control.

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger