-1

I have a problem with some PHP / JavaScript. I know a bit PHP but new to Javascript. I'm trying to get the JavaScript alertify to work with php.

My Q's: 1: How can I parse a PHP variable to a Javascript alertify function

2: It need to be a if php statement, because

if ($id==1){*show alertify};
elseif($id==2){*Do nothing*};

3: And when back to PHP again.

4: What function do I need to call to get the alertify on page load?

The logic:

enter image description here

Please examples in JavaScript because i'm totally new :)

My code so far:

 <?php include_once "config.php"; 
if(!isset($_SESSION['idUserType'])){

$sql = mysql_query("SELECT * FROM Test WHERE id = $id"); 

while($row = mysql_fetch_array($sql )){
$idStatus = $row["idStatus"];

    if($idStatus==1){

    echo '<BODY onLoad="confirm()">';
    echo '<script>window.onload = confirm();</script>';
    ;} 

    elseif($idStatus==2){

    $sqlsearchoutput .= 'Do nothing'
    ;}

    }}?>

JavaScript code:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="jscss/alertify.min.js"></script>
<script>
        function reset () {
            $("#toggleCSS").attr("href", "jscss/alertify.default.css");
            alertify.set({
                labels : {
                    ok     : "ok",
                    cancel : "cancel"
                },
                delay : 5000,
                buttonReverse : false,
                buttonFocus   : "ok"
            });
        }

        // Standard Dialogs

        $("#confirm").on( 'click', function () {
            reset();
            alertify.confirm("Comfirm", function (e) {
                if (e) {
                    alertify.success("You clicked: Cancel");
                } else {
                    alertify.error("You clicked: OK");
                }
            });
            return false;
        });
    </script>
Albzi
  • 15,431
  • 6
  • 46
  • 63
chrpust
  • 17
  • 1
  • 2
  • 7
  • 1
    Java is not the same as javascript. – Albzi Mar 28 '14 at 11:43
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) - First make absolutely sure you really understand this. – deceze Mar 28 '14 at 11:46

1 Answers1

0

I think your problem is that the "JavaScript" is inserted into the document after the php echos:

if($idStatus==1){

echo '<BODY onLoad="confirm()">';
echo '<script>window.onload = confirm();</script>';
;} 

You will need to have them both inserted at the same time.

Dean Meehan
  • 2,511
  • 22
  • 36