Radio Buttons Submit passes wrong value - php

I have a few Radio Buttons which shell send their value in the post method, when the submit button is clicked. I am pretty sure it is easy, but for any reason it does not work. It sends the calue of the submit button instead. Please help me. This is my code for now(this code is inside a PHP script):
echo "<form action=\"nutzerverwaltung.php\" method=\"post\">";
echo "<table [...]";
while ($row = $alluser->fetch_assoc())
{
echo "<tr>
HERE---> <td><input type=\"radio\" name=\"select_to_delete\" value=".$row["id"]."></td>
<td>".ucwords(strtolower(str_replace(".", " ", $row["username"])))."</td>
<td>".$row["username"]."#via-ev.de</td>
<td>".$row["permissionlevel"]."</td>
</tr>";
}
echo "</table><br />";
echo "<input type=\"submit\" name=\"select_to_delete\" class=\"nv_button\" />
</form>";

Putting my comments to an answer, since it was clearly the issue. As posted 17 mins. prior to my answer.
Both your radio and submit form elements hold the same name attribute.
I would call that a collision/conflict. Rename one, mainly your submit button.
You can have radio buttons holding the same name attribute as an array, but not the submit button. Elements of the same "group" can have the same name attribute.

your submit input have the same name attribute just remove that
echo "<input type=\"submit\" class=\"nv_button\" />
in html forms if you have inputs with same name the last one value will be send as your value

Related

use an isset result inside another one

I have a problem when developping a page in php, when click on a submit button another submit button. appear.
my problem appeared at the isset of this submit button, echo $b doesn't work; here is my code:
<form method='POST'>
<input type="submit" name="s1">
</form>
</body>
</html>
<?php
if(isset($_POST["s1"])){
$b=2;
echo "<form method='POST'><input type='submit' name='s2'></form>";
if(isset($_POST["s2"])){
echo $b;
}
}
I've already tried to make $b a global variable, but no change :(
Thank's for your help.
The flow of your code is first send the value of input s2, if this was set with some value echo your second form and set b variable to 2, when your second form is printed out on the browser the b value will have lost, so your must store this value in some place, this could be done through a hidden field in your second form like this:
echo "<form><input type='text' name='s2'><input type='hidden' name='b' value='$b'></form>"
if(isset($_POST["s2"])){
echo $_POST['b'];
}
Hope this helps you!

How to check which radio button a user checked if not in a form

I have a textfile with information that I am using to create a quiz. I am only displaying one quiz question at a time however. I am generating radio buttons based on the number of questions and then I have a submit button after the loop that generates the radio buttons. I can't figure out how to get which radio button the user selected in the $_POST array when I click submit. My initial thought is to use a form tag and then have the loop run but I don't know if this works or how to make it work syntactically.
textfile (the last number is the index of the right answer):
What does charmander evolve to?#Charmeleon:charizard:squirtle#0
WHo is the main character in Pokemon?#Misty:Ash:Obama#1
Script:
<?php
$indexTemp = intVal($_SESSION["index"]);
if($_SESSION["numQuestions"] == $indexTemp){
echo "Your Results are: ";
echo "<form action=\"process.php\" method=\"post\"> Back to Main screen: <input type=\"submit\"><br \> </form>";
exit();
}
$filename = $_SESSION["quizOfTheDay"];
$quizStuff = file($filename);
$ctr =1;
$questionInfo = $quizStuff[$indexTemp];
$questionParse = explode("#", $questionInfo);
$_SESSION["correctAns"] = $questionParse[2];
echo $_SESSION["correctAns"]." from line 55 <br />";
$answerChoices = explode(":",$questionParse[1]);
echo "$questionParse[0] ? <br />";
#This is where the radio buttons are being generated
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans$ctr' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}
$_SESSION["index"] = $indexTemp +1;
echo "<form action=\"questions.php\" method=\"post\"> <input type=\"submit\"><br \> </form>";
?>
Obtaining data from radio buttons and checkboxes can be a little tricky, generally due to a lack of understanding of how radio buttons and checkboxes work.
It is important to remember two facts:
Checkbox "Names" Must Be Unique to Each Checkbox
Radio Button "Names" Must Be Identical For Each Group of Buttons
Update the foreach since you must have same name for all the radio buttons but with different values.
<?php
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value=".$ctr."> <label for='q1'>".$answerChoice."</label> <br />";
$ctr +=1;
}
?>
Now your for-each concatenation also looks wrong and i have updated it and since the concatenation is wrong the value will not be displayed in the radio button. The radio button value will be the count of the increment variable.
remove $ctr
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}

PHP - check if multiple radios are selected and then go to other php

I have a form containing 36 radio button selection sections. These are being generated from an SQL query:
//sql query
<form method="post" action="next.php">
<table border=1><tr><td>ID</td>selection</td></tr>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["ID"]. "</td><td><input type=\"radio\" name=\"".$row["ID"]."\" value=\"1\">1<input type=\"radio\" name=\"".$row["ID"]."\" value=\"X\">X<input type=\"radio\" name=\"".$row["ID"]."\" value=\"2\">2</td></tr>";
}
?>
<input type="submit" name="submit" value="Submit"></table></form>
So, the name of each radio section is the ID from the query. When the user clicks on the Submit button I need all the radios are set (loop through them). If not, then I want to show an error but keeping the user on the same page without loosing all the selections he made already. If all radios are set, then go to next.php where I will process the stuff from $_POST.
As mentioned by #putvande in the comments, the problem can be solved by putting a required="required" attribute on the radio inputs.

Send column name from button

I have made a php page. On which I am displaying table 'prod' from my data base.
each row is displayed nicely. Today i tried to add a button named 'rate' at the end of each row of my table. which I did successfully. Now I want to send the the value of the first column of that row to another php page when that button is clicked. I am stuck that how to do so? can you help please ??
I know i have to use the method post in my form and i have to use $_post[that value] on the other php page to inculcate the value for further function.
I just need to ask that where to add the value of my first column in the button line. so that onclick it can send that value. I hope I am clear over this. Thank You very much for help :)
<?php
include("connection.php");
$query = "select * from prod";
$res = oci_parse($conn,$query);
usleep(100);
if (oci_execute($res)){
usleep(100);
print "<TABLE border \"1\">";
$first = 0;
while ($row = #oci_fetch_assoc($res)){
if (!$first){
$first = 1;
print "<TR><TH>";
print implode("</TH><TH>",array_keys($row));
print "</TH></TR>\n";
}
print "<TR><TD>";
print #implode("</TD><TD>",array_values($row));
print "</TD></TR>\n";
echo "<td><form action='detailform.php' method='POST'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
}
print "</TABLE>";
}
?>
you have to add inputs in your form whatever kind u prefer
echo "<td>
<form action='detailform.php' method='POST'>
<input type='hidden' name='your_val_key' value='".$row[your_val_key_in_query]."'> <!-- input hidden, change to text 4 debug -->
<input type='submit' name='submit-btn' value='Rate'/>
</form>
</td></tr>";
and than, in your detailform.php u can get the val with
echo $_POST["your_val_key"];
if u are not sure how much data u send or somthing, try this and u get the full data:
echo "<pre>".print_r($_POST,true)."</pre>";
BTW: why are u mixing print and echo?
Use hidden input
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='col-name' value='you-col-value'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";

Detect click on submit button in PHP

I have a php file that contains a form (which contains 2 input boxes and a submit button) for updating a contact. I managed to fill the fields with the contact's data, but I can't detect if the submit button is clicked
form looks like this
echo "<form action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";
this code should output a "clicked" message if the button is clicked
if (isset($_POST['UpdateContact']))
{
echo "<p>clicked";
}
else
{
echo "<p>not clicked";
}
can anyone help me out or tell me what i've done wrong
(I want from the same php file to fill the contact's data in a from and to update the database)
The default method for a form is GET, so either set the form's method attribute to "post", or change your $_POST in PHP to $_GET.
echo "<form method=post action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";
or
if (isset($_GET['UpdateContact']))
{
echo "<p>clicked";
}
else
{
echo "<p>not clicked";
}
are you sure your code is reaching the php file. If yes, then the first think to check is the request type.. using $_SERVER['REQUEST_METHOD']
it will give POST in case the button was click and GET in case it the page was accessed using URL like.. google.com/Contact.php.
also, u might ned to add METHOD ="POST" in the first line of form action "form action=Contact.php

Categories