Reputation: 183
this is my latest problem. I've got the layout and everything working, but now i am trying to figure out the php side of this part. what i want to do is how do i specifically say which radio button is checked using php? i tried using $_POST['ins'] (where 'ins' is the id for the insert button) but that didn't work. i want it to work out as when the insert button is checked, do whatever steps, but i don't know how to get it so say that the insert button is being selected/checked.
here is my code
<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>
Upvotes: 0
Views: 902
Reputation: 727
As for your question (if I got you right) - you should set the value attribute of each radio to the one that you're planning to use when identifying it in your server-side code, for example:
<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 />
And on the server-side, the value of the radio that was chosen will be accessible as a POST parameter by the name of the group, in your case:
$_POST['group']
And..I strongly agree with eggyal's comment and recommend you to take a look at prepared statements (and SQL injections in general).
Good luck!
Upvotes: 3
Reputation: 539
There are a couple of different problems here. The first one is that your form is being submitted using the GET method, but you're trying to retrieve your values in PHP as if it had been submitted with the POST method. Since you're updating values in a database, the POST method is preferable, so your form opening tag should be changed to this:
<form method="post" action="index.php" onsubmit="return check();">
The "name" field of an input is what ends up in your $_POST
variable, and the value of that variable is the value of whichever input was selected.
So you should change your inputs to look like this:
<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 />
(I added the <label>
markup because it's a good practice to include that. It helps with accessibility, and if people click on the label of your radio button, it will treat it as having clicked the radio button itself.)
Now in your PHP code:
$selection = $_POST['selection'];
Now $selection
will contain the value representing which radio button was selected, and you can perform the appropriate logic:
if ($selection == "ins") {
// do work here
} elseif ($selection == "upd") {
// do work here
}
// etc.
Upvotes: 2