You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助: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
  }); 
}

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:08:09