ajax call not working

2021

had 2FA system in 2020 working and now in 2021 having some minor issues with code not firing.

have 2 custom forms otp.php / verify1.php ( generated and sitting in the views folder)

otp is the main file that gets displayed if 2fa is enabled, the user enters the 6 digit code to validate the account with a “validate” button which then fires an ajax script (verify1.php) to check the code entered.

in otp.php, the call code is as follows:

(sURL set to current path… assuming its being executed in the views folder, but not sure)

$(“#verify”).click(function() {
var sURL = ‘Verify1.php’;
$.ajax({
url: sURL,
type: ‘POST’,
data: {
otp: $(“#otp”).val()
},
}).done(function (data) {
if(data == ‘Validated’)
window.location.assign(“Dashboard2.php”);
else
$(“#status”).html(“

Verification Failed.
”);

}).fail(function (data) {
$(“#status”).html(“

”+data+“
”);
});
});

this was firing in 2020 (the 2fa code was in its own folder), it does get called but does not actually execute the verify1.php script, along with this I’'ve noticed that $(“#status”).html(“

”+data+“
”); code is not working as well.

as well, when i view the “data” variable it appears to have code for the page html display. I’ve put break points in the controller/models classes and nothing is being hit.

i’ve also altered the sURL variable and it is behaving differntly depending on what path i put, but still verify1.php doesn’t fire.

the otp.php was generated by phpmaker and contains only my custom code and a few lines that were auto generated

<? php namespace PHPMaker2021\myNamespace; // Page object $Otp = &$Page; ?>

< php
CUSTOM 2FA CODE
?>

<? php echo GetDebugMessage(); ?>

any help appreciated

Where did you put the “Verify1.php” file, and the file that call/run this Ajax? Are both files located in “views” sub-folder?

If so, try this:

var sURL = ew.PATH_BASE + “views/Verify1.php”;

In v2021, it may be better to use a custom API action, see https://phpmaker.dev/doc/api.htm#custom, e.g. you can create a custom API action like:

function Api_Action($app)
{
$app->get(‘/verify/{otp}’, function ($request, $response, $args) {
$opt = $args[“otp”] ?? null; // Get the input value
// your code to validate
if (…) $response = $response->write(“Validated”);
return $response; // Return Psr\Http\Message\ResponseInterface object
});
};

Then you can call the API action by:

$.get(ew.getApiUrl(“verify”) + “/” + encodeURIComponent($(“#otp”).val())).done(…).fail(…);

Note: sticcino wrote:

                window.location.assign("Dashboard2.php");

You need to update the URL to the route for the dashboard also.

thanks for the info, i’ll look at the Api_Action and try to implement/learn it.

mohbar’s comment as well, seemed to fire the Verify1.php script. (all the custom files are generated with the phpmaker defaults – otp.php and Verify1.php are in the views folder)

I noticed a few changes to custom files as well.

for example the PHPGansta google auth plugin was failing to load.

added the following to the custom file, which allowed the plugin to load.

<?php // Require files $RELATIVE_PATH = "../"; // Auto load require_once $RELATIVE_PATH."vendor/autoload.php"; ?> <?php $authenticator = new \PHPGangsta_GoogleAuthenticator(); .. ?>

what happened to access to phfn.php and userfn.php. In the custom files ?.

is there no way to keep backward compatibility with custom files ?

also added the following to the file to get the functions running…

require_once $RELATIVE_PATH.“src/constants.php”;
require_once $RELATIVE_PATH.“src/config.php”;
require_once $RELATIVE_PATH.“src/phpfn.php”;
require_once $RELATIVE_PATH.“src/userfn.php”;

i’ll work on migrating to Api_Action

sticcino wrote:

is there no way to keep backward compatibility with custom files ?

I don’t think so. There are some differences in Custom Files between v2021 and v2020.

How do you get access to these variables in custom files??

global $Security
CurrentUserID()

i have implemented similar code, but have noticed that the $Security object in custom file is not availible.

i put something like this in one of my custom files, as well calls to ExecuteXXXX() fail as well

<?php namespace PHPMaker2021\myNameSpace; // Page object $myObject = &$Page; ?> <?php // Require files $RELATIVE_PATH = "../"; if (session_id() == "") session_start(); // Init session data ob_start(); // Turn on output buffering // Auto load require_once $RELATIVE_PATH."vendor/autoload.php"; require_once $RELATIVE_PATH."src/constants.php"; require_once $RELATIVE_PATH."src/config.php"; require_once $RELATIVE_PATH."src/phpfn.php"; require_once $RELATIVE_PATH."src/userfn.php"; ?>

Just make sure you have already enabled “Include common files” for your Custom Files, then you can access it.

I did, there’s absolutely nothing in the custom file (in views) except my code and a couple of lines that were generated.

this is the exact code that is generated, I think its still routing thru index.php, i don’t need it to go thru index.php.

<?php namespace PHPMaker2021\myName; // Page object $GetLevenshteinMatches = &$Page; ?> <?php $sObjectString = Post("ObjectString"); $result = ""; $sSql="SELECT id AS 'Key', `name` AS 'Company', addr_1 AS 'Address', addr_prov AS 'Province', addr_postal AS 'Postal Code', ROUND(MATCH (`name`) AGAINST ('$sObjectString' IN NATURAL LANGUAGE MODE),2) AS 'Score', levenshtein('$sObjectString', `name`) AS 'Distance', '$sObjectString' AS 'SearchValue' FROM clients WHERE MATCH (`name`) AGAINST('$sObjectString' IN NATURAL LANGUAGE MODE) >= 5.0 ORDER BY Distance ASC LIMIT 10"; $result = ExecuteJson($sSql); echo $result; ?> <?php echo GetDebugMessage(); ?>
  1. Read “Custom Files”: https://phpmaker.dev/doc/customfile.htm, see the explanation on the “Path” setting. There are 2 cases, learn the differences.

Case 1 - The page is .php file with Includes common files enabled.
Case 2 - The page is .php file with Includes common files disabled, or it not a .php file.

Depends on how you write your code, both cases work for you. For example, if you use case 1, common files are already included, you don’t need to include them yourself at all, you just need to change the URL to the route of the page (read “Migrating to v2021”: https://phpmaker.dev/doc/migrate2021.htm), e.g.

var sURL = ‘/my/site/Verify1’; // Assume lowercase file name not enabled

  1. $Security and CurrentUserID() can be used in Custom Files, you can verify by, e.g.

var_dump($Security, CurrentUserID());

i tried something a little simplier, but keep getting undefined function calls…

filename: createDocumentsPackage.php
include common files: YES
path: /createDocumentsPackage

in the class php file there is a custom button that calls JS confirmation function that then is suppose to call the custom php file to zip up the selected files.

in the php custom file that was generated, when it hits the first Post() call it fails with:

Fatal error: Uncaught Error: Call to undefined function PHPMaker2021\mySpace\Post() in C:\xampp\htdocs\myspace\views\CreateDocumentsPackage.php:9

<?php namespace PHPMaker2021\mySpace; // Page object $CreateDocumentsPackage = &$Page; ?> <?php $sAccountId = Post("AccountId"); $sAccountName = Post("AccountName"); $sObjectName = Post("PageObjectName"); in userfn.js where the script is called this the code, not sure if i'm doing/calling this correctly... function ConfirmCreateDocumentsPackageMsg(sObject, sAccountId, sAccountName, sTitle, sContent) { // confirmation var sURL = ew.PATH_BASE + '/views/createDocumentsPackage.php'; $.confirm({ theme: 'modern', // 'bootstrap', 'material', 'supervan' , 'modern' title: sTitle + ' Account: ' + sAccountId + ' ' + sAccountName, content: sContent + ' For ' + sAccountName + '
Only PDF Document Types Will Be Added To The Archive.', icon: 'fa fa-question', animation: 'scale', closeAnimation: 'bottom', buttons: { 'confirm': { theme: 'modern', // 'material', 'supervan' text: 'Proceed', btnClass: 'btn-green', action: function () { $.ajax({ url: sURL, type: 'POST', data: { PageObjectName: sObject, AccountId: sAccountId, AccountName: sAccountName }, }).done(function (msg) { ew_showMsg(msg); // location.reload(); }).fail(function (msg) { ew_showMsg("Error: " + msg); }); } }, cancel: function () { }, } }); } i've tried a few scenarios with sURL. don't think i'm grasping the new method for custom files sURL = '/mySpace/createDocumentsPackage' sURL = ew.PATH_BASE + '/createDocumentsPackage' sURL = ew.PATH_BASE + '/mySpace/createDocumentsPackage' RESULTS IN : ===>>> Route createDocumentsPackage not found.
  1. You may try: (Assume your site is at //www.mycompany.com/mySpace)

sURL = ‘/mySpace/CreateDocumentsPackage’; // or
sURL = ew.PATH_BASE + ‘CreateDocumentsPackage’; // Note: No “/” before ‘CreateDocumentsPackage’, ew.PATH_BASE already includes the trailing “/”

If you enable “Lowercase output file name”, try:

sURL = ‘/mySpace/createdocumentspackage’; // or
sURL = ew.PATH_BASE + ‘createdocumentspackage’; // Note: No “/” before ‘createdocumentspackage’, ew.PATH_BASE already includes the trailing “/”

You may check all the available routes in the source code of the generated file ‘src/routes.php’.

Replacing ‘’/mySpace/’ by ew.PATH_BASE is just not hard-coding the base path, the URL is the same and it works the same.

  1. Note also that there is no ew_XXX() functions unless it is your own code.

  2. If you write global function in client side global code, you need to use, e.g.

window.myFunction = function(…) { …}

thanks for you help and insight, appreciated.

the JS AJAX scripted ran with a return result of “done”, but the createdocumentspackage modules were not executed.

I then changed the POST to GET in the AJAX request and the createdocumentspackage actually ran

is that the way it should be???

function ConfirmCreateDocumentsPackageMsg(sObject, sAccountId, sAccountName, sTitle, sContent) { // confirmation
var sURL = ‘/mySpace/createdocumentspackage’;

$.ajax({
url: sURL,
type: ‘GET’,
data: {
PageObjectName: sObject,
AccountId: sAccountId,
AccountName: sAccountName
},
}).done(function (msg) {
alert(msg);
}).fail(function (msg) {
alert("Error: " + msg);
});
}

i’ll do some testing on it to see if it actually is working as this module

thanks

sticcino wrote:

        data: {
            PageObjectName: sObject,
            AccountId: sAccountId,
            AccountName: sAccountName
        }

I then changed the POST to GET in the AJAX request and the createdocumentspackage actually ran

Did you get the data by $_GET[“PageObjectName”] in your code so when you send ajax request by POST it doesn’t work?

You may use Param(“PageObjectName”) so that it will get the value from $_GET or $_POST.

hi,

All working now.

Did you get the data by $_GET[“PageObjectName”] in your code so when you send ajax request by POST it doesn’t work?

no, i was using the Post() functions to get the data with ajax POST in 2020.

I wasn’t aware of the Param option, I’ll review and use.

Thanks.