PHP-How to get values from multiples dynamic radio button - php

i´m doing a little survey and i have the next problem.
I get the data from database and the name of the answer is the id of the question(question 1 id=1, question 2 id=2...)
<div id='box'>
<form action='#' method='POST'>
<?php
foreach($lista as $list){
echo "<p>".$list->getQuestion()."</p>";
$idtemp=$list->getid(); //first get question
$arr=$list->getQuestion($idtemp);
foreach ($arr as $ar) {
//second, get and draw the radio with the answers per question
echo "<input type='radio' value='".$ar[0]."' name='".$idtemp."'>".$ar[1]."</input><br>";
}
}
?>
<button type="button" >Cancel</button>
<button type='submit' value='end' name='end'>end</button>
<?php
if (isset($_POST['end'])) {
echo "<span>selected :<b> ".$_POST['radio']."</b></span>";
}
?>
</form>
</div>
I only wants to get the radios checked and sending to database,

Here, you need to use below line
echo "<input type='radio' value='".$ar[0]."' name='answer[".$temp."]'>".$ar[1]."</input><br>";
instead of this
echo "<input type='radio' value='".$ar[0]."' name='".$temp."'>".$ar[1]."</input><br>";
Then use
<?php
if (isset($_POST['answer'])) {
foreach($_POST['answer'] as $key=>$value){
echo "<span>selected :<b> ".$key." ".$value."</b></span>";
}
}
?>

Related

Wrong value of input number returned

I'm doing this assignment where I have to do website. I created a input number for the quantity of the product with a button all this in a form. When I try to get the quantity by putting in the URL. The problem is I keep getting the wrong quantity. Not the quantity that I had in my input.
<?php
foreach ($productList as $product){
?>
<form action="index.php" method="post">
<?php echo "<ul class='row'>"?>
<?php echo "<li>"?>
<?php echo "<img src ='".$product->getImage()."'class=image'>" ?>
<?php echo "<div class='overlay'>"?>
<?php echo '<div class="text">'.$product->__toString()."</div>"?>
<?php echo "</div>"?>
<?php
echo"<p id ='titleQte'>Quantity </p>";
echo"<div class='qteProduct'>";
echo "<input type='number' name='qte' value='1' min = '1'/>";
echo"</div>";
echo"<a href=index.php?action=addCartProduct&id=".$product->getId().'&quantity='.((isset($_POST['qte'])) && (isset($_POST['btnAjout'])) ? $_POST['qte']:1).'>'."<button type='button' name='btnAdd' id='add' class='btn btn-success'>Add to Cart</button></a>";
?>
<?php echo "</li>"?>
<?php echo "</ul>"?>
</form>
<?php
}
?>
It gives me 1 instead of the actual value of the input.
Some help would be appreciated.
Thank you.
You are passing the value in the tag as value = '1' so this takes precedence over the actual input. Remove it and pass
echo "<input type='number' name='qte' min = '1'/>";
Hope this helps !
Just remove the value attribute from this line:
echo "<input type='number' name='qte' value='1' min = '1'/>";
into
echo "<input type='number' name='qte' min = '1'/>";
I hope this would be helpful.

On submit i need to pass anchor tag value in php

while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo

How can names in an echoed html tags be used

I have a code like this abc.php
I want to create an array of check boxes but if I try accessing the name or the id, it doesn't seem to work.Is there any alternative way to do this?
<html>
<body>
<form method="post">
<?php
echo ""."<input type='checkbox' name='check[]' id='check[]' />"
$c=0;
if(isset($_POST['check[$c]'])){
echo "checked";
}
?>
</form>
</body>
</html>
You have $_POST['check[$c]'] in single quotes, it will not be replaced with the value of $c. Not to mention that multidimensional arrays do not work this way.
To check that $_POST variable treat it as an array:
$checked = $_POST['check'];
// $checked[0] - first element
// $checked[1] - second etc
Use this as a base code, also you can split the php code from the html.
The foreach is better because now you know how what values the user actually submitted in a fast and easy way.
<?php
if(isset($_POST['submit'])){
if(isset($_POST['check']) && is_array($_POST['check'])){
foreach($_POST['check'] as $key => $value){
echo "$key => $value<br>"; # wrap this around single quotes ', see the difference.
}
}
}
?>
<html>
<body>
<form method="post">
<input type='checkbox' name='check[]' value="a">a</input>
<input type='checkbox' name='check[]' value="b">b</input>
<input type='checkbox' name='check[]' value="c">c</input>
<input type='checkbox' name='check[]' value="d">d</input>
<input type='submit' name='submit' id='submit' />
</form>
</body>
</html>
No need to use arra, simply use variable like if(isset($_POST['check'])). If you want to display the value of checkbox then use $_POST['check'][0].
<form method="post">
<?php
echo ""."<input type='checkbox' name='check[]' id='check[]' value='test' />";
echo "<input type='submit' />";
if(isset($_POST['check'])){
echo "checked";
}
?>
</form>

Show value taken from database as a radio button

I have created a database and retrieved some values and show them in my view as a text. I want to show these values as radio buttons. How can i do this?
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo $rec->ans1."<br/>";
echo $rec->ans2."<br/>";
echo $rec->ans3."<br/>";
echo $rec->ans4."<br/>";
}
?>
Like this i have shown my values in database as texts. How can i show these like radio buttons?
Thank you.
Try this
<?php
foreach ($records as $rec) {
?>
<input type="radio" name="" value="<?php echo $rec['ans'] ?>"><?php echo $rec['ans'] ?>
<?php
}
?>
Question should come in another array
You can do this as follows:
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans1_".$rec->id."'>".$rec->ans1."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans2_".$rec->id."'>".$rec->ans2."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans3_".$rec->id."'>".$rec->ans3."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans4_".$rec->id."'>".$rec->ans4."<br/>";
}
?>

Store textbox values in php array

So basically I have the user enter a number on my first screen.
Here is test1.php which generates the number of text boxes that the user had previously entered. That number is $input
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='Names'>";
}
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
Then my test2.php should print all the values entered, but it only prints out the last value entered from test1.php. For example is $input is 4, only the text entered in the 4th text box prints, which is understandable as I don't know how to print all values.
$names=$_POST['Names'];
foreach($number as $num){
echo $num;
}
Is the problem with the name I gave to the textboxes, or something else?
Any help is much appreciated.
Just create a name grouping attribute, so that you'll get an array of inputs instead of just one:
<input type='text' name='Names[]'>
// ^ this is important
Sidenote:
I don't know if this is a typo, but this should be $names instead of $number:
$names = $_POST['Names'];
foreach($names as $num){
echo $num . '<br/>';
}
Sample Demo
Your problem is that you give the same name to all of your input (in your test1.php) so when you try to restore them on your test2.php, your $_POST['Names'] just takes the last input with this name.
A solution is to give a different name to all of your input
In yout first file use it :
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='".$i."'>";
}
echo "<input type='hidden' name='input' value='".$input."'>";
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
And in your 2nd file :
for($i=1; $i<=$_POST['input']; $i++){
echo $_POST['$i'];
}
<form method="post" name="myform">
<input type="text" name="array[]" Value="101"/>
<input type="text" name="array[]" Value="102"/>
<input type="text" name="array[]" Value="103"/>
<input type="text" name="array[]" Value="104"/>
<input type="submit" name="submit" Value="submit"/>
</form>
if(isset($_POST['submit'])){
foreach($_POST['array'] as $myarray) {
echo $myarray.'<br>';
}
OUTPUT
101
102
103
104

Categories