2

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.

2
  • 1
    You are not quoting your string correctly. Maybe this works for you: echo "<script>function();</script>";. If not you have to provide a more complete example. Commented Jun 23, 2018 at 5:14
  • It would be cleaner if you made a PHP variable like say $is_member and stored that in a JavaScript variable, so you didn't have to sprinkle JavaScript in your PHP Commented Jun 23, 2018 at 5:15

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey so I know you answered my question, but I have another quick small one, so I'm trash at Javascript and I'm trying to run my function when the if statement is executed but it isn't working?? When I put the function onto a button it works perfectly but not when I just have functionname() would you like me to send you my code or should I make another question? This would help me so much :) thanks <3
It might be better to make another question @CharlieSimon Also, remember to upvote the answer ;)
@CharlieSimon Send the link to the question here
0

Use this shortcut method:-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.