-3

i dont even know how to write the question , but i have a list of check boxes and i can insert their data as a json to my database using php . what i want now is when i open the page without refreshing, grab the json and check boxes that needs to be checked and leave the others empty .

i'm aware that ajax can pull this off but i have no ajax skills what so ever

My HTML

      <div style='display:none;' id="priv">                                                
       <li><input name="chk[]" id="settings" value="settings" type="checkbox">Can Access Settings</input></li>
       <li><input name="chk[]" id="view_team" value="view_team" type="checkbox">Can View Team Data</input> </li>
       <li><input name="chk[]" id="delete_team" value="delete_team" type="checkbox">Can Delete Team Profile</input></li>
       <li><input name="chk[]" id="edit_team" value="edit_team" type="checkbox">Can Edit Team Profile</input> </li> 

the is a select menu before these check boxes that contains a few items , so whenever i choose an item , it should send a request to my database , get the checked values and fill those checkboxes accordingly

<select onchange="showDiv('priv', this)" id="role_selector" name="roles">
<option value='0' disabled selected >Please Choose</option><                             
 <option value='0' disabled selected >Admin</option>                                     
 <option value='0' disabled selected >Editor</option>

i have a javascript function to show and hide the div that contains the check boxes

function showDiv(divId, element){
    document.getElementById(divId).style.display = element.value == 1 ? 'block' :'none';
}

my PHP code

if (isset($_POST['chk'])) {
    $access=$_POST['chk'];
}
$json=json_encode($access);
$sql= "UPDATE `roles` SET privilege='$json' WHERE `id`=$role_id ;";
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Can you share some server-side code; I mean you can make an ajax request but your server must have an endpoint right? – Lakshaya U. Aug 31 '21 at 16:32
  • i have added my server side code but i can only add to the database , i can get data from the database but i have no idea how to use javascript with it for the checkboxes – karzan Nawzad Aug 31 '21 at 16:36
  • just to clearify , i have no code regarding getting the data back from the database . i can get them through php ,that is easy , but i dont know how to add them to the checkboxes . that is why i havent written anything , since i dont know where to begin – karzan Nawzad Aug 31 '21 at 16:38
  • 1
    Share your full code, i can re write the code – Ajmal PraveeN Aug 31 '21 at 16:38
  • @AjmalPraveen how do you want me to send the code ? and what part Do you want ? client side or server side ? – karzan Nawzad Aug 31 '21 at 16:42
  • 1
    Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Aug 31 '21 at 16:43
  • 1
    @karzanNawzad the server side code in particular. – Lakshaya U. Aug 31 '21 at 16:44
  • @RiggsFolly copy and pasting code to stackoverflow question is a nightmare , otherwise on my computer using vs code the spacing and indentation is acceptable – karzan Nawzad Aug 31 '21 at 16:45
  • Actually you can google some php beautifier. – Lakshaya U. Aug 31 '21 at 16:47
  • @karzan Nawzad I have provide you the solution ;) and i welcome you. https://stackoverflow.com/a/69002519/8530310 – Ajmal PraveeN Aug 31 '21 at 16:48
  • @hacKaTun3s the PHP i wrote in the question above is all i have there is noting in there . i simply take the checked boxes values and add them to the database . – karzan Nawzad Aug 31 '21 at 16:48
  • See https://stackoverflow.com/a/29972553/2310830 for sensible vscode settings. Make a tab into 4 spaces and not a tab. Works universally – RiggsFolly Aug 31 '21 at 16:48
  • 2
    Your script is open to [SQL Injection Attack](http://stackoverflow.com/questions/60174). Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187) You should alway use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's instead of concatenating user provided values into the query. Never trust ANY user input! – RiggsFolly Aug 31 '21 at 16:49
  • 1
    @AjmalPraveen has the correct implementation. Try to get that done with your code. – Lakshaya U. Aug 31 '21 at 16:50
  • @RiggsFolly security will be handled later after i make everything work . plus this is a side project that i will use for learning purposes , so it is not a concern yet – karzan Nawzad Aug 31 '21 at 16:51
  • 2
    Learning the wrong way and then the right way is either a waste of time and energy or most likely you will never learn the right way – RiggsFolly Aug 31 '21 at 16:52
  • `security will be handled later after i make everything work... 1) security should always be your first thought, not your last, 2) it's a waste of time to implement and test something when you already know it will need important parts of it to be re-written and re-tested at the end. Just do it properly from the start and then you won't get into bad habits or waste time, or just forget to make the changes. 3) any respectable specification would include the security requirements in it, so it would be impossible to define the code as "working" anyway until it meets those requirements. – ADyson Aug 31 '21 at 18:11
  • `this is a side project that i will use for learning purposes , so it is not a concern`...this is the wrong logic. If you're trying to learn, then learn to do things the right way not the wrong way, then you don't have to re-learn later. – ADyson Aug 31 '21 at 18:14

2 Answers2

1

This is an Example Solution to Do

<?php
// This is an example to edit by Ajmal Praveen

/*
If database role value are following, you can use as below. which is the simple and right solution for you.
Admin = 1
Editor = 2
*/

$role = $row['role'];

?>
<option value="" disabled selected required>Please Choose</option>
<option value='0' <?php if ($role = '1') { echo 'selected'; } ?> >Admin</option>                                     
<option value='1' <?php if ($role = '2') { echo 'selected'; } ?> >Editor</option>

Hope you understood now, you can modify and develop it as you wish.

Ajmal PraveeN
  • 414
  • 8
  • 16
  • i didn't downvote , others did it . but you miss understood the question . we have select with 2 roles , when you click on admin it shows all the check boxes that indicates the privileges the admin has – karzan Nawzad Aug 31 '21 at 16:53
  • 1
    We are misunderstanding because you are not explaining yourself very well and You are basically asking us to either design, or design and write code from a Specification. That is not what StackOverflow is for, we help each other fix code related issues we are having, we dont write code for you – RiggsFolly Aug 31 '21 at 16:54
  • i swear i'm doing my best , but english isnt my first language , so programming in general and learning is a challenge for me – karzan Nawzad Aug 31 '21 at 16:55
  • 2
    @karzan Nawzad So you need to provide the code, without sharing it how we can help you ? Think. I understood what you are saying. you need to create a unique column for a user, to mark it as admin or user for ex: 1 = admin and 0 = user. and we check whether he can permission to edit or anything. or if you wanna do it more complicated, then you need to created more tables, such write, read, permissions in separate table. Hope you understood what i was saying.. I worked with lot of user, admin dashboards. that 'sway i was suggesting you. – Ajmal PraveeN Aug 31 '21 at 16:57
  • @AjmalPraveen i will give it a shot , thank you – karzan Nawzad Aug 31 '21 at 17:00
  • @karzan Nawzad i always welcome you, give a try :) – Ajmal PraveeN Aug 31 '21 at 17:14
0

What I could understand by your question is..

  1. You are requesting a php file to give the privileges details for 2 users {select}.
  2. And after requesting you should get the checkboxes to be checked according to the user.

What I couldn't understand

  1. UPDATE roles SET privilege='$json' WHERE id=$role_id what is this query doing, are you trying to change the privileges..?

Anyways, I will do something like this...

Working Example here

HTML

<select onchange="showpriv(this)" id="role_selector" name="roles">
<option value='0' disabled selected >Please Choose</option><                             
 <option value='1' disabled selected >Admin</option>                                     
 <option value='2' disabled selected >Editor</option>
</select>

<div style='display:none;' id="priv">                                                
               <li><input name="chk[]" id="settings" value="settings" type="checkbox">Can Access Settings</input></li>
               <li><input name="chk[]" id="view_team" value="view_team" type="checkbox">Can View Team Data</input> </li>
               <li><input name="chk[]" id="delete_team" value="delete_team" type="checkbox">Can Delete Team Profile</input></li>
               <li><input name="chk[]" id="edit_team" value="edit_team" type="checkbox">Can Edit Team Profile</input> </li>
        </div>
<!-- I am gonna load the scripts here to check and uncheck boxes accordingly -->
<div class="privscripts"></div>

Script/JQuery {for ajax request}

function showpriv(elem){
   $.post("privileges.php",
       { role : elem.value, type : "privilege" },
       function(data, status){
          if(status == "success"){ 
              console.log(data);
              document.getElementById('priv').style.display='';
              $('.privscripts').html(data);
            }
          else console.log("something went wrong");
       }
);
}

PHP code

<?php
 if(isset($_POST['type'])){
    if($_POST['type']== "privilege"){//Just to ensure everything goes right...!
       if($_POST['role'] == 1){//If admin
          //Echo out script to check/uncheck boxes
          echo "<script> 
          document.getElementById('settings').checked = true;
          document.getElementById('view_team').checked = true;
          document.getElementById('delete_team').checked = true;
          document.getElementById('edit_team').checked = true;
          </script>";
        }
        else if($_POST['role'] == 2){//If editor
          //Echo out script to check/uncheck boxes
          echo "<script> 
          document.getElementById('settings').checked = false;
          document.getElementById('view_team').checked = true;
          document.getElementById('delete_team').checked = false;
          document.getElementById('edit_team').checked = true;
          </script>";    
        }
     }
  }
?>

You can interact with DB and do your stuff as you wish, I have given an example. You can ECHO out html/json/or some error/debug code, and then do your stuff in AJAX function accordingly, explore and learn.

For any queries feel free to comment down.

Mohammed Khurram
  • 616
  • 1
  • 7
  • 14