2016-07-17 2 views
0

クラスのリストの学生を表示するプログラムを作成しています。各学生名の前には、休暇、現在、休暇の3つのラジオボタンがあります。PHPのラジオボタン変数の提出

生徒のリストはwhileループで生成されます。今度は、このような変数にラジオボタンの値を渡したいと思います。

echo "<tr>" 
    . "<td>$id</td>" 
    . "<td>$name</td>" 
    . "<td><input type=radio name=$name value=P></td>" 
    . "<td><input type=radio value=L name=$name></td>" 
    . "<td><input type=radio name=$name value=A></td>" 
    . "</tr>"; 

このようなラジオボタンの値を送ることは可能ですか?

+0

はい。しかし、3つすべてのラジオボタンの名前は同じでなければならず、他のレコードや他のラジオボタングループでは繰り返さないでください。名前は 'name = 'your_name''のようにしてください。variableを使ってボタンの 'value'を渡すこともできます。 –

答えて

0

はい、可能です。コード:

<?php 

$students = [ 
    [ 
     'id' => '1', 
     'name' => 'Amy' 
    ], 
    [ 
     'id' => '2', 
     'name' => 'Bob' 
    ], 
    [ 
     'id' => '3', 
     'name' => 'Charlie' 
    ]  
]; 

echo 
'<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' . 
'<table>' . 
' <thead>' . 
'  <tr>' . 
'   <th>Student Id</th>' . 
'   <th>Student Name</th>' . 
'   <th>Attendance (P/L/A)</th>' . 
'  </tr>' . 
' </thead>' . 
'  <tbody>'; 

foreach ($students as $student) { 
    echo 
    '  <tr>' . 
    '   <td>' . $student['id'] . '</td>' . 
    '   <td>' . $student['name'] . '</td>' . 
    '   <td>' . 
    '    <input type="radio" name="attendance_' . $student['id'] . '" value="P" />' . 
    '    <input type="radio" name="attendance_' . $student['id'] . '" value="L" />' . 
    '    <input type="radio" name="attendance_' . $student['id'] . '" value="A" />' . 
    '   </td>' . 
    '  </tr>'; 
} 

echo 
'  <tr>' . 
'   <td colspan="3">' . 
'    <input type="submit" name="submit" value="Attendance" />' . 
'   </td>' . 
'  </tr>' . 
' </tbody>' . 
'</table>' . 
'</form>'; 

if (isset($_POST['submit'])) { 
    echo '<pre>' . print_r($_POST, true) . '</pre>'; 
} 

、フォームに提出:はい

Array 
(
    [attendance_1] => P 
    [attendance_2] => L 
    [attendance_3] => L 
    [submit] => Attendance 
) 
3

が、あなたの入力属性が単一/二重引用符を持つべきであることに注意してください。

echo "<input type=radio name='$name' value='p' />"; 

か、この

echo "<input type=radio name=\"$name\" value=\"P\">"; 

のような二重引用符をエスケープするか、連結することができますPHPの変数

echo '<input type=radio name="'.$name.'" value="P">"; 

UPDATE: 各学生のプロフィールをチェック

  1. whileループ内に配列名を作成します。 アレイ名はこのようなものです。角括弧の後に文字列「attendance」があります。

    <input name="attendance[]" /> 
    
  2. 今、各学生の行について。配列名の中に生徒IDを割り当てます。あなたの配列名の内部で$ IDを置く

    echo "<tr>" 
    . "<td>".$id."</td>" 
    . "<td>".$name."</td>" 
    . "<td><input type='radio' name='attendance[".$id."]' value='P'></td>" 
    . "<td><input type='radio' name='attendance[".$id."]' value='L'></td>" 
    . "<td><input type='radio' name='attendance[".$id."]' value='A'></td>" 
    . "</tr>"; 
    
  3. 各学生の出席状況を更新するために、バックエンドでそれを使用し、あなたのポインタとして機能します。

    foreach ($_POST['attendance'] as $key => $value) { 
    //$_POST['attendance'] variable is an array. 
    //Where $key variable is your Student ID, use this to update their status 
    //Where $value is a student's selected attendance status. 
    
        echo 'Student ID:'. $key . ' is '. $value . '<br>'; 
        //Update a student's attendance status using $key as their id. 
    } 
    

また、私が作ったこのコードをシミュレートすることができます:あなたはこのような名前の周りの単一引用符または二重引用符を含める必要がhttp://viper-7.com/Tb1lbo

+0

感謝します...しかし、次のページには何がありますか。$ nameOpt = $ _ POST ['$ name'];私たちはこれのように書くことができますか? –

+0

はい、正しいです。 PHP変数を割り当てることは、文字列でなければ引用符を付ける必要はありません。 – dresdain

+0

$ _POST ['$ name']の一重引用符を削除します。 $ nameは文字列ではなくPHP変数です。 – dresdain

0

まず:

<tr><td>$id</td><td>$name</td><td><input type='radio' name='$name' value='P'></td><td><input type='radio' value='L' name='$name'></td><td><input type='radio' name='$name' value='A'></td></tr>"; 

あなたはその後、使用することができますラジオボタングループの名前。リクエストタイプに応じて$_POSTまたは$_GET超大域から値を取得します。

$name = 'name_of_the_radios' 
$studentPresence = $_POST[$name] //For get requests use $_GET 
if($studentPresence == 'P') 
{ 
    //Student is present 
} 
else if($studentPresence == 'A') 
{ 
    //Student is absent 
} 
else if($studentPresence == 'L') 
{ 
    //Student is on leave 
} 
else 
{ 
    throw new Exception("Invalid student presence argument'); 
} 
+0

一方($行=は、mysql_fetch_array($結果)){ $番号= $行[ 'ID']上記を参照してください。 $ name = $ row ['name']; は​​$ ID​​$名​​の​​<入力タイプ= 'ラジオ' 値= 'L' NAME = '$名をエコー'>​​ "; } ここは自分のコードです –

関連する問題