-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

asked Jul 11, 2016 at 9:06
11
  • 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. Commented Jul 11, 2016 at 9:08
  • extending @Epodax 's comment , simply change $pop.= to $pop = Commented Jul 11, 2016 at 9:09
  • When i do it without the concatenation, the popup does not display Commented Jul 11, 2016 at 9:10
  • try changing variable name. Commented Jul 11, 2016 at 9:10
  • changing variable name hasnt worked. it only displays when i place the concatenation, which makes no sense. Commented Jul 11, 2016 at 9:13

1 Answer 1

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
760k185 gold badges1.4k silver badges1.5k bronze badges
answered Jul 11, 2016 at 9:17
Sign up to request clarification or add additional context in comments.

3 Comments

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.
No luck. still not displaying popup
@user5898266, Refer this for variable with different variations: w3schools.com/php/func_string_echo.asp

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.