-1

Hi I have a jquery popup script I am trying to call in a php function.

The script calling the function is:

<link rel='stylesheet' href='css/sweetalert.css'>
<script src='js/sweetalert-dev.js'></script>
<script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>

<?php

include('functions/notification.php');

$notification_message = "There is a problem loading Smart Telecom at the moment.";

notify($notification_message);

?>

and the function call in notification.php:

<?php
function notify($message)
{
        $pop.= "<script>sweetAlert('". $message ."', 'Our Sincere apologies.', 'error');</script>";

        echo $pop;
}

?>

As it is, the pop up will display with an error about undefined variable $pop. If I try to disable error notifications, the popup doesn't display. if I do it without the concatenation, the popup also doesn't display.

Where am I going wrong?

UPDATE - SOLVED:

Got it to work. echoed "nbsp;", just before echoing $pop

user5898266
  • 355
  • 2
  • 6
  • 20
  • That is because you are using `.=` in your `notify` function, `.=` means to add something to existing variable, but since the variable doesn't exist before your `$pop .=` line, it throws that undefined variable message. - Remove the dot from the `$pop .=` and you should be set. – Epodax Jul 11 '16 at 09:08
  • extending @Epodax 's comment , simply change `$pop.=` to `$pop =` – Noman Jul 11 '16 at 09:09
  • When i do it without the concatenation, the popup does not display – user5898266 Jul 11 '16 at 09:10
  • try changing variable name. – Noman Jul 11 '16 at 09:10
  • changing variable name hasnt worked. it only displays when i place the concatenation, which makes no sense. – user5898266 Jul 11 '16 at 09:13
  • Have you checked the console for JS/jQuery errors whie the concat was removed from `$pop.=` ? / Inspect the html / js output from the function? – Epodax Jul 11 '16 at 09:15
  • try changing code to this swal('". $message ."', 'Our Sincere apologies.', 'error'); – Peternak Kode Channel Jul 11 '16 at 09:16
  • No luck. I checked the console and it did have an uncaught error "Uncaught TypeError: Cannot read property 'className' of null" – user5898266 Jul 11 '16 at 09:31
  • Well that is most likely the culprit then, when you remove the dot from `$pop .=` and run the function, what is the HTML output then? Is the syntax correct? – Epodax Jul 11 '16 at 09:34
  • You could try to load jQuery before you load sweetalert, I'm not sure if that can be an issue in this case. So first instead of sweetalert – McBurgerKong Jul 11 '16 at 09:41
  • The syntax should be okay. when i run the script and and view page source, the css call is in red – user5898266 Jul 11 '16 at 09:43

1 Answers1

0

Ok so follow these steps to ensure the code is working, then check the pop issue:

  1. Delete all data in the function to look like below:

    <?php
    function notify($message) {
        echo $message;
    } //output: data passed to $message variable should be displayed?>
    
  2. if echo $message works then

    <link rel='stylesheet' href='css/sweetalert.css'>
    <!-- You must always place the jQuery first and then call functions or it will not work -->
    <script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>
    <script src='js/sweetalert-dev.js'></script>
    <?php
    function notify($message) {
        $pop = "<script>sweetAlert('$message', 'Our Sincere apologies.', 'error');</script>";//refer link: http://stackoverflow.com/questions/10512452/php-using-a-variable-inside-a-double-quotes
        echo $pop;
    }?>
    
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ravistm
  • 2,163
  • 25
  • 25
  • What have you changed and why? Code only answers rarely provide much insight for the OP and future visitors, a good answer explains what OP was doing wrong / what you have changed to make it work. – Epodax Jul 11 '16 at 09:22
  • No luck. still not displaying popup – user5898266 Jul 11 '16 at 09:22
  • @user5898266, Refer this for variable with different variations: http://www.w3schools.com/php/func_string_echo.asp – Ravistm Jul 11 '16 at 09:54