How to get access to dynamically generated radio buttons using POST - php

I have some dynamically generated HTML radio buttons based on fetching each character from a database table as show below:
<form name="form" id="myForm" method="POST" action="process.php">
<?php
$stmt = $this->registry->db->getDB()->prepare("SELECT * FROM characters");
$stmt->execute();
if($stmt->rowCount() > 0)
{
foreach($stmt as $row)
{
?>
<input type="radio" name="<?php print $row[0];?>" value="<?php print "value" . $row[1];?>">
<?php
}
}
?>
</form>
How would I get access to each selected radio button that is generated in my PHP script using $_POST[], because the name attribute of each radio button is created dynamically, I can't get my head around how I would access each radio button value in PHP so I can process the form.
Note that each radio button generated will be unique so they will not be grouped with the same name.

One easy way would be to create an array, in this case data[]:
<input type="radio" name="data[<?php print $row[0];?>]" value="<?php print $row[1];?>">
Then to get them:
if(isset($_POST['data'])) {
foreach($_POST['data'] as $name => $value) {
echo "$name = $value<br/>";
}
}

Related

PHP get forms data with one button when the form is inside a loop fetched from database

I have a query which fetches data from the database within a loop, for each instance of loop a form is created, now I want to get the value of each forms data within the loop itself and check if it matches with its corresponding row's column in the database:
for eg:
<?php
$marks = 0;
while ($row= mysqli_fetch_assoc($resut)) {
echo '<form method="POST" action="">
<div class="col-sm-8" id="question">'.$row['question'] .'</div>
<input type="radio" placeholder="" name="answer" value = "true" id=""> True
<input type="radio" placeholder="" name="answer" value = "false" id=""> false
</form>';
if (isset($_POST['save'])){ // this is the submit button present outisde the loop at last
if ($_POST['answer'] == $row['answer']) {
$marks++;
} else {
echo "incorrect";
}
}
}
?>
<form role="form" action="" method= "POST">
<button name ="save" id=" class="btn btn-primary">Save</button>
</form>
So what I am trying to achieve is that I have a column in database which I can get by $row['answer'] within the loop. so I could check the forms data with this row by if( $_POST['answer'] == $row['answer']) .. .do smth...But the problem is that I have to keep the submit button outside the loop since there should only be only 1 submit button and if I put it outside the loop it is not working.
My attempts:
I tried to keep the form tag before the loop. if I do this then suppose there are 10 rows in database I would be needing 10 forms and 10 values taken within the loop. but then if I press the radio button in one of the rows the other would be deselected. so in my opinion the form tag should be inside the whole loop only.
I tried keeping the form submit button (present at last) inside the while loop within every instance of rows fetched. then again it would echo every button for every row which is not the motive. I just want one button for all the forms to be submitted and get values accordingly.
also I cannot do this check outside or in any other page since I have to check with every instance of the loop i.e. $row['answer'] with every instance of forms radio button data i.e. $_POST['answer'].
Thank you for every response.
If you want to send all the answers at once, you'll need to use a single form.
To do that, you can change the name of your input so that it's different for each question.
$marks = 0;
$idx = 0;
echo '<form method="POST" action="">';
while ($row= mysqli_fetch_assoc($resut)) {
++$idx;
echo
'<div class="col-sm-8" id="question">'.$row['question'].'</div>
<input type="radio" placeholder="" name="answer'.$idx.'" value = "true" id=""> True
<input type="radio" placeholder="" name="answer'.$idx.'" value = "false" id=""> False';
if (isset($_POST['save'])) { // this is the submit button present
outisde the loop at last
if ($_POST['answer'] == $row['answer']) {
$marks++;
} else {
echo "incorrect";
}
}
}
echo '</form>';
You can also inject the $idx between brackets, which will give you a single $_POST['answer'] entry, but which will be an array instead of a string. That array will contain all your answers assignated to the keys you injected as $idx :
<input type="radio" placeholder="" name="answer['.$idx.']" value = "true" id=""> True
<input type="radio" placeholder="" name="answer['.$idx.']" value = "false" id=""> False

PHP get selected value from select box?

This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";

Adding users with hidden inputs using selection box in php

I'm trying to allow users to add other users on a form via a selection box. So, a user can select another user from a drop down selection, press add user, and they're added to an array. The user can select another user, and that second user should be added to this array. Right now, each time a user is added the original user disappears. I tried using hidden inputs but I'm not having any luck.
On formprocessing.php
//this generates a list of users. The code to actually generate the users is omitted
<center><h3>Invite Friends</h3></center><br>
<form method="post">
<tr><td>Friends:</td><td><select name ="friend"><Option value="">Friends<? echo $selection; ?></Select></td>
<input type="hidden" name="hiddenFriends" value="<? echo $friends; ?>"/>
<td><input type="submit" name="submitFriend" value="Add Friend"/>
</form>
On form.php
//i'm trying to create an array of users which are added. When completed, they'll be added to the db
<?
if(isset($_POST['submitFriend'])){
if(count($_POST['hiddenFriends']) > 0){
$friend = $_POST['friend'];
array_push($friends,$_POST['friend']);
}
else{
$friends = array('');
array_push($friends,$_POST['friend']);
}
}
?><input type="hidden" name="hiddenFriends" value="<? echo $friends; ?>"/><?
print_r($friends);
?>
Dude you trying to add PHP array into html input. Its IMPOSSIBLE.
but don't worry you can use explode and implode functions.
<?
$friends = array();
if(isset($_POST['submitFriend'])){
if(strlen($_POST['hiddenFriends']) > 0){
$friends = explode(',',$_POST['hiddenFriends']);
}
$friends[] = $_POST['friend'];
}
?>
<input type="hidden" name="hiddenFriends" value="<? echo implode(',',$friends); ?>"/>

Two forms with multiple submit buttons in single PHP file

I am trying to write a dynamic form using PHP. I'd like to have a single webpage that contains two forms:
The upper form allows to search for an element in the mysql database, e.g., for a name
The lower form shows the data that is associated with this name in the database
If I press on the "Search" button of the upper form, then the the lower form is shown and the text fields are filled with data from the database that belong to this name. If I change the user name to some other value and press again "Search", then the data that is associated with the new record is shown and so on.
The lower form also has a button "Update" which allows to transfer changes made to the text boxes (in the lower part) to the database.
Now, I have the following problem: In my script I set initially the value of name (from the upper form) to "". When I then press the "Search" button, then the lower part of the form is shown and the corresponding data is shown in the lower part. When I then press the "Update" button, then the text field associated with name is set to the empty string. This is because in my script I set initially name to the "". I'd like that in this case the data entered in the upper form is not changed, i.e., it stays the same.
I guess, I am missing something here. There is probably an easy solution for this and I am doing something fundamentally wrong. It'd be great if you could help me.
That's what I tried... I deleted lots of details, but I guess that can give you an idea what I am trying to do. Notice that the whole code is in the file update.php.
<?php
function search_bus($mysql, $name)
{
// do some stuff here...
}
function update_bus($mysql, $b_id)
{
// do some stuff here...
}
// some global variables
$b_id = 0;
$username = ""; // username of business
// get b_id that corresponds to username
if (isset($_REQUEST['search']))
{
$b_id =0; // business id
if (isset($_POST['user']))
{
$username = $_POST['user'];
$b_id = search_bus($mysql, $username);
}
}
elseif(isset($_REQUEST['update']))
{
update_bus($mysql, $b_id);
}
?>
<h2>Search:</h2>
<form name="search_bus" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="submit" value="Suchen" name="search"/>
</form>
<?php
if($b_id != 0)
{
?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<-- some form follows here -->
<?php
}
?>
I think what you're missing is to create a HTML Hidden field to keep the value of Name variable.
<input type="hidden" name="name" value="<?php print $nameVar ?>" />
Add this input to both forms so you can keep the value no matter what button the user clicks.
Hope this helps.
Adding code to verify the
<h2>Search:</h2>
<form name="search_bus" method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<input type="submit" value="Suchen" name="search"/>
</form>
<?php if($b_id != 0) { ?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];>">
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<-- some form follows here -->
<?php } ?>
Dont initialize $b_id if it already comes into the http request.
if (!isset($_POST['b_id']))
{
$b_id = 0;
}
else
{
$b_id = $_POST['b_id'];
}
This way you can alway remember the last selected value of b_id.
Hope this can help you.

Can I get the name of submit button in another form? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can I get the name of submit button in another form?
Hi,
I have a form which has 3 submit buttons. Their names are generated and assigned in a loop. Now if I use a post method, how can access the name of the submit button which was clicked.
The following is the example of my form:
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
?>
<button type="submit" name="<?php echo $i ?>" value="<?php echo $i ?>" >
</button>
<?php
}
?>
</form>
May be I can use onsubmit attribute for the button tag in one.php, but I am unable to get the output. Any suggestions?
You can examine the $_POST array to see which number was sent through.
How you do that is entirely up to you, it could be as basic as a few isset() checks.
if (isset($_POST['1'])) {
echo "Clicked button 1";
}
if (isset($_POST['2'])) {
echo "Clicked button 2";
}
if (isset($_POST['3'])) {
echo "Clicked button 3";
}
It would probably make more sense to use the same name and different values for multiple submit buttons.
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
echo "<input type='submit' name='submitButton' value='{$i}' />";
}
?>
</form>
In two.php you can then simply pull the value of the submit button that was pressed using $_POST['submitButton']

Categories