If statement using javascript inside php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Sun Apr 06, 2014 3:08 pm

If statement using javascript inside php

Postby nubeeei899 » Sun Apr 06, 2014 3:15 pm

I want to hide div "mySelect" if Emp_ID is <2000, but it seems it confusing because I have to write getElementById('mySelect') using single quote, for php which is end of echo string while my script continues, without singlequote javascript doesnt work? don't know what to do/?>

Code: Select all

      $result = mysqli_query($con,"SELECT Employee.L_name,....Employee ON Users.Emp_ID=Employee.Emp_ID WHERE username='{$_SESSION['username']}';");

      $row = mysqli_fetch_array($result);

      if ($row['Emp_ID'] <2000)
         echo '<script type="text/javascript"> document.getElementById(mySelect).style.display = "none";</script>';
         echo '<script type="text/javascript"> document.getElementById(mySelect1).style.display = "none";</script>';

New php-forum User
New php-forum User
Posts: 10
Joined: Mon Apr 07, 2014 2:26 am

Re: If statement using javascript inside php

Postby adicooldude » Mon Apr 07, 2014 2:32 am

hi ,

try this it works .

include your 'div' before including php script

Code: Select all

if (200 <2000)
   echo "<script type='text/javascript'> document.getElementById('mySelect').style.display='none';</script>";
   echo "<script type='text/javascript'> document.getElementById('mySelect1').style.display='none';</script>";

php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: If statement using javascript inside php

Postby seandisanti » Tue Apr 08, 2014 6:49 pm

The better solution would be to add 'AND Emp_ID>=2000' to your query. That way you don't waste resources collecting and presenting unwanted records to then hide them. If you don't want to go that route, let me know and I'll show you another example that doesn't involve code duplication like the one already presented.

A good rule of thumb to avoid messy code is that if you're writing the same thing twice, you're writing it too many times.

Return to “PHP coding => General”

Who is online

Users browsing this forum: Google Adsense [Bot] and 3 guests