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
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
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.
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;
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
}
}
}
?>
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 );
}
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 );
}
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).
ASKER
Julian I can't access the link you provided
It is just a link to my first comment in this thread.
ASKER
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
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" />
And just replace it with this$disabled = $userID == 1 ? '' : 'disabled';
echo <<< BUTTON
<button id="edit" class="edit" value="Edit" {$disabled}/>
BUTTON;
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.
ASKER
i finally solved it Julian..
Here is my script..
Here is my script..
<?php
$users = $_SESSION['username'];
$query2 = "SELECT * FROM users WHERE username = '$users'";
?>
<?php
while($row = mysqli_fetch_array($result2))
{
$disabled = $row["access"] == 1 ? '' : 'disabled';
echo $row["access"];
}
?>
<input type="button" name="edit" value="Edit" id="<?php echo $row["id"];?>" class="btn btn-info btn-xs edit_data"<?php echo "{$disabled} /> "; ?>
That will do it. Personally I prefer the don't render for users - but that is your call.
ASKER
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>";
}
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.
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?
ASKER
I'll try what you seggest...Sense my current code is less secured.
Thank YOU GUYS
Thank YOU GUYS
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.
if($(this).find('option:se
$("#btnSubmit").attr('disa
else
$("#btnSubmit").attr('disa
});
I hope this works for you.