I'm using ValidForm Builder to build my forms in PHP.
I have 2 custom buttons on a ValidForm as shown in the code below. Both buttons submit the form in a manner that causes form validation to occur.
I am looking for the syntax to cause the Cancel button to either go back a page, similar to the way the browser back button works like this onclick="window.history.go(-1)"
or to cause the back button to bypass form validation.
Source Code:
// SUBMIT BUTTONS
$objGroup = $objForm->addButton('Insert User Group', array('fieldname' => 'action'));
$objGroup = $objForm->addButton('Cancel', array('fieldname' => 'cancel'));
// END USER GROUP UPDATE SECTION
// FORM DATA HANDLER
if ($objForm->isSubmitted() && $objForm->isValid()) {
require_once(CORE_SQL . 'users_userGroups.php');
if (strpos(strtolower($_POST['action']), 'insert') !== false) insertUserGroup();
elseif (strpos(strtolower($_POST['action']), 'update') !== false) updateUserGroup($_POST['userGroupID']);
elseif (strpos(strtolower($_POST['action']), 'delete') !== false) deleteUserGroup($_POST['userGroupID']);
header('Location: ' . PATH_USERS . 'userGroups.html');
exit;
}
else {
$strOutput = $objForm->toHtml();
}
Screen Shot of Cancel Button onClick Behavior can be seen this link
If you add this meta array:
array(
"fieldname" => "cancel",
"fieldid" => "form-cancel-button"
);
You can then use Javascript to handle this buttons' actions. Since you've already included jQuery for use with ValidForm Builder, you can do something similar like this:
$("#form-cancel-button")
.off("click") // remove ValidForm's default handlers
.on("click", function () {
window.history.go(-1);
return false;
});
That should do the trick. But in my opinion, a cancel button shouldn't 'go back'. A cancel button should just clear the form and stay on the current page.
By the way, I see you're using $_POST to fetch data from your posted form. That's a big no-go. ValidForm Builder enables you to validate and sanitize user input before using it in the rest of your application (like SQL queries). The syntax to extract a value from ValidForm Builder (when submitted and validated) is this:
$strFieldValue = $objForm->getValidField("fieldname")->getValue();
Thisway, you know the user input has been validated by ValidForm Builder and is safe to use.