Forum
Form Validator for value in responseJSON
I've been trying to figure this out for weeks, but I keep running into dead ends. I'm using Perch Shop, and I've created a code generator that creates a code at checkout, and associates it with a given order. This data is then stored in a form, using the perch:form functionality. I want to then allow a user to enter this code, and have perch validate its existence from the responseJSON column of the perch2_forms_responses table. I've created a validator class for this.
class Perch_Forms_Validators
{
public static function check_promocode($text)
{
$API = new PerchAPI(1.0, 'perch_forms');
$db = $API->get('DB');
$sql = 'SELECT COUNT(*) FROM perch2_forms_responses WHERE responseJSON='.$db->pdb($text);
$count = $db->get_count($sql);
if ($count > 0) {
return true;
}
return false;
}
}
I recognize that the above code with reference the entire JSON string. I'm trying to figure out if there is any way to reference a specific value within the JSON data. Can I somehow use
decode_json(responseJSON, true);
I'm using version 5.6.33 of MySQL, so there is no native JSON functionality.
Yes, you can use that. JSON is native from PHP 5.3.
Thanks for the response Drew. I guess what I'm asking is how would I be able to check "Count" on a value within a JSON string? In know mySQL 5.7 offers JSON_EXTRACT() as an option, but I'm not sure if anything like that is available on an earlier version of mySQL.
Unfortunately we don't have the capacity to be able to offer general PHP or MySQL coding advice through free Perch support.