As the Title suggests, the code below is some JS inside of a PHP file and for some reason the JS won’t go to the else if even when the if isn’t true. At it’s current state, which ever if statement is put first will run correctly but then the second one will never fire. All this system is designed to do is run x queries when staff === true and run y queries when user == true
session.php
<?php // mysqli_connect() function opens a new connection to the MySQL server. $conn = mysqli_connect("localhost", "root", "", "sports world"); session_start();// Starting Session // Storing Session $user_check = $_SESSION['login_user2']; $p = $_SESSION['login_user3']; ?> <script> if sessionStorage.getItem('status2') === 'staff') { <?php // SQL Query To Fetch Information Of User $query = "SELECT Username from staff where Username = '$user_check'"; $ses_sql = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($ses_sql); $login_session = $row['Username']; // SQL Query To Fetch Information Of User $query2 = "SELECT StaffCode from staff where Username = '$user_check' AND Password = '$p'"; $ses_sql2 = mysqli_query($conn, $query2); $row2 = mysqli_fetch_assoc($ses_sql2); $cust = $row2['StaffCode']; $_SESSION['CustCode'] = $cust; // SQL Query To Fetch Information Of User $query3 = "SELECT StaffGivenName As 'Given Name', StaffSurname As 'Surname' from staff where StaffCode = '$cust'"; $ses_sql3 = mysqli_query($conn, $query3); $row3 = mysqli_fetch_assoc($ses_sql3); $name1 = $row3['Given Name']; $name2 = $row3['Surname']; $_SESSION['Given Name'] = $name1; $_SESSION['Surname'] = $name2; // SQL Query To Fetch Information Of User $queryAccountDeatils = "SELECT StaffDob As 'Date of Birth', StaffGender As 'Gender', StaffAddress1 As 'Address', StaffAddress2 As 'Region', StaffPostCode As 'Post Code', StaffPhone As 'Phone Number', StaffEmail As 'Email', StaffTFN As 'Message Preferences', StaffEmerConName As 'Member Type', StaffEmerConPhone As 'Sports' from staff where StaffCode = '$cust'"; $ses_sql4 = mysqli_query($conn, $queryAccountDeatils); $row4 = mysqli_fetch_assoc($ses_sql4); $Dob = $row4['Date of Birth']; $Address = $row4['Address']; $Gender = $row4['Gender']; $Region = $row4['Region']; $PostCode = $row4['Post Code']; $PhoneNumber = $row4['Phone Number']; $Email = $row4['Email']; $MessagePref = $row4['Message Preferences']; $MemberType = $row4['Member Type']; $Sports = $row4['Sports']; $_SESSION['Date of Birth'] = $Dob; $_SESSION['Address'] = $Address; $_SESSION['Gender'] = $Gender; $_SESSION['Region'] = $Region; $_SESSION['Post Code'] = $PostCode; $_SESSION['Phone Number'] = $PhoneNumber; $_SESSION['Email'] = $Email; $_SESSION['Message Preferences'] = $MessagePref; $_SESSION['Member Type'] = $MemberType; $_SESSION['Sports'] = $Sports; ?> } else if (localStorage.getItem('status2') === 'user') { <?php // SQL Query To Fetch Information Of User $query = "SELECT Username from login where Username = '$user_check'"; $ses_sql = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($ses_sql); $login_session = $row['Username']; // SQL Query To Fetch Information Of User $query2 = "SELECT CustCode from login where Username = '$user_check' AND Password = '$p'"; $ses_sql2 = mysqli_query($conn, $query2); $row2 = mysqli_fetch_assoc($ses_sql2); $cust = $row2['CustCode']; $_SESSION['CustCode'] = $cust; // SQL Query To Fetch Information Of User $query3 = "SELECT CustGivenName As 'Given Name', CustSurname As 'Surname' from customers where CustCode = '$cust'"; $ses_sql3 = mysqli_query($conn, $query3); $row3 = mysqli_fetch_assoc($ses_sql3); $name1 = $row3['Given Name']; $name2 = $row3['Surname']; $_SESSION['Given Name'] = $name1; $_SESSION['Surname'] = $name2; // SQL Query To Fetch Information Of User $queryAccountDeatils = "SELECT CustDob As 'Date of Birth', CustGender As 'Gender', CustAddress1 As 'Address', CustAddress2 As 'Region', CustPostCode As 'Post Code', CustPhone As 'Phone Number', CustEmail As 'Email', CustMsgPref As 'Message Preferences', CustMemberType As 'Member Type', CustSports As 'Sports' from customers where CustCode = '$cust'"; $ses_sql4 = mysqli_query($conn, $queryAccountDeatils); $row4 = mysqli_fetch_assoc($ses_sql4); $Dob = $row4['Date of Birth']; $Address = $row4['Address']; $Gender = $row4['Gender']; $Region = $row4['Region']; $PostCode = $row4['Post Code']; $PhoneNumber = $row4['Phone Number']; $Email = $row4['Email']; $MessagePref = $row4['Message Preferences']; $MemberType = $row4['Member Type']; $Sports = $row4['Sports']; $_SESSION['Date of Birth'] = $Dob; $_SESSION['Address'] = $Address; $_SESSION['Gender'] = $Gender; $_SESSION['Region'] = $Region; $_SESSION['Post Code'] = $PostCode; $_SESSION['Phone Number'] = $PhoneNumber; $_SESSION['Email'] = $Email; $_SESSION['Message Preferences'] = $MessagePref; $_SESSION['Member Type'] = $MemberType; $_SESSION['Sports'] = $Sports; ?> } </script>
Advertisement
Answer
PHP is executed by the server prior to sending the page content to the browser, once it has arrived in the user’s browser any JS you have sent along with the HTML can be executed then.
However you will not be able to go back and run more PHP code depending on the outcome of a Javascript if statement as you are trying. You do have some options though.
Option 1:
Use PHP to compose some JS expressions or functions that will run in the client side by echo
ing valid JS expressions into the <scirpt>
tag. But again running your MySQL queries will have to happen first during the server-side phase.
Option 2:
You could also use an AJAX-style JS call to call upon a secondary PHP scripts that echo your results as JSON or some other consumable format. jQuery has AJAX functions or look into “Axios” or the browser-native “fetch()”.
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
Option 3:
Forget about Javascript if you can and write the If…Else into the PHP logic.