求助:PHP变量$thedate通过JSON传递至JS变量datestr失败
Hey, let's work through getting that $thedate variable from mainclass.php into the datestr variable in your getsched() function over in footer_script.php. The problem with your current code is that footer_script.php doesn't have access to $thedate unless you explicitly pass it over—here are a few reliable ways to fix this:
方法1:提前注入全局JS变量(最直接,适用于footer_script依赖mainclass渲染的页面)
If your footer_script.php is loaded within the HTML page generated by mainclass.php, you can first output a global JS variable in mainclass.php that your JS can reference later:
In mainclass.php (place this in your HTML before loading footer_script.php):
<script> // Safely convert PHP variable to a JS-compatible value window.globalTheDate = <?php echo json_encode($thedate); ?>; </script>
Then update your getsched() function in footer_script.php:
function getsched(str) { // Some existing code $.ajax({ type: 'POST', url: 'mainclass.php', data: 'date=' + str + '&form=getsched', success: function(data) { var datestr = window.globalTheDate; // Use the pre-injected global variable $("#" + str).html(data); } }).error(function() { alert('Request failed—check your AJAX setup!'); // Fixed: original alert(data) won't work here }); }
方法2:Return date alongside AJAX response(适合需要动态日期的场景)
If $thedate is generated or updated during the AJAX request to mainclass.php, modify the server-side code to return a JSON object containing both your HTML content and the date:
In mainclass.php (where you handle the getsched form request):
if ($_POST['form'] === 'getsched') { // Your existing logic to generate the HTML content $scheduleHtml = // ... your existing HTML output code ...; // Package content and date into a JSON response $response = [ 'html' => $scheduleHtml, 'thedate' => $thedate ]; header('Content-Type: application/json'); echo json_encode($response); exit; }
Then adjust the AJAX callback in footer_script.php:
function getsched(str) { // Some existing code $.ajax({ type: 'POST', url: 'mainclass.php', data: { date: str, form: 'getsched' }, // Object syntax avoids string concatenation issues dataType: 'json', // Tell jQuery to expect JSON success: function(response) { var datestr = response.thedate; // Pull date from the response $("#" + str).html(response.html); // Insert the schedule HTML } }).error(function(jqXHR, textStatus) { alert('Request failed: ' + textStatus); // More meaningful error message }); }
方法3:Use HTML data attributes(适合纯JS footer_script files)
If footer_script.php is a plain .js file (not parsed by PHP), store the date in an HTML element's data-* attribute and retrieve it with JS:
In mainclass.php (add this to a top-level element like <body>):
<body data-date="<?php echo htmlspecialchars(json_encode($thedate), ENT_QUOTES); ?>"> <!-- Rest of your page content --> </body>
Then update your getsched() function:
function getsched(str) { // Some existing code $.ajax({ type: 'POST', url: 'mainclass.php', data: 'date=' + str + '&form=getsched', success: function(data) { // Parse the date from the data attribute var datestr = JSON.parse(document.body.dataset.date); $("#" + str).html(data); } }).error(function() { alert('Request encountered an error!'); }); }
内容的提问来源于stack exchange,提问作者Mel

