Validate Code Entry vs MySQL DB

Hi guys and gals,

Great plugin! Attempting to build a form to help us with a client request. They are running a contest where users will enter a unique code to register for a special event. We want to prevent each code from being used more than one time and codes must match predetermined list of codes. I am attempting to do this via a MySQL call via gform_validation which checks against already existing codes and whether they have already been submitted. On a multipage form, I would like the code to be validated before the are allowed to move to the second page. However the validation never seems to take place, and the form proceeds on to the second page of form. Here is the code I have written to accomplish this. Could you assist in letting me know where its failing?

add_filter( 'gform_field_validation_7_1', 'validate_code' );

function validate_code($validation_result){
	$form = $validation_result['form'];

	foreach( $form['fields'] as &$field ) {

	if ( strpos( $field->cssClass, 'validate-code' ) === false ) {
	    continue;
		}
	}

	$field_value = rgpost( "input_1" );

	$is_valid = is_code( $field_value );

	if ( $is_valid ) {
	   /continue;
	}

	$validation_result['is_valid'] = false;

	$field->failed_validation = true;
	$field->validation_message = 'The code you have entered is not valid. Please enter another.';

	$validation_result['form'] = $form;

	return $validation_result;
}

function is_code($code){
	// check the DB for the correct USPBL Code and validate.  Upon success update row to make sure
	$info = 'mysql response';

	$conn = mysqli_connect("$hostname", "$u", "$p", "$d");
	if (mysqli_connect_errno()) {
	    printf("Connect failed: %s\n", mysqli_connect_error());
	    echo 'Unable to connect to server, please use the back button in your browser and resubmit the form';
	    exit();
	}

	//$code = mysqli_real_escape_string($conn, $_POST['email']);

	$sql_check = mysqli_query($conn, "SELECT * FROM $t WHERE codes = '$code' AND valid = 0");
	$sql_checkrows = mysqli_num_rows($sql_check);
	if ($sql_checkrows > 0) {
		$sql_update = "UPDATE $t SET valid = 1 WHERE codes ='$code'";
		if (mysqli_query($conn, $sql_update)) {
			$info = mysqli_affected_rows($conn) . " Row updated.\n";
			$info = 'success';
			return true;
	} else {
		return false;
		}
		mysql_close($conn);
	}
}

I have found that it’s easiest to determine where this is going wrong if you use the built in Gravity Forms logging.

Enable logging for Gravity Forms core. Then, add some custom logging statements. I like to log any variable right after you set the value, or inside a loop to be sure you made it into the log.

Also, why aren’t you using all four arguments for the filter?

add_filter( 'gform_field_validation_7_1', 'validate_code', 10, 4 );
function validate_code( $result, $value, $form, $field ) {

I like to log the $_POST right inside the function, and the $value and $field as well.

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.