I'm trying to create a radio button in a loop, but I want to assign a unique id so it becomes a group where only one radio can be selected. Is this possible? My loop duplicates the same radio and I can select every button. I only want to select one.
<?php
$i = 0;
$i = $i++;
while($i>=0)
{
echo "<input type='radio' name='test[$i++]' value='test[$i++]'>test[$i++] ";
echo "<BR>";
$i++;
}
?>
UPDATE THIS WORKS!!!
<?php
$i = 0;
while($i++ < 5)
{
echo "<input type='radio' name='test' value='test[$i]'>test[$i] ";
}
?>
Learn basic PHP syntax. "$i++" is a string that contains a variable ($i), and two + characters. it's NOT going to increment your $i variable.
You're literally generating the following html:
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
<input type='radio' name='test[1++]' value='test[1++]'>test[1++]
As well, as written, your while() is going to be an infinite loop, since $i will ALWAYS be greater than 0.
Try this instead:
while($i++ < $limit) {
echo "<input type='radio' name='test[$i]' value='test[$i]'>test[$i] ";
}
you can use of bottom code.please preuse input tag(value).
`<?php for($i=1;$i<10;$i++) { `?>`
<input name="RadioBox" type="radio" value="<?php print($i); ?>" />
<?php } ?>
Related
Basically the code is supposed to be simple yet it is not working!
First page:
<?php
$i = 1;
$var;
while($i != 10)
{
$var="
<form id='upload' action='test2.php' method='POST'>
<input type='hidden' name='test' value='{$i}'>
<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div> ";
$i = $i+1;
echo $var;
}
?>
Second page:
<?php
echo $_POST['test'];
?>
when I run the code I always get the last value only (9) ... I need to have different value for each button .. can you help me?
You don't need multiple forms or hidden inputs to achieve this. You can just use buttons, and set their values to $i.
echo "<form id='upload' action='test2.php' method='POST'>";
for ($i = 0; $i < 10; $i++) {
echo "<button type='submit' name='test' value='$i'>Go to album</button>";
}
echo '</form>';
In test2.php, $_POST['test'] will have the $i value of the button you clicked.
Create single form with multiple input elements name as an array to get multiple value's in single input
Try this:
<input type='hidden' name='test[]' value='{$i}'>
Now you will receive an array of test as $_POST['test'] with different values
The problem with the other proposed solution is that you will have 10 forms, but you won't be able to submit all of the items at once. You will only be able to submit the value of one of them.
I believe you're trying to create 10 input elements instead of 10 separate forms:
<?php
$i = 1;
$var;
$var .= "<form id='upload' action='test2.php' method='POST'>"
while($i != 10)
{
$var .= "<input type='hidden' name='test[]' value='{$i}'>"
$i = $i+1;
}
$var .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div>"
echo $var
?>
Here's code that I would suggest instead of what you've got:
<?php
$html = "<form id='upload' action='test2.php' method='POST'>";
for ($i = 1; $i <= 10; $i++){
$html .= "<input type='hidden' name='test[]' value='{$i}'>";
}
$html .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>"
$html .= "</form>";
echo $html
?>
It's a little more work than what Mahesh's answer would require
First, a question: do you really want 10 forms - or do you want one form with 10 questions. Keep in mind that $_POST will only contain values from the form which was submitted (read: one form).
I think you want something like
<form id="upload" action="test2.php" method="POST">
<?php for ($i = 0; $i < 10; $i++) { ?>
<input name="test[]" type="hidden" value="<?=$i?>">
<?php } ?>
<button type="submit">submit</button>
</form>
edit: given your response below, why not use query parameters?
Go to Album <?=$i?>
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
I have this script where I want to post values from the form into my database. I want to post many text boxes into one form.
The problem is, only the last text box gets updated.
<form action='' method='post'>
<?php
for ($i=0; $i<$count; $i++) {
echo "<input type='text' name='".$i."' />
<input type='hidden' name='img_id' value='".$img_id."' />
<input type='hidden' name='loop' value='".$i."' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
//now i need help
$query = "UPDATE photos SET description = '".$_POST[$_POST['loop']]."' WHERE id = '".$img_id."'";
$res = mysql_query($query) or die(mysql_error);
}
?>
For example, loop goes 4 times, that means I have 4 text boxes.
In my script, which is not fully written here, the code updates only the last loop into the database.
First, you need to use the PHP array notation for the name attribute in your HTML. Otherwise you will have multiple inputs with the same name and only one of those will be posted
echo "<input type='text' name='".$i."' />
<input type='hidden' name='img_id[]' value='".$img_id."' />
<input type='hidden' name='loop[]' value='".$i."' />";
Now you can access those elements in your PHP as an array.
foreach ($_POST['img_id'] as $key=>$value) {
// img_id's value is in $value
// loop's value is in $_POST['loop'][$key]
}
Use the foreach to build your queries and after the loop execute all of them.
Your query is wide open to SQL injections. Use mysql_real_escape_string at least (even better: prepared statements, this will also be better performance wise since you always have the same query only different input). Also keep in mind that mysql_* functions are officially deprecated and hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SO for more information.
You need to make the names of your form elements unique so that they don't clash. One way would be to post them through as arrays instead:
Also, can't you use the same $count variable to loop over the potential results?
It looks like you're posting the hiddens with the name loop in order to pass through how many times over the loop you need to go. If not, you can post the loop variable through like this, but you use it in place of $count for your update loop. Also, I'd consider just adding that element in once, at the end - using the value of $count instead.
E.g. Something like this may work for you...
<form action='' method='post'>
<?php
for ($i=0; $i<$count; $i++) {
// How is img_id set here? You have no indication in your example - I'll assume that you've put them into an array...
echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
echo "<input type='text' name='name[".$i."]' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
for ($i=0; $i<$count; $i++) {
if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name' ] ) {
$query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][]."'";
$res = mysql_query($query) or die(mysql_error);
}
}
}
?>
Or, if you can't use $count:
<form action='' method='post'>
<?php
echo( "<input type='hidden' name='loop' value='".$count."' />" );
for ($i=0; $i<$count; $i++) {
// How is img_id set here? You have no indication in your example - I'll assume that you've put them into an array...
echo "<input type='hidden' name='img_id[\"".$i.\""]' value='".$img_id[$i]."' />";
echo "<input type='text' name='name[".$i."]' />";
}
?>
<input type='submit' value='Update' name='update' /></form>
<?php
if (isset($_POST['update'])) {
for ($i=0; $i<$_POST['loop']; $i++) {
if ( array_key_exists( 'name', $_POST ) && is_array( $_POST['name'] ) && array_key_exists( $i, $_POST['name'] ) {
$query = "UPDATE photos SET description = '".$_POST['name'][$i]."' WHERE id = '".$_POST['img_id'][$i]."'";
$res = mysql_query($query) or die(mysql_error);
}
}
}
?>
You have to use something like,
if (isset($_POST['update'])) {
for($i=0; $i<=$_POST['loop']; $i++){
i m trying to increment value 1 using while loop condition, i m developing quiz generation system, but stuck here and not found any solution, please help and tell what to do....
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$i++;
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech1; ?>'><?php echo $qech1."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech2; ?>'><?php echo $qech2."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech3; ?>'><?php echo $qech3."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech4; ?>'><?php echo $qech4."<br>"; ?><br>
<?php
}
?>
you seen above code, it is $i = 1; and then i echo $i as well in 4 fields of radio. it retrives around 3 questions from database and each question has 4 values, but all of the total of 12 values of 3 questions has same name that is 1, it is not increment using $i++ not sure why ?
here is out put
<span style='font-size:18px;font-weight:550px; '>english</span><br><input type='radio' name='1' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='jkj'>jkj<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<span style='font-size:18px;font-weight:550px; '>adnan </span><br><input type='radio' name='1' value='jhwjksahajsh'>jhwjksahajsh<br><br>
<input type='radio' name='2' value='kjhjhjh'>kjhjhjh<br><br>
<input type='radio' name='2' value='kjhkjjh'>kjhkjjh<br><br>
<input type='radio' name='2' value='hjkjhj'>hjkjhj<br><br>
<span style='font-size:18px;font-weight:550px; '>sdfsd</span><br><input type='radio' name='1' value='gfgf'>gfgf<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gf'>gf<br><br>
You can use a Heredoc string to help you with this. I made a simple example that will output what you want in a simple way. Take a look:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
echo <<<HTML
<input type='radio' name='$i' value='$qech1'>$qech1<br><br>
<input type='radio' name='$i' value='$qech2'>$qech2<br><br>
<input type='radio' name='$i' value='$qech3'>$qech3<br><br>
<input type='radio' name='$i' value='$qech4'>$qech4<br><br>
HTML;
$i++;
}
The reason it isn't incrementing is because you are calling $i right after incrementing it.
while ... {
$i++
...
..name="$i"...
}
Right now, it goes through the loop once and prints all names as 2. The next time all of them would be 3, etc.
What you want is more of a foreach loop.
I assigned basic values just for my testing purposes. I personally would eliminate assigning $quech1 = $rows[optiona]. You can just work with the array of $rows with the foreach loop.
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$correct=$rows['correct'];
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
You'll see from my inspect element, that you are now getting incremented names:
However, if I'm not mistaken. Don't you generally want all of the names to be the same for a group of radio buttons? You'll want unique ID's. And then have all buttons that are to be grouped together with the same name. This makes it so only one button in that group can be selected. If that is what you're trying to accomplish, use the revised code:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$name = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$name++;
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' id='<?php echo $i; ?>' name='<?php echo $name; ?>'value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
Only change is that $i is being assigned to id now. And we'll declare a $name counter as well. We'll increment that in the while loop, so that it will only be incremented once for each set of button options. And then you use $name in the name attribute for the button.
That's what makes sense to me. But without more information, we don't really know exactly what you're trying to accomplish. :)
I'm looping a question for the user with different numerical values for $i. They will input a quantity in the html form that is also looped. Upon them clicking submit, I would like to have an array (with the key ranging form [0] onwards) that stores their response to each particular variant of the question. However, with the code I have written, I only manage to store their last input with the key [0] as if it was the first element of the array. All the previous answers seem to be lost when I call print_r. Please, I would really appreciate it if anyone could point out why this is happening or how I could fix it.
<?php
for ($i=2; $i<=10; $i++)
{
print "question $i";
echo"<form action=\"mysqlranked.php\" method=\"post\">
<input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">
</form>
<br>";
}
print "
<form>
<input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
</form>";
if (isset($_POST["formSubmit"]))
{
$var = $_POST["pools"];
}
print_r($var);
?>
You had each of your inputs in a new form and the submit button in its own form as well. I fixed it for you:
<?php
echo "<form action=\"mysqlranked.php\" method=\"post\">";
for ($i=2; $i<=10; $i++)
{
print "question $i";
echo"
<input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">
<br>";
}
print "
<input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
";
echo "</form>";
if (isset($_POST["formSubmit"]))
{
$var = $_POST["pools"];
}
print_r($var);
?>
Let me know if it works.
You should not have a new form tag for every question.
Try the code below:
<form action="mysqlranked.php" method="post">
<?php
for ($i = 2; $i <= 10; $i++)
{
print "question $i";
?>
<input type="text" name="pools[]" value="0" maxlength="2" size="2">
<?php
}
?>
<input type="submit" name="formSubmit" value="Submit">
</form>
<?php
if (isset($_POST["formSubmit"]))
{
$var = $_POST["pools"];
}
print_r($var);
?>
<?php
for ($i=2; $i<=10; $i++)
{
print "question $i";
echo"<form action=\"mysqlranked.php\" method=\"post\">
<input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">
<br>";
}
print "
<input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
</form>";
if (isset($_POST["formSubmit"]))
{
for($counter=0;$counter<count($_POST["pools"]);$counter++)
{
$var = $_POST['pools'][$counter]
}
}
print_r($var);
?>
you may use array_push($arrayName, $elementToBePushed);