0
//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>';
?>

これは私が使用しているコードで、ID から情報を取得するものであり、classId を取得するために非表示の要素を追加する必要がありました。次に、データベースを新しい情報で更新するように変更した後、投稿します。これは非常に奇妙な方法だと思います。これらの変数を渡すためのより良い解決策が必要です。

これを行うためのより良い方法を教えてください。ありがとう!

4

4 に答える 4

2

PHPに

jquery ajax を使用するか、フォーム ポストを使用する

JSに

json でエンコードされた値を持つ非表示の入力フィールドを使用する (データ構造が複雑な場合)

于 2013-03-08T16:05:25.007 に答える
0

jQuery $.post内にjson データ型を追加します。

jQuery

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

post_attendance.phpjson_encode で結果をエコーするよりも:

$array = array('response'=>'Some value', error => false);
echo json_encode($array);
于 2013-03-08T16:10:05.703 に答える
0

非表示の入力フィールドを使用して、エンコードされた json を保存できます。

$data = array(
    "key1" => "value1",
    "key2" => "value2"
);
$input = "<input type=\"hidden\" id=\"storage\" value=\"%s\" />";
printf($input, htmlspecialchars(json_encode($data)));

jQueryで

var storage = $.parseJSON($("#storage").val());

もう 1 つの方法は、属性を使用することです。dataこれらの属性には、 を使用してアクセスできます$("#element").data()

いくつかのクールな変更を行います。

storage["key1"] = "value3";

PHP に戻る:

$.post("url.php", storage, function(){
   alert("Alright, bro");
})
于 2013-03-08T16:15:29.347 に答える
0

あなたが利用できると思う2つの構造があります。

1 つはHTML5datasetで、マークアップのセマンティクスの一部ではない Web アプリケーションのカスタム プライベート データを保存できます。idこれは、1 つしか持つことができず、他の目的があるため、使用するよりも柔軟性がidあります。複数のclasses を持つことができますが、それも使いにくいです。

2 つ目はhidden属性で、要素がページの現在の状態に関連していない、またはユーザーが使用するものではないことを宣言します。この属性を使用して、ページのマークアップに関連しないアプリケーション データを整理する要素を作成することがあります (ただし、これが正しい使用法であることを願っていますが、私は肯定的ではありません)。

于 2013-03-08T16:09:04.867 に答える