如何将变量传递给javascript并从php返回? //Javascript 函数updateStudentAttendance(事件) { if($(this).hasClass('here')){ $(this.removeClass('here'); $(this.addClass('缺席'); var schoolId=$(this.children('p').attr('id'); var studentId=$(this.attr('id'); var classId=document.getElementById(“classId”).value; 后勤(学生ID、学校ID、classId、“缺席”); } 功能后注意力(学生ID、学校ID、classId、attendanceEvent) { $.post('post_attention.php'{ “学生ID”:学生ID, “学校ID”:学校ID, “事件”:attendanceEvent, “classId”:classId },函数(数据){ //警报(数据); }); } //php代码如下所示: 到PHP

如何将变量传递给javascript并从php返回? //Javascript 函数updateStudentAttendance(事件) { if($(this).hasClass('here')){ $(this.removeClass('here'); $(this.addClass('缺席'); var schoolId=$(this.children('p').attr('id'); var studentId=$(this.attr('id'); var classId=document.getElementById(“classId”).value; 后勤(学生ID、学校ID、classId、“缺席”); } 功能后注意力(学生ID、学校ID、classId、attendanceEvent) { $.post('post_attention.php'{ “学生ID”:学生ID, “学校ID”:学校ID, “事件”:attendanceEvent, “classId”:classId },函数(数据){ //警报(数据); }); } //php代码如下所示: 到PHP,php,javascript,html,variables,Php,Javascript,Html,Variables,使用jqueryajax或使用formpost 致JS 使用带有json编码值的隐藏输入字段(如果数据结构复杂)我认为您可以使用两种结构 一个是,它允许您为不属于标记语义的web应用程序存储自定义私有数据。这比使用id更灵活,因为您只能有一个id,并且它还有其他用途。您可以有多个类es,但这也很难使用 第二个是属性,它声明一个元素与页面的当前状态无关/供用户使用。我有时使用此属性创建元素来组织与页面标记无关的应用程序数据(虽然我希望这是一个正确的用法,但我不是肯定的).在jQuery中添加一些j

使用jqueryajax或使用formpost

致JS
使用带有json编码值的隐藏输入字段(如果数据结构复杂)

我认为您可以使用两种结构

一个是,它允许您为不属于标记语义的web应用程序存储自定义私有数据。这比使用
id
更灵活,因为您只能有一个
id
,并且它还有其他用途。您可以有多个
es,但这也很难使用

第二个是属性,它声明一个元素与页面的当前状态无关/供用户使用。我有时使用此属性创建元素来组织与页面标记无关的应用程序数据(虽然我希望这是一个正确的用法,但我不是肯定的).

在jQuery中添加一些json数据类型:

jQuery

//Javascript
function updateStudentAttendance(event)
  {
    if ($(this).hasClass('here')) {
  $(this).removeClass('here');
  $(this).addClass('absent');
  var schoolId = $(this).children('p').attr('id');
  var studentId = $(this).attr('id');
  var classId = document.getElementById("classId").value;
  postAttendance(studentId, schoolId, classId, 'Absent');
}

function postAttendance(studentId, schoolId, classId, attendanceEvent)
{
  $.post('post_attendance.php', {
  'studentId': studentId,
  'schoolId': schoolId,
  'event': attendanceEvent,
  'classId': classId
}, function(data) {
 // alert(data);
});
}

//php code looks like this:
<?php
    print sprintf('<div id="%s" class="student span2 well well-small %s">
        <input type="hidden" id="classId" name ="classId" value="%s">'
        , $student->getId(), $class, $classId);
    print '<img src="img/userp.png" style="width: 100%; text-align: center;">';
    print sprintf('<p id="%s" style="text-align: center; font-size: 12px;">%s %s'
        , $_GET['schoolId'], $student->getFirstName, $student->getLastSurname());               
    print '</p>';
    print '</div>';
?>
而不是在您的
post_attention.php上使用json_encode回复您的结果:

 $.post('post_attendance.php', {data:value},
    dataType:"json"
}, function(data) {
   alert(data.error); //will be false
});

您可以使用隐藏的输入字段来存储编码的json

$array = array('response'=>'Some value', error => false);
echo json_encode($array);
另一种方法是使用
数据
属性,可以使用
$(“#元素”).data()访问这些属性

做一些很酷的改变:

var storage = $.parseJSON($("#storage").val());
回到PHP:

storage["key1"] = "value3";

Cookie或回显变量或隐藏的输入字段。你现在做的很好。只需将变量写入JS,然后发布?我其实不太懂javascript,我正在修改其他人的代码。你如何将其写入JS?
storage["key1"] = "value3";
$.post("url.php", storage, function(){
   alert("Alright, bro");
})