Jump to content

Recommended Posts

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 by ohno
Link to comment
https://forums.phpfreaks.com/topic/317381-php-variables-with-json-data/
Share on other sites

  • ohno changed the title to PHP variables with JSON data
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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.