DJ-DOO
DJ-DOO

Reputation: 4781

Issue with Calling JS function through PHP based on IF statement

I have a web based tool. There is a login where username, loggedin and authorised are stored in session variables.

There is one particular page where I have a form that has multiple buttons, I wish to disable one button based on what the users authorisation level is. So if authorised is 0 (user) the button is disabled, else it's enabled as I've only got two authorisation levels, 0 & 1.

I've attached what I've done below, and to me it looks right, obviously it's not!

Here is the JQuery function:

$(function disable(){
  $('#signBtn').attr('disabled', true);
});

Here is the PHP code:

if($_SESSION['authorised'] == '0') 
{
    echo "<SCRIPT LANGUAGE='javascript'>disable();</SCRIPT>";
}

Here is my HTML code:

   <input type=\"submit\" name=\"save\"  id = \"signBtn\" class = 'eBtnSubmit' value=\"Sign off by Chairperson\" />

If there is anyone out there that can see what my problem is I would really appreciate it...this is my last piece of the puzzle and I'm presenting this today (Software Intern).

So basically if the level is 0 call the function.

Regards, Gary

Upvotes: 0

Views: 120

Answers (3)

It might be easier to simply set the value in the HTML if possible

if($_SESSION['authorised'] == '0'){
    echo '<button type="button" disabled="disabled">Click Me!</button>';
}else{
    echo '<button type="button">Click Me!</button>';
}

This would work before the page even starts loading.

The other option would be to hook the window on load function.

window.onload = (function(){ [...] }

Upvotes: 0

segFault
segFault

Reputation: 4054

is the <input type=\"submit\" name=\"save\" id = \"signBtn\" class = 'eBtnSubmit' value=\"Sign off by Chairperson\" /> being echoed from a php statement? if not you can get rid of all the '\'s. they are not necessary when writing strictly html.

Upvotes: 0

Otto Allmendinger
Otto Allmendinger

Reputation: 28268

No warranty because I don't have all the code

Change the static JavaScript Code to this

var disableSingoff = function () {
    $('#signBtn').attr('disabled', true);
}

In your original code, you execute the disable() function right when the DOM is ready by wrapping it in $().

Change the PHP code to this

if($_SESSION['authorised'] == '0') {
    echo "<script>$(function () { disableSignoff(); })</script>";
}

Upvotes: 1

Related Questions