0

i have create a custom Alert Dialog Box but i am not able to call it in php

below is the code of alert dialog box

<script>
function CustomAlert(){
this.render = function(dialog){
    var winW = window.innerWidth;
    var winH = window.innerHeight;
    var dialogoverlay = document.getElementById('dialogoverlay');
    var dialogbox = document.getElementById('dialogbox');
    dialogoverlay.style.display = "block";
    dialogoverlay.style.height = winH+"px";
    dialogbox.style.left = (winW/2) - (550 * .5)+"px";//position of dialog box 
    dialogbox.style.top = "200px"; //height of the box 
    dialogbox.style.display = "block";
    document.getElementById('dialogboxhead').innerHTML = "Registraion Succesfull";
    document.getElementById('dialogboxbody').innerHTML = dialog;
    document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()">OK</button>';
}
this.ok = function(){
     window.open("employer/personal","_self");
}
}var Alert = new CustomAlert();

below is the php code

<?php 
echo "<script> CustomAlert(Alert.render('You look very pretty today.'));        </script>";
?>
  • You cannot call a javascript function in PHP. By the time the javascript runs, the PHP has finished. In order to run it, you need to trap a user event ([see addEventListener](http://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick)) such as a button click, scrolling to a particular location, clicking into an input field, etc. – cssyphus Jun 26 '16 at 13:29
  • Does your PHP file also echo or include the script block that defines your CustomAlert and Alert objects, or does it just echo the single line? In other words, if you view the page's source after it renders, what does it look like? – stratedge Jun 26 '16 at 13:29

1 Answers1

0

The code you provided works. My guess is, you execute the php code before the javascript code, which would result in an undefined Alert-var. Include you js in the head-tag would solve this problem.

<html>
<head>
    <script src="alert.js"></script>
</head>
<body>
    ...
    <div id="dialogoverlay"></div>
    <div id="dialogbox">
        <div id="dialogboxhead">HEAD</div>
        <div id="dialogboxbody">BODY</div>
        <div id="dialogboxfoot">FOOT</div>
    </div>
    ...
    <?php echo "<script>Alert.render('You look very pretty today.');</script>"; ?>
</body>
</html>
Philipp
  • 15,377
  • 4
  • 35
  • 52