Note that there are some explanatory texts on larger screens.

plurals
  1. POAfter php user validation look up user_id to give user certain access
    primarykey
    data
    text
    <p>I have a login form built into a site that uses PHP and JQuery to validate. However, I would like to add a function built into the PHP or the JQuery validator that will take the user_id of the user logged in and compare it to another table in the same database. The other table will have data that will allow users to select from a drop-down list of projects specific to that user that will ultimately control what the user sees in the website. The 2 tables are the "login" table and "projects" table. I think I need to fetch the user_id from the login table and compare to the user_id on the projects table to see if it matches. Not sure how to go about that.</p> <p>PHP:</p> <pre><code>&lt;?php session_start(); $con = mysqli_connect("localhost","****","****","db") or die("Connection error: " . mysqli_error($con)); $query = mysqli_query("projects"); if(isset($_POST['submit'])){ $username = $_POST['user_name']; $password = $_POST['pwd']; $stmt = $con-&gt;prepare("SELECT * FROM login WHERE username = ? AND password = ? LIMIT 1"); $stmt-&gt;bind_param('ss', $username, $password); $stmt-&gt;execute(); $stmt-&gt;bind_result($username, $password); $stmt-&gt;store_result(); if($stmt-&gt;num_rows == 1) { while($stmt-&gt;fetch()) { $_SESSION['Logged'] = 1; $_SESSION['user_name'] = $username; $_SESSION['timeout'] = time(); echo 'true'; } } else { echo 'false'; } $stmt-&gt;close(); } else{ } $con-&gt;close(); ?&gt; </code></pre> <p>JQuery:</p> <pre><code> $("#login_validation").click(function(){ var username=$('#user_name').val(); var password=$('#password').val(); if(username===''){ $("#add_err").html("*Please enter a user name."); return false; } else if(password===''){ $("#add_err").html("*Please enter a password."); return false; } else{ var loadTimeout = setTimeout(tick, 12100); $.ajax({ type: "POST", timeout:12000, url: "login_validation.php", data: "submit=true&amp;user_name="+username+"&amp;pwd="+password, beforeSend:function(){ $("#add_err").fadeIn("fast").html('&lt;img src="image/loader.gif" /&gt;'); }, success: function(html){ clearTimeout(loadTimeout); var html=trim(html); if(html=='true'){ $("#login_b").fadeOut("normal"); $("#login_b").promise().done(function(){ $('#projects').css("display", "block"); }) //$('#profile').css("display", "block"); //$("#profile").html("&lt;a href='logout_session.php' id='logout'&gt;Logout&lt;/a&gt;"); } else{ $("#add_err").html("*Wrong username or password"); } } });//end ajax return false; } });//end #login_validation $("#project_validation").click(function(){ }); var tick = function(){ $("#add_err").html('Unable to fetch page!'); } function trim(str){ var str=str.replace(/^\s+|\s+$/,''); return str; } </code></pre> <p>Form:</p> <pre><code>&lt;fieldset id="login_form_wrap" class="login_form_header"&gt; &lt;legend&gt;CUSTOMER LOGIN&lt;/legend&gt; &lt;?php session_start(); ?&gt; &lt;div id="profile"&gt; &lt;?php if((!isset($_SESSION['user_name'])) || ($_SESSION['timeout'] + 10 * 60 &lt; time())){ ?&gt; &lt;div id="login_a"&gt; &lt;a id="login_profile" href="#"&gt;click to login&lt;/a&gt; &lt;/div&gt; &lt;?php }else {;?&gt; &lt;a href='logout_session.php' id='logout'&gt;Logout&lt;/a&gt; &lt;!-- header('location: dashboard.php'); --&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;form action="login_validation.php" id="login_form" method="POST"&gt; &lt;div id="login_b"&gt; &lt;div class="welcome"&gt;&lt;/div&gt; &lt;div class="wrapper"&gt; &lt;span class="col1"&gt;USER NAME:&lt;/span&gt; &lt;input type="text" id="user_name" name="user_name" class="input" required /&gt; &lt;/div&gt; &lt;div class="wrapper"&gt; &lt;span class="col1"&gt;PASSWORD:&lt;/span&gt; &lt;input type="password" id="password" name="password" class="input" required /&gt; &lt;/div&gt; &lt;div class="wrapper"&gt; &lt;span class="col1"&gt;&lt;/span&gt; &lt;input type="submit" name"submit" id="login_validation" value="SUBMIT" /&gt; &lt;input type="reset" id="cancel_hide" value="CANCEL" /&gt; &lt;div class="err" id="add_err"&gt;&lt;br&gt;&lt;/div&gt; &lt;div id="divMayus" style="visibility:hidden"&gt;Caps Lock is on.&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="projects"&gt; &lt;div class="welcome"&gt;Welcome &lt;?php echo $_SESSION['user_name']; ?&gt;&lt;/div&gt; &lt;div class="wrapper"&gt; &lt;span class="col1"&gt;PROJECTS:&lt;/span&gt; &lt;select id="Projects" class="input"&gt; &lt;option value="" selected="selected" disabled='disabled'&gt;Choose a Project...&lt;/option&gt; &lt;?php // Loop through the query results, outputing the options one by one while ($row = mysql_fetch_array($query)) { echo '&lt;option value="'.$row['projects'].'"&gt;'.$row['projects'].'&lt;/option&gt;'; }?&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="wrapper"&gt; &lt;span class="col1"&gt;&lt;/span&gt; &lt;input type="submit" name"submit" id="project_validation" value="SELECT" /&gt; &lt;div class="err" id="add_err"&gt;&lt;br&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p></p> <p>I'm still learning JQuery and PHP so any help in the right direction is greatly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload