0

これが私の最新の問題です。レイアウトとすべてが機能していますが、今はこの部分のphp側を理解しようとしています。私がやりたいのは、phpを使用してどのラジオボタンがチェックされているかを具体的にどのように言うかです。$ _POST ['ins']('ins'は挿入ボタンのID)を使用してみましたが、機能しませんでした。挿入ボタンがチェックされているときと同じように機能させたいのですが、どのような手順でも実行できますが、取得方法がわからないので、挿入ボタンが選択/チェックされていると言います。

これが私のコードです

<html>
<head><title></title>
<script type="text/javascript">

function show()
{
if (document.getElementById('ins').checked){
document.getElementById("p1").style.display = "block"
document.getElementById("zn").style.display = "block";
document.getElementById("p2").style.display = "block"
document.getElementById("gpa").style.display = "block";
document.getElementById("p3").style.display = "none"
document.getElementById("ngpa").style.display = "none";
}
else if (document.getElementById('upd').checked){
document.getElementById("p1").style.display = "block"
document.getElementById("zn").style.display = "block";
document.getElementById("p2").style.display = "none"
document.getElementById("gpa").style.display = "none";
document.getElementById("p3").style.display = "block"
document.getElementById("ngpa").style.display = "block";
}
else {
document.getElementById("p1").style.display = "block"
document.getElementById("zn").style.display = "block";
document.getElementById("p2").style.display = "none"
document.getElementById("gpa").style.display = "none";
document.getElementById("p3").style.display = "none"
document.getElementById("ngpa").style.display = "none";
}
}

function check()
{
    if((document.getElementById("zn").value == "") ||   (document.getElementById("gpa").value == "") ||
    (isNaN(parseFloat(document.getElementById("gpa").value))))
    {
        alert("Please complete both fields and check to see if the GPA entered is a number!");
        return false;
    }
    return true;

}
</script>



</head>
<body>
<form action = "index.php" onsubmit="return check();">
<input type="radio" name="group" id = "ins" onclick ="show()"/> Insert Student <br />
<input type="radio" name="group" id = "upd" onclick ="show()"/> Update Student <br />
<input type="radio" name="group" id = "del" onclick ="show()"/> Delete Student <br />
<p id="p1" style="display:none">Enter Z-number: <br /></p>
<input type='text' name = 'z' id ="zn" maxlength='9' style="display:none"/>
<p id="p2" style="display:none"><br />Enter GPA: <br /></p>
<input type='text' name='gpa' id ="gpa" style="display:none"/>
<p id="p3" style="display:none"><br /> Enter new GPA: <br /></p>
<input type="text" name ="ngpa" id ="ngpa" style="display:none"/>
<br /><input type='submit' value='Submit request' name='submit'/>
</form>
<?php
if (isset($_POST['z'])) {
$con = mysql_connect('localhost', '*****', '***********');
if ($con) {
mysql_select_db('ghamzal', $con);

$a = $_POST['z'];
$b = $_POST['gpa'];
$sql = "INSERT INTO Students VALUES (' $a ' ,  '$b')";

if (mysql_query($sql, $con)) echo 'Operation succeeded';
else echo 'Not succeeded ' .mysql_error();




if (isset($_POST['ins'])) {
$sql = "SELECT * FROM Students";
$res = mysql_query($sql, $con);

echo "<table border ='1'>";
echo "<tr><th>Znum</th><th>GPA</th></tr>";
while ($r = mysql_fetch_array($res)) {
echo "<tr><td>".$r['Znum']."</td><td>".$r['gpa']. "</td></tr>";
}
echo "</table>";
}
mysql_close($con);
}
else {
echo 'Insertion failed'. 'Could not connect to DB.';}

}

?>
</body>
</html>
4

2 に答える 2

3

あなたの質問については (私が正しければ)、各ラジオの属性を、サーバー側のコードで識別するときに使用する予定のものに設定する必要があります。次に例を示します。

<input type="radio" name="group" value="ins" id = "ins" onclick ="show()"/> Insert Student <br />
<input type="radio" name="group" value="upd" id = "upd" onclick ="show()"/> Update Student <br />
<input type="radio" name="group" value="del" id = "del" onclick ="show()"/> Delete Student <br />

サーバー側では、選択されたラジオの値は、グループの名前で POST パラメータとしてアクセスできます。

$_POST['group']

そして..eggyalのコメントに強く同意し、準備されたステートメント(および一般的なSQLインジェクション)を確認することをお勧めします。

幸運を!

于 2012-04-28T16:13:54.657 に答える
2

ここには、いくつかの異なる問題があります。1 つ目は、フォームが GET メソッドを使用して送信されているが、POST メソッドで送信されたかのように PHP で値を取得しようとしているということです。データベースの値を更新しているため、POST メソッドが望ましいため、フォームの開始タグを次のように変更する必要があります。

<form method="post" action="index.php" onsubmit="return check();">

入力の「名前」フィールドは最終的に$_POST変数になり、その変数の値は選択された入力の値になります。

したがって、入力を次のように変更する必要があります。

<input type="radio" name="selection" value="ins" id="ins" onclick="show()"/><label for="ins">Insert Student</label><br />
<input type="radio" name="selection" value="upd" id="upd" onclick="show()"/><label for="upd">Update Student</label><br />
<input type="radio" name="selection" value="del" id="del" onclick="show()"/><label for="del">Delete Student</label><br />

(マークアップを追加したのは、<label>マークアップを含めることをお勧めするためです。アクセシビリティに役立ちます。ラジオ ボタンのラベルをクリックすると、ラジオ ボタン自体をクリックしたものとして処理されます。)

今あなたのPHPコードで:

$selection = $_POST['selection'];

$selectionどのラジオ ボタンが選択されたかを表す値が含まれるようになり、適切なロジックを実行できます。

if ($selection == "ins") {
     // do work here
} elseif ($selection == "upd") {
     // do work here
}
// etc.
于 2012-04-28T16:12:26.043 に答える