This topic is locked

Using Sweetalert to display messages in Before Record Added event

1/16/2021 3:43:57 PM
PHPRunner General questions
M
marcelorribas author

I hope everyone is ok.

I run several tests before add a record and today I use the "alert()" function to display the error messages to user, but I would like to use the new Sweetalert plugin, but the code below doesn´t show anything:
====================

echo "

<script type=\"text/javascript\">

swal('Inclusão em andamento. Por favor, aguarde...', {

buttons: false,

timer: 30000,

icon: 'info',

});

</script>";

=====================
Any sugestions?
Thanks in advance,

D
david22585 1/16/2021



I hope everyone is ok.

I run several tests before add a record and today I use the "alert()" function to display the error messages to user, but I would like to use the new Sweetalert plugin, but the code below doesn´t show anything:
====================

echo "

<script type=\"text/javascript\">

swal('Inclusão em andamento. Por favor, aguarde...', {

buttons: false,

timer: 30000,

icon: 'info',

});

</script>";

=====================
Any sugestions?
Thanks in advance,


How are you getting the error? Are you using server side code VIA the events tab to check the data and make the error?

M
marcelorribas author 1/17/2021

Hi,

I run the tests in "Before record added" event.

D
david22585 1/17/2021



Hi,

I run the tests in "Before record added" event.


Check out my list of ways to use sweetalerts here:
https://asprunner.com/forums/topic/28037-sweetalerts-different-methods/
But for your case, here is the code:
Events -> Before record added/updated

if ($values["value"] == "some kind of data"){

// Shows the error message at the top of the page as well like how PHPRunner normally does

$message = "This is a sample error message";

// Proxy for the sweet alert

$pageObject->setProxyValue("error", "Insert any error message here");

return false;

} else {

return true;

}


JavaScript OnLoad Event

if (proxy["error"]){

swal({

title: "Error",

text: "proxy["error"]",

icon: "error",

button: "Okay",

timer: 5000,

});

}


If you had multiple checks on the data, it would be like this:

if ($values["name"] == ""){

$message = "Your name cannot be blank";

$pageObject->setProxyValue("error", "Your name cannot be blank");

return false;

} else if ($values["email"] == "something"){

$message = "Invalid email address";

$pageObject->setProxyValue("error", "Invalid email address");

return false;

{ else if ($values["address"] == "wrong_data3"){

$message = "Address must be valid";

$pageObject->setProxyValue("error", "Please use your proper address");

return false;

} else {

return true;

}
M
marcelorribas author 1/17/2021

Thank you so much, David for your help.

I'll implement your code.

Marcelo

M
marcelorribas author 1/18/2021

Hi David, how you doing?

Only today I was able to implement your code, but unfortunately it didn´t work for me.

I have created a little project to use as a test. Steps follow below:
1 - I put on "Before record added" the code below:

--------------------------------

IF ($values['listorder']<>1){

$pageObject->setProxyValue("error", "Sorry, you need to type 1");

return false;

}

else

{

return true;

}

-------------------------------

2 - In JavaScript OnLoad event (on Add page)

-------------------------------

if (proxy["error"]){

swal({

title: "Error",

text: "proxy["error"]",

icon: "error",

button: "Okay",

timer: 5000,

});

}

-----------------------------

The test works ok (the record is not saved) but nothing is displayed.
I can´t see what I´m doing wrong...

M
marcelorribas author 1/18/2021



Hi David, how you doing?

Only today I was able to implement your code, but unfortunately it didn´t work for me.

I have created a little project to use as a test. Steps follow below:
1 - I put on "Before record added" the code below:

--------------------------------

IF ($values['listorder']<>1){

$pageObject->setProxyValue("error", "Sorry, you need to type 1");

return false;

}

else

{

return true;

}

-------------------------------

2 - In JavaScript OnLoad event (on Add page)

-------------------------------

if (proxy["error"]){

swal({

title: "Error",

text: "proxy["error"]",

icon: "error",

button: "Okay",

timer: 5000,

});

}

-----------------------------

The test works ok (the record is not saved) but nothing is displayed.
I can´t see what I´m doing wrong...


Hi David, I think I found the problem. You can´t use double quotes in [text] parameter to display a variable name.

I have altered your code and now is working great.

Thanks again,
----------------------------------------

if (proxy["error"]){

swal({

title: "Sorry, we have a problem!",

text: proxy["error"],

icon: "error",

button: "Ok",

timer: 5000,

});

}

----------------------------------------

Regards,

Marcelo

D
david22585 1/19/2021



Hi David, I think I found the problem. You can´t use double quotes in [text] parameter to display a variable name.

I have altered your code and now is working great.

Thanks again,
----------------------------------------

if (proxy["error"]){

swal({

title: "Sorry, we have a problem!",

text: proxy["error"],

icon: "error",

button: "Ok",

timer: 5000,

});

}

----------------------------------------

Regards,

Marcelo


Hi Marcelo,
You're exactly right. You can put in anything you want for text if you use the double quotes, but if you want to display the proxy value, you cannot use the double quotes at all. That was my bad on the code, I did a copy/paste and edit and forgot those in there by accident. Glad to see it's working!

M
marcelorribas author 1/19/2021



Hi Marcelo,
You're exactly right. You can put in anything you want for text if you use the double quotes, but if you want to display the proxy value, you cannot use the double quotes at all. That was my bad on the code, I did a copy/paste and edit and forgot those in there by accident. Glad to see it's working!


I´ve just noticied another thing: If you use the "open in popup mode" to add the record, Sweetalert doesn´t work. Nothing is displayed. Do you have any ideas how to do an workaround?
Thanks,

D
david22585 1/19/2021



I´ve just noticied another thing: If you use the "open in popup mode" to add the record, Sweetalert doesn´t work. Nothing is displayed. Do you have any ideas how to do an workaround?
Thanks,


Try adding this to After Rrecored Added/Updated:



$pageObject->stopPRG = true;

$pageObject->setProxyValue("saved", true);
M
marcelorribas author 1/19/2021



Try adding this to After Rrecored Added/Updated:



$pageObject->stopPRG = true;

$pageObject->setProxyValue("saved", true);



Hi David,

Unfortunately, the problem remains. In popup mode, nothing is displayed.

D
david22585 1/20/2021



Hi David,

Unfortunately, the problem remains. In popup mode, nothing is displayed.


I'll play with it later this afternoon or this evening (eastern United States time) and get back with a hopeful solution then.

M
marcelorribas author 1/20/2021



I'll play with it later this afternoon or this evening (eastern United States time) and get back with a hopeful solution then.



Thank you in advance.

D
david22585 1/20/2021



Thank you in advance.


I just tried a bunch of different ways and couldn't get it to work. This may be one for Sergey to address. Sorry Marcelo.

M
marcelorribas author 1/20/2021



I just tried a bunch of different ways and couldn't get it to work. This may be one for Sergey to address. Sorry Marcelo.


There´s no reason to sorry David. Your previous code has already help me a lot.

I thank you so much for your efforts and good will to help.

Regards,

Marcelo

M
marcelorribas author 1/21/2021

Hi David, how are you?

I received a tip from Sergey to use this article as inspiration - https://xlinesoft.com/phprunner/docs/how_to_ask_for_confirmation_before_saving_record.htm

and from that I was able to make a simple adaptation to display the Sweetalert messages before the record has been added (either in full or pop-up mode).

Unfortunately I was unable to make an adaptation to use to show messages about error situations (as your code does).
JavaScript OnLoad Event (add pages)

-------------------------------------

this.on('beforeSave', function(formObj, fieldControlsArr, pageObj){

{

swal({

title: "Por favor, aguarde!",

text: "Inclusão e envio de e-mail de alerta aos voluntários em andamento!",

icon: "info",

button: "Ok",

timer: 15000,

});

}

});



-------------------------------------
It is very useful in situations where recording information takes longer than usual.

Regards,

Marcelo