dataxspy Posted January 29, 2023 Share Posted January 29, 2023 (edited) Basically nothing happens, I know clicking the button works and connecting to the db works but I'm unsure how to diagnose what is wrong with the javascript. It could be a syntax error but I've scanned for variable errors, wrong spelling. driver.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My Website</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> </head> <body> <form method="POST"> <input type="text" id="search" size="2"> <input type="button" name="submit" id="submit" value="Search Drivers"> </form> <table id="tbl_driver" style="display:none"> <tr><th>Last Name</th><td id="Last_Name"></td></tr> <tr><th>First Name</th><td id="First_Name"></td></tr> </table> <script> $(document).ready(function() { $("#submit").click(function() { $.ajax({ url:"fetch.driver.info.php", type:"POST", data:{driver:$("#search").val()}, dataType:"JSON", success:function(data) { $("#tbl_driver").show(); $("#Last_Name").text(data.Last_Name); $("#First_Name").text(data.First_Name); alert( "Handler for .click() called." ); } }) }); }); </script> </body> </html> fetch.driver.info.php <?php require 'db.inc.php'; $id = $_POST["driver"]; $sql = "select * FROM Drivers WHERE Driver_ID = $id"; $query = mysqli_query($con,$sql); $data = array(); while($row=mysqli_fetch_array($query)) { $data['Last_Name']=$row['Driver_Last_Name']; $data['First_Name']=$row['Driver_First_Name']; } echo json_encode($data); ?> db.inc.php <?php $con = new mysqli("localhost","root","","United_Limousine"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } ?> Edited January 29, 2023 by dataxspy Quote Link to comment https://forums.phpfreaks.com/topic/315849-cant-find-error-in-php-mysql-jquery/ Share on other sites More sharing options...
Solution Barand Posted January 29, 2023 Solution Share Posted January 29, 2023 Use your browser's developer tools network tab to check what is being sent and received by the ajax request Quote Link to comment https://forums.phpfreaks.com/topic/315849-cant-find-error-in-php-mysql-jquery/#findComment-1605173 Share on other sites More sharing options...
mac_gyver Posted January 29, 2023 Share Posted January 29, 2023 (edited) edit: repeats advice already given - use the browser's developer console network tab to see what is being sent from the javascript and returned by the server-side code. next, your search requires an exact match with existing data. you should use a select/option menu instead. also, don't put external, unknown, dynamic values directly into an sql query. use a prepared query instead. you would probably want to switch to the much simpler PDO database extension, since using the mysqli extension with a prepared query is overly complicated and inconsistent. don't unconditionally output raw database connection/query errors onto a web page. the connection error contains the database hostname/ip, username, if you are using a password or not, and web server path information. when a hacker intentionally triggers a connection error, by flooding your site with requests that consume all the available connections, the existing connection error handling will give them 2 or all 3 pieces of information they need to connect to your database. instead use exceptions for database statement errors (the latest php versions automatically does this for mysqli connection errors anyways, so your existing connection error handling will never be executed) and in most cases simply let php catch and handle any database exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed/logged the same as php errors.) this will let you remove any database statement error handling logic you have now, simplifying the code. for a query that is expected to match at most one row of data, don't use a loop. just fetch that single row of data. Edited January 29, 2023 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/315849-cant-find-error-in-php-mysql-jquery/#findComment-1605174 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.