PHP form plugin error

henk

Member
I use the PHP form plugin and I want to compare the # records in two tables before form submission and display an error text. The user has to redo the input field(s) to prevent the error.
I set the plug-in to run 'onAfterProcess' with this code
$db = JFactory::getDbo();
$query =$db->getQuery(true);
$query = "SELECT COUNT(*) FROM table_1";
$db->setQuery($query);
$a = $db->loadResult();
$query =$db->getQuery(true);
$query = "SELECT COUNT(*) FROM table_2";
$db->setQuery($query);
$b = $db->loadResult();
if ($a !== $b) echo "ERROR !";
But than I always get this error: SyntaxError: Unexpected token F in JSON at position 1
Now I am lost here, what to do to make it work??
 
Last edited:
To do a validation/stopp form submission you must run "onBefore" and you must return true or false, error texts can be set via the coressponding variables.
http://fabrikar.com/forums/index.php?wiki/php-form-plugin/#stopping-form-submission.

In principal:If you get this sort of error I assume you have an ajaxfied list or form? You can use your browser console "Network" tab to get the underlying error message which is breaking the expected JSON response format.
 
Well, I dont't want the form submission to stop. The submission should complete and store in db.
But AFTER that I want to check the db and generate a error message.
So my guess would be to use onAfterProcess in this case.
In fact the second db is a view where, after submission of this form, a double record may occur.

Now I use the same code in the list for that view and that works, but I want to generate an error also after the form submission.

I don't see any more info in the browser console.
 
You can display error message in form PHP-plugin onAfterProcess like:
JFactory::getApplication()->enqueueMessage('Your Error Message', 'error');

And for query errors, var_dump the results to see what the variables actually contain, e.g. like:

var_dump($a);
exit;

And like @troester said, echo is not working here, so change to:
PHP:
if ($a != $b) {
     JFactory::getApplication()->enqueueMessage('Your Error Message', 'error');
}
 
@juuser: After using JFactory::getApplication()->enqueueMessage('Your Error Message', 'error');
I don't get the SyntaxError anymore.
But now I don't get ANY message even if I change if ($a != $b) into if ($a == $b) .
So the error message is lost in space or the onAfterProcess does not run??
 
'enqueueMessage' creates a Joomla system message which should show up during the page load after submit.

Again my question: Do you have an ajaxfied form (via ajaxfied links, content plugin, popup ...)? In this case there's no new page load, so no enqueued message to show.
Still not sure what you want to do
I want to compare the # records in two tables before form submission and display an error text. The user has to redo the input field(s) to prevent the error.
To force the user to do something you need a validation or an onBefore php plugin.
 
@troester: Yes, I have an ajaxfied form, so it's clear that the message is not shown. I understand now.
I think there is no way I can use the php form plugin for what I want to do.
I will use the code in the list intro (using sourcerer) to do that. Only that will not show up right after the form is submitted, but only after a page refresh.

So new question: is there a way to do an page request automatically?
It does that the same time after I deleted a record by showing the "record deleted" message, but not if I added a new record.
 
You can try the redirect plugin. It will show the message as a JS alert in case of ajaxfied forms.
In the condition you can do your test.
 
Thank troester that looks like a smart idea.
I tried, but I can't get it to work. It behaves the same as if I set save and next = yes (but I set that to no)
Also tried to add return true and return flase.
if ($a !== $b) {
echo "Error";
return false;
}else{
return true;
}
I have to current page as jump page, but with or without that makes no difference.
I guess I do something wrong here.
 
Don't set "Save and Next" or any other option.
upload_2022-1-31_14-32-13.png
Don't echo anything
Don't set a jump page.

Set the message.
Set a condition returning true (= plugin is run = the message will be shown) or false (= the plugin won't run).
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top