识别选中的单选按钮并使用php

识别选中的单选按钮并使用php,php,mysql,html,Php,Mysql,Html,这是我最近的问题。我已经完成了布局和所有工作,但现在我正在尝试了解这部分的php方面。我想做的是如何具体说明使用php检查哪个单选按钮?我尝试使用$_POST['ins'](其中'ins'是插入按钮的id),但没有成功。我希望它能像选中insert按钮时一样工作,执行任何步骤,但我不知道如何获得它,所以说insert按钮正在被选中/选中 这是我的密码 <html> <head><title></title> <script type="tex

这是我最近的问题。我已经完成了布局和所有工作,但现在我正在尝试了解这部分的php方面。我想做的是如何具体说明使用php检查哪个单选按钮?我尝试使用$_POST['ins'](其中'ins'是插入按钮的id),但没有成功。我希望它能像选中insert按钮时一样工作,执行任何步骤,但我不知道如何获得它,所以说insert按钮正在被选中/选中

这是我的密码

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

函数show()
{
if(document.getElementById('ins')。选中){
document.getElementById(“p1”).style.display=“块”
document.getElementById(“zn”).style.display=“block”;
document.getElementById(“p2”).style.display=“块”
document.getElementById(“gpa”).style.display=“block”;
document.getElementById(“p3”).style.display=“无”
document.getElementById(“ngpa”).style.display=“无”;
}
else if(document.getElementById('upd')。选中){
document.getElementById(“p1”).style.display=“块”
document.getElementById(“zn”).style.display=“block”;
document.getElementById(“p2”).style.display=“无”
document.getElementById(“gpa”).style.display=“无”;
document.getElementById(“p3”).style.display=“block”
document.getElementById(“ngpa”).style.display=“block”;
}
否则{
document.getElementById(“p1”).style.display=“块”
document.getElementById(“zn”).style.display=“block”;
document.getElementById(“p2”).style.display=“无”
document.getElementById(“gpa”).style.display=“无”;
document.getElementById(“p3”).style.display=“无”
document.getElementById(“ngpa”).style.display=“无”;
}
}
函数检查()
{
if((document.getElementById(“zn”).value==”)| |(document.getElementById(“gpa”).value==”)||
(isNaN(parseFloat(document.getElementById(“gpa”).value)))
{
警告(“请填写这两个字段并检查输入的GPA是否为数字!”);
返回false;
}
返回true;
}
插入学员
更新学生信息
删除学生
输入Z编号:


输入GPA:


输入新的GPA:



这里有几个不同的问题。第一个问题是表单是使用GET方法提交的,但是您试图在PHP中检索您的值,就好像表单是使用POST方法提交的一样。由于您正在更新数据库中的值,因此最好使用POST方法,因此应将表单打开标记更改为:

<form method="post" action="index.php" onsubmit="return check();">
(我添加了
标记,因为这是一个很好的做法。它有助于提高可访问性,如果人们单击单选按钮的标签,它会将其视为单击了单选按钮本身。)

现在在PHP代码中:

$selection = $_POST['selection'];
现在,
$selection
将包含表示所选单选按钮的值,您可以执行适当的逻辑:

if ($selection == "ins") {
     // do work here
} elseif ($selection == "upd") {
     // do work here
}
// etc.

这里有几个不同的问题。第一个问题是表单是使用GET方法提交的,但是您试图在PHP中检索您的值,就好像表单是使用POST方法提交的一样。由于您正在更新数据库中的值,因此最好使用POST方法,因此应将表单打开标记更改为:

<form method="post" action="index.php" onsubmit="return check();">
(我添加了
标记,因为这是一个很好的做法。它有助于提高可访问性,如果人们单击单选按钮的标签,它会将其视为单击了单选按钮本身。)

现在在PHP代码中:

$selection = $_POST['selection'];
现在,
$selection
将包含表示所选单选按钮的值,您可以执行适当的逻辑:

if ($selection == "ins") {
     // do work here
} elseif ($selection == "upd") {
     // do work here
}
// etc.
至于你的问题(如果我没记错的话)-你应该将每个收音机的值设置为你计划在服务器端代码中识别它时使用的属性,例如:

<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 />
而且..我非常同意eggyal的评论,并建议您看看准备好的语句(以及一般的SQL注入)

祝你好运

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

<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 />
而且..我非常同意eggyal的评论,并建议您看看准备好的语句(以及一般的SQL注入)


祝你好运

您的代码易受SQL注入攻击。您确实应该使用准备好的语句,将变量作为参数传递到语句中,而这些参数不会对SQL进行求值。如果您不知道我在说什么,或者不知道如何进行,请阅读。您是否尝试过按照搜索Google时出现的任何教程进行操作?您的代码易受SQL注入攻击。您确实应该使用准备好的语句,将变量作为参数传递到语句中,而这些参数不会对SQL进行求值。如果你不知道我在说什么,或者怎么说,请阅读。你有没有尝试过在谷歌搜索时出现的任何教程?好的,我以前从未使用过label属性,所以这对我来说是新的。使用标签有什么用?看看这个:-它们使复选框更易使用和访问。好的,这是一篇很棒的文章和阅读。谢谢你。选中“更新”单选按钮时,我正在工作。它要求输入Z编号并输入新的gpa。如何更新用户在mysqlok中输入的znumber的gpa值,我以前从未使用过label属性,所以这对我来说是新的。使用标签有什么用?看看这个:-它们使复选框更易使用和访问。好的,这是一篇很棒的文章和阅读。谢谢你。选中“更新”单选按钮时,我正在工作。它要求输入znumber并输入