ohno Posted October 17, 2023 Share Posted October 17, 2023 (edited) I wasn't sure if this should be in the JS/AJAX section or PHP,as the JS part seems OK I guess here! I have this at the very top of my script : - // Get the data from the AJAX request $searchQuery = isset($_POST['searchQuery']) ? $_POST['searchQuery'] : ''; $sessionid = isset($_POST['sessionid']) ? $_POST['sessionid'] : ''; // Declare as global variables // tried, no change // global $searchQuery, $sessionid; // Log the data to a text file $logData = "Received data from JavaScript:\n"; $logData .= "Search Query: " . $searchQuery . "\n"; $logData .= "Session ID: " . $sessionid . "\n\n"; $logFilePath = __DIR__ . '/log.txt'; file_put_contents($logFilePath, $logData, FILE_APPEND); This is working fine as I have checked the log file, so the payload is received OK. I then have this code lower down in my script : // Check if $searchQuery and $sessionid are not empty if (!empty($searchQuery) && !empty($sessionid)) { // Check if the sessionid from the database matches the sessionid from the AJAX payload if ($data['sessionid'] == $sessionid) { echo '<td class="center">***** ' . $searchQuery . ' *****</td>'; } else { echo '<td class="center">No search query available</td>'; } } else { echo '<td class="center">Missing search query or session ID</td>'; } // End With the above I get nothing, If I hard code a `sessionid` and a search term it appears in my HTML table for the correct user. Ie, with this it "works" : $searchQuery = 'This is where the data should appear!!'; $sessionid = '28if81v4amdsl7qlh5f5ktvc1r'; // Check if $searchQuery and $sessionid are not empty if (!empty($searchQuery) && !empty($sessionid)) { // Check if the sessionid from the database matches the sessionid from the AJAX payload if ($data['sessionid'] == $sessionid) { echo '<td class="center">***** ' . $searchQuery . ' *****</td>'; } else { echo '<td class="center">No search query available</td>'; } } else { echo '<td class="center">Missing search query or session ID</td>'; } // End Why can I not use the variables set at the top of my page? Or, how can I fix this?! Thanks for any help Edited October 17, 2023 by ohno Quote Link to comment https://forums.phpfreaks.com/topic/317381-php-variables-with-json-data/ Share on other sites More sharing options...
requinix Posted October 17, 2023 Share Posted October 17, 2023 1 hour ago, ohno said: I then have this code lower down in my script : But what's everything in between? Or rather, what's the whole script? Because there's something important here that isn't shown in what you've posted - if statements, function calls, reused variables... Also, partly for good measure and partly because there's a rather high likelihood that doing so will help you discover the problem: have you verified that your error reporting/logging settings are appropriate, and then checked for error messages? Quote Link to comment https://forums.phpfreaks.com/topic/317381-php-variables-with-json-data/#findComment-1612432 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.