php - 识别哪个单选按钮被选中并使用 php

标签 php mysql html

这是我最近遇到的问题。我的布局和一切正常,但现在我想弄清楚这部分的 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>

最佳答案

至于您的问题(如果我没猜错的话)- 您应该将每个 radio 的 value 属性设置为您计划在服务器端代码中识别它时使用的那个,例如:

<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 />

在服务器端,所选 radio 的值将作为 POST 参数通过组名访问,在您的情况下:

$_POST['group']

并且..我非常同意 eggyal 的评论,并建议您查看准备好的语句(以及一般的 SQL 注入(inject))。

祝你好运!

关于php - 识别哪个单选按钮被选中并使用 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10365120/

相关文章:

php - 将现有帐户链接到 Facebook - 问题/问题

mysql - sql 与使用非英语字母表的 php 的排序规则

php - 使用 Eloquent ORM/laravel 准备好的声明

php - 为什么 JSON_ENCODE 不返回 "/"(斜杠) 它总是像这样返回“\/”?

html - css - 当光标在 IE 中移动时内容滚动到顶部 (leaflet js)

php - 将 Laravel 从 5.6 升级到 6.0 后,调用未定义的 str_random() 函数不起作用

php - 测试是否已经存在

javascript - 使用javascript重命名数组中的对象键

javascript - Kendo UI Scheduler 自定义时间标记

php - MySQL JOIN 或 UNION 与之前的结果集,WordPress pre_get_posts 和 post_meta