Charlie Simon
Charlie Simon

Reputation: 143

Run JavaScript from PHP if statement

so I saw a few similar questions like this but none worked for me so I thought I might as well ask. So I'm trying to make a popup load if the member's ID isn't in a database. I have all the PHP and SQL working for the database but I'm struggling to run the function for the JavaScript.
I tried

if (condition){ 
   my database code;
  }else{
     my database code;
     echo "function()"
  }


I also tried

if (condition){ 
   my database code;
  }else{
     my database code;
     echo "<script type="text/javascript">function();</script>"
  }

but that still didn't work? I really want this on my website so if someone knows another way that would be AWESOME :)
Thanks in advance.

Upvotes: 2

Views: 1779

Answers (2)

PHP Web
PHP Web

Reputation: 257

Use this shortcut method:-

echo "<script>myFunction()</script>" ;

Upvotes: 0

Sheshank S.
Sheshank S.

Reputation: 3278

Try this:

if (condition){ 
   my database code;
} else {
   my database code;
   echo '<script type="text/javascript">function();</script>'
}

What was happening was that in "<script type="text/javascript">function();</script>" there were double quotes that weren't being escaped. Using singlequotes fixes this.

Upvotes: 3

Related Questions