user2361103
user2361103

Reputation: 101

echoing a jquery alert popup in php

I want to display a pop-up alert box upon some condition in PHP being satisfied. Something like:

  echo "<script type="text/javascript"> alert('bleh'); </script>";

except using a custom jquery alert box. Is this possible??

I've tried something like:

  echo "<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
   <link rel="stylesheet" href="/resources/demos/style.css" />
   <script>
   $(function() {
   $( "#dialog-message" ).dialog({
   modal: true,
   buttons: {
    Ok: function() {
      $( this ).dialog( "close" );
    }
  }
  });
});
</script>"; 

but it gives me a weird effect. Not pop up.

Thanks for your interest.

Upvotes: 1

Views: 4831

Answers (6)

Markus013
Markus013

Reputation: 72

Sure thing. you probably want something around this:

  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
   <link rel="stylesheet" href="/resources/demos/style.css" />
   //check for PHP condition. Script will only be output on condition pass.
   <?php if(condition == true){ ?>
   <script>
   $(function() {
    $("#dialog-message").dialog({
     modal: true,
     buttons: {
     Ok: function() {
        $(this).dialog("close");
      }
    }
   });
 });
 </script>
 <?php } ?>

basically, you don't actually need to 'echo' the script, as long as you're outputting it from a .php file and not an external .js file. the script will show up as long as the condition passes.

Upvotes: 0

Leandro Villagran
Leandro Villagran

Reputation: 300

as noted before you need to escape your character or use it like this so no need to escape characters in here

<?php if (condition == true) : ?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
   $(function() {
   $( "#dialog-message" ).dialog({
   modal: true,
   buttons: {
    Ok: function() {
      $( this ).dialog( "close" );
    }
  }
  });
 });
 </script>
<?php endif; ?>

Upvotes: 0

Sizzling Code
Sizzling Code

Reputation: 6070

Yes its possible..

echo "<script type=\"text/javascript\">alert('bleh');</script>";

but i am not sure if its a good way to write whole function in echo, Instead Define Your Function in HTML some where in your Page and echo the function name here like i did above..

Upvotes: 0

Ryan
Ryan

Reputation: 3171

PHP is expecting " (double quotes) in any lines like rel="stylesheet" to be php code. You have to either use ' (single quotes) or escape them with a function around the echo like add_slashes. To get around this, I generally use single quotes around echo contents, so all the double quotes are not bothered.

echo 'all the "quoted" stuff here';

Upvotes: 0

Reactgular
Reactgular

Reputation: 54751

Try something like this. Where $bleh is the PHP variable with the message to show.

<script type="text/javascript">
$(document).ready(function(){
   var dlg = $('<div>').text(<?php echo $bleh ?>);
   $(body).append(dlg);
   dlg.dialog(
      modal: true
   );
});
<script>

Upvotes: 0

Kivylius
Kivylius

Reputation: 6547

echo <<<EOD
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
       $("#dialog-message").dialog({
           modal: true,
           buttons: {
               Ok: function() {
                  $( this ).dialog( "close" );
               }
           }
       });
    </script>
EOD;

Upvotes: 3

Related Questions