Link to home
Start Free TrialLog in
Avatar of Yaku Kakashi
Yaku Kakashi

asked on

PHP JQuery MySQL How to disable button if

Hello guys I know it is so simple with you guys.
How can I disable button if userID is not equal to "1"

<button id="edit" class="edit" value="Edit"/>

How can I disable that button if userID is not equal to 1


Thank You in advance
Avatar of Shruti Upari
Shruti Upari
Flag of India image

$("select").on('change',function(){
   if($(this).find('option:selected').id()=="1")
       $("#btnSubmit").attr('disabled',true)
   else
       $("#btnSubmit").attr('disabled',false)
});


I hope this works for you.
Avatar of Julian Hansen
You have not given us any reference code so it is very difficult to answer this question.
Here is some general code that sets a disabled value based on the value of $userID and then includes that in the button code.
$disabled = $userID == 1 ? '' : 'disabled';
echo <<< BUTTON
<button id="edit" class="edit" value="Edit" {$disabled}/>
BUTTON;

Open in new window

Try this code:-

<?php 
	$userid="1";
	$query=mysqli_query($conn,"SELECT * FROM table");
		if(mysqli_num_rows($query) > 0){
			while($row = mysqli_fetch_assoc($query)){
				if($userid != $row['userid']){
					?>
					<!-- if condition false-->
					<?php
				}else{
					?>
					<button id="edit" class="edit" value="Edit"/>
					<?php
				}
                     }
              }
?>

Open in new window

Avatar of Yaku Kakashi
Yaku Kakashi

ASKER

Sorry for late response.
What I want to happen is to disable  
Edit button
<input type="button" id="edit" class="edit" value="Edit"/>

now i separated my users as Admin and User using user_rights
Admin has a value of 1 and user is 2 in my mysql structure.

now I want user not to edit anything on my database so my sulotion is to disable Edit button for them not to do anything not necessary as user privileges.

$access = 1;
if($access != $row['user_rights']
{
$( "#edit" ).prop( "disabled", true );
}
elese
{
$( "#edit" ).prop( "disabled", false );
}
$access = 1;
if($access != $row['user_rights']
{
$( "#edit" ).prop( "disabled", true );
}
elese
{
$( "#edit" ).prop( "disabled", false );
} 

Open in new window

This is wrong and a very bad idea. Anything you do in script can be undone. You don't want to be disabling key functionality with script.

Either do as recommended here and add disabled property as part of page render
OR
Don't render the button at all for users (preferred option).
Julian I can't access the link you provided
It is just a link to my first comment in this thread.
How can I apply that into full script Julian...I wanna try that one
Just insert it into the script that renders the page. I can't show that to you as you have not shared your code with us.

In the PHP script that creates the page the button will be on - find where you draw the button i.e where this line is
<button id="edit" class="edit" value="Edit" />

Open in new window

And just replace it with this
$disabled = $userID == 1 ? '' : 'disabled';
echo <<< BUTTON
<button id="edit" class="edit" value="Edit" {$disabled}/>
BUTTON;

Open in new window

NOTE: PLEASE change the variable names ($userID) to match your script. I don't have access to your code so I have to present a generic solution - you need to adapt this to your specific environment.
i finally solved it Julian..
Here is my script..
<?php
$users = $_SESSION['username'];
$query2 = "SELECT * FROM users WHERE username = '$users'"; 
?>

Open in new window

  <?php  
          while($row = mysqli_fetch_array($result2))
{
$disabled = $row["access"] == 1 ? '' : 'disabled';
echo $row["access"];
}
 ?> 

Open in new window

<input type="button" name="edit" value="Edit" id="<?php echo $row["id"];?>" class="btn btn-info btn-xs edit_data"<?php echo "{$disabled} /> "; ?>

Open in new window

That will do it. Personally I prefer the don't render for users - but that is your call.
Do you have any options how to do it in other way?...
Yes just don't output the button

if ($row["access"] == 1) {
echo <<< BUTTON
<input type="button" name="edit" value="Edit" id="<?php echo $row["id"];?>" class="btn btn-info btn-xs edit_data">
BUTTON;
}
else {
   echo "<span></span>";
}

Open in new window

Is it possible to hide the entire Edit column instead of just the button Julian?User generated image
Again - just don't render it - check your row and if the value is a 1 then render the extra cell (<td>) otherwise don't.

It is very difficult to answer your questions with no context. You are asking questions about how to render a page but we have not seen any code on how the page is being rendered so very difficult to give you a solution.

Therefore I am giving you generic solutions.

In general your PHP script will
a) Get some rows from a DB
b) Iterate over those rows
c) For each row output 1 row of the table
d) Within c) it will iterate over all the output columns for the table and render out each one inside <td> </td> tags

What I am suggesting is that as part of d) when it gets to the part where the Edit coloumn is output - it checks the access status and then if it is a 1 it outputs the cell with the <button> otherwise it does not.

This action would need to be replicated for the rendering of the header row.
Agree wholeheartedly with Julian...don't render the button.  Either way, you are ensuring that you check for user privileges when your PHP scripts receive a call to Edit, etc, right?
I'll try what you seggest...Sense my current code is less secured.
Thank YOU GUYS
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.