简体   繁体   中英

Jquery Dialog Pop Up Box Set With PHP Variable Error?

I seem to be having an issue with my jquery alert box, I'm trying to trigger them using a number variable called msg however I'm setting it using php $msg I'm using this to parse the variable over var msg = '<?php echo $msg ?>'; which I thought was the conventional way. However when I run it won't load and outputs the following error:

Uncaught SyntaxError: Invalid or unexpected token

Now I know its var msg = '<?php echo $msg ?>'; causing the problem as if I changed it to var msg = '1'; it loaded my first alert box.

If anyone knows how I can resolve this that would be great!

JAVASCRIPT

<script>

    var msg = '<?php echo $msg ?>';

    $( function() {

        $( "#dialog1" ).dialog({

            autoOpen: false,

            show: {

                effect: "puff",

                duration: 300

            },

            hide: {

                effect: "clip",

                duration: 500

            }

        });

        if(msg == 1){

            $( "#dialog1" ).dialog( "open" );

        }

        $( "#opener" ).on( "click", function() {

            $( "#dialog1" ).dialog( "open" );

        });

    }); 

    $( function() {

        $( "#dialog2" ).dialog({

            autoOpen: false,

            show: {

                effect: "puff",

                duration: 300

            },

            hide: {

                effect: "clip",

                duration: 500

            }

        });

        if(msg == 2){

            $( "#dialog2" ).dialog( "open" );

        }

        $( "#opener" ).on( "click", function() {

            $( "#dialog2" ).dialog( "open" );

        });

    });       

</script>

PHP

<?php

    $msg = 1;

?>

HTML

<html>

    <head>

        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>

        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    </head>

    <body>

        <div id="dialog1" title="Title!">

            <p>This is dialog box number 1.</p>

        </div>

        <div id="dialog2" title="Title!">

            <p>This is dialog box number 1.</p>

        </div>

    </body>

</html>

hmm, try removing the single quotes around <?php echo $msg; ?> <?php echo $msg; ?> putting those single quotes is making it a string, but you want it to be an integer in JavaScript when you are doing the comparison.

This error you are getting is due to php code placed after js, for sure.

Their are 3 ways to do so -

  1. Place js code at the end of file.

If you don't get the first way just check out second way

  1. Alter your js code in this way -
var msg = <?php echo isset($msg) ? $msg : '2' ?>;

Now here you have to pass some default value in our case we passed 2 if $msg is not set.

But if your value is dyanamic then i recommend you to go with third way, it will work under every circumstances-

  1. echo js from php code itself to avoid such syntax error. ie
<?php
    $msg = 1;
    echo "<script>var msg = $msg</script>";
?>

All 3 ways will surely work for you but i recommend you to follow third approach.

Hope you get this right :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM