I have a php page which is reading a text file and printing the file line by line. and also I have given 'yes' 'no' option with every line.Now I want to print those lines in the 2nd page which is selected as yes.If user select yes then I want to print those line to next page.I have the below code.Can any one check this code?
<?php
$i=0;
$lines = file("text.txt");
foreach($lines as $line) {
$select_val=$line.'-'.$i;
print " <br> ";
print " <br> ";
echo($line);
echo '<select name=".$select_val.">';
echo '<option value="Yes">Yes</option>';
echo '<option value="No">No</option>';
echo '</select>';
print " <br> ";
print " <br> ";
$i++;
}
?>
<input type="hidden" name="ival" value="$i" /> <?
print "<input type=submit value=Submit><input type=reset>";
?>
2nd page:
<?php
$i=0;
$lines = file("text.txt");
foreach($lines as $line) {
$select_val=$line.'-'.$i;
$a=$_POST[$select_val];
if ($a == 'Yes') {
echo($line);
}
$i++;
}
?>
The code would almost achieve what you're trying to do, but a 2-value selection may be better achieved with a radio selection instead of a drop-down. This makes better usability since a radio click is achieved with one action vs. a drop-down select which requires two actions.
echo '<input type="radio" name="' . $select_val . '" id="'
. $select_val . 'yes" value="Yes" /><label for="'
. $select_val . '1"> Yes</label> ';
echo '<input type="radio" name="' . $select_val . '" id="'
. $select_val . 'no" value="No" /><label for="'
. $select_val .'no"> No</label> ';
Your original code has some formatting error. If you're going to use single quotes for your echo statements, you have to concatenate the variable with the single quotations to return to PHP parsing like this (recommended method):
echo '<select name="' . $select_val . '">';
or use double quotations like this (while escaping the double quotations inside of the select tag):
echo "<select name=\"$select_val\">";
Otherwise the select tag will show up as (using your original code)
<select name=".$select_val.">
instead of
<select name="line-0">
Examine the HTML output of your PHP script and you'll see this.
Related
I have PHP code creating multiple forms on a single page and the submission of any of these forms should trigger an API call based on which form was submitted.
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
// $listOfApples is a list of 10 apples each with a unique ID
foreach ($listOfApples as $apple) {
echo '<form method="post" action="mypage.php">';
echo '<input type="hidden" name="appleId" value="' . $apple->{"id"} . '">';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
No matter which of the form buttons I click, the value in $_POST['appleId'] is the ID of the first apple in the list. I don't have much experience with PHP or HTML forms, is my approach completely off?
I am seeing some small mistakes as
echo '<input type="hidden" name="appleId" value"' . $apple->{"id"} . '">';
You should fix it,instead of foreach yor can use for loop to display forms,
as I haven't $listofapples,I used 10,you can query for row numbers than place row numbers value as $listofapples,then echo your unique id to input value,
Finally it's working as you wished
Check it & let me know if it's the perfect fit for you
<?php
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
$listOfApples = 10;
// $listOfApples is a list of 10 apples each with a unique ID
$sn=0;
for ($i = 1; $i <= $listOfApples; $i++) {
echo '<form method="post" action="hy.php">';
echo '<input type="hidden" name="appleId" value=" ' .++$sn. '"/>';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
?>
I have selected data from mysql and inserted them into a radio button form which is looped as shown:
if($num_rows) {
echo '<form name="fixtureform" method="POST" action="index.php">';
while ($row = mysql_fetch_assoc($result)) {
echo $row["home_team"] . " vs " . $row["away_team"]. '<br />' . " Home" . '<input type="radio" name="win'.$row["home_id"].'" value="'.$row["home_id"].'"/>' ." Draw" . '<input type="radio" name="'.$row["home_id"].$row["away_id"].'" value="draw"/>' ." Away" . '<input type="radio" name="win'.$row["away_id"].'" value="' . $row["away_id"] . '"/>' .'<br />';
}
echo '<input type="Submit" Name="Submitacc" Value="Submit your teams">';
echo '</form>';
} else {
echo "There are no fixtures today ";
}
I then have an if statement for submitted values:
if($_POST['Submitacc']=='Submit your teams') {
}
How would I say if the selected name of the radio button is win.$row[home_id] - insert value into mysql table. I'm struggling with the fact I can't get $row[home_id] outside of the loop?
thanks in advance
Try as below
$i=0;
while ($row = mysql_fetch_assoc($result)) {
echo $row["home_team"] . " vs " . $row["away_team"]. '<br />' . " Home" . '<input type="radio" name="result['.$i.']" value="h_'.$row["home_id"].'"/>' ." Draw" . '<input type="radio" name="result['.$i.']" value="draw"/>' ." Away" . '<input type="radio" name="result['.$i.']" value="a_' . $row["away_id"] . '"/>' .'<br />';
$i++;
}
In Your PHP
foreach($_POST['result'] as $value){
if($value == 'draw') {
//do stuff
}
else {
$apart = explode('_',$value);
if($apart[0]=='h'){
//home team win
echo $apart[1];
}
else {
//away team win
echo $apart[1];
}
}
}
use this
echo"<input name='win' type='radio' value='".$row['home_id']."' />";
Because your radios have different names they are all separate and all being submitted, I recommend naming them all "result" and giving them values of "win", "draw" and "loose" respectively.
<label>Win: <input type="radio" name="result" value="win" /></label>
<label>Draw: <input type="radio" name="result" value="draw" /></label>
<label>Loose: <input type="radio" name="result" value="loose" /></label>
<input type="hidden" name="home_team" value="<?php echo $row['home_team']; ?>" />
<input type="hidden" name="away_team" value="<?php echo $row['away_team']; ?>" />
You can access the value with $_POST['result'] and since you know the home team and away team you can handle the data accordingly
i think a diferent usage of the radio button mechanic shuold be used in this case.
in your example you are creating 3 diferent instances of a radio button for each row, instead create one instance of a radio button set with diferent values depending on the answer given
like:
<input type="radio" name="game'.$row["game_id"].'" value="'.$row["home_id"].'">
<input type="radio" name="game'.$row["game_id"].'" value="draw">
<input type="radio" name="game'.$row["game_id"].'" value="'.$row["away_id"].'">
this means that the variable $_POST["game1"] will have the value of the winner or draw that was selected for game which is game_id 1
hope this helps you
Change your element name to win.home without the id, since you will get it by the value.
Then go like:
if(isset($_POST['win.home'])) { }
Edit:
If you want to do it like this, try this:
3 radio buttons, named win.home, draw, win.away plus a unique id (mysql id?) like win.home.1234 then you can use as many games as you want. You can get the id back with explode() later.
Then the values:
1st: $row["home_id"] . "-" . $row["away_id"]
2nd: draw
3rd: $row["away_id"] . "-" . $row["home_id"]
Then when processing the form:
Split the value with explode() at the -. So the first id is always the winner.
Hello Coders,
I have a dynamically created table in PHP and for each row there is a radio button. The intention is that depending upon which radio box is selected, the user can perform multiple functions like, Delete, Amend etc. I can make this all work with hard coded "Value" for the radio button like
echo "<td>" . '<input type="radio" name="radioSelect" value="2" checked="checked" />' . "</td>";
What I am looking for is a way to set the "2" dynamically. For example:
while($row = mysql_fetch_array($result) and $i<=100)
{
echo "<tr>";
$sno= $row['SNo'];
echo "<td>" . '<input type="radio" name="radioSelect" value= $sno checked="checked" />' . "</td>";
}
What is the syntax for this? Is this possible at all?
Thanks for your help.
Yes, it's called concatenation, and you're already doing it with the . operator after "<td>", for example.
echo "<td>" . '<input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" />' . "</td>";
I'm not sure why you put it that way, though.
echo '<td><input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" /></td>';
Also, note that only one radio button with the same name can be checked at a time, so you might want to rethink what you're doing there.
You can do like this ? if you are having a problem in CONCATENATION.
<?php
while($row = mysql_fetch_assoc($result) and $i<=100)
{ $sno= $row['SNo'];?>
<table>
<tr>
<td><input type="radio" name="radioSelect" value= <?php echo $sno; ?> checked="checked" /> <?php } ?></td>
</tr>
</table>
i'm trying to select values from the database and place each and every one on a separate text input field. my code works properly, however, when i tried to display a string on a text field, the string is cut or trimmed on its very first space character. for example:
value #1: "my-picture.jpg"
value #2: "my name"
if i were to place the two values above and insert them inside a text field, the output would be like this:
value #1: my-picture.jpg
value #2: my
this is the code i'm working on:
<?php
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo "
<input type='text' value=".$newArray['my_picture'].">
<input type='text' value=".$newArray['my_name'].">
";
?>
What is wrong? Thanks for any help.
You need to quote your values in your HTML form:
echo '
<input type="text" value="'.$newArray['my_picture'].'">
<input type="text" value="'.$newArray['my_name'].'">
';
Also for good measure it would be a good idea to use htmlentities() or htmlspecialchars() on the values in your variables to encode special characters:
echo '<input type="text" value="' . htmlentities($newArray['my_picture']) . '">
<input type="text" value="' . htmlentities($newArray['my_name']) . '">';
Try viewing the source.. That says:
<input type='text' value=the content of your variable>
Where 'the content of your variable' needs to be surrounded by quotes, naturally. So; change it into:
<?php
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo '<input type="text" value="' . $newArray['my_picture'] . '">';
echo '<input type="text" value="' . $newArray['my_name'] . '">';
}
?>
You need to surround your value with quotes. Something like
echo "
<input type='text' value='".$newArray['my_picture']."'>
<input type='text' value='".$newArray['my_name']."'>
";
So this is part of my code :
echo "<form name='whatever' action='next.php' method='get'>";
while($row = mysql_fetch_assoc($qsq))
{
echo"<input type='checkbox' name='choice[]' value='" . $row['question_id'] . "' /> ". `$row['question_text'] . '<br />';`
}
echo"<br>";
echo "<input type='submit' value='submit' /></form>";
What should i do in the next.php ? I'm thinking to put the selected info in an array and display it. Then store the selected results in a table.But i am not sure with the codes.I am beginner in php can someone help me with the coding ?Thanks in advance!
First of all I cleaned up your code a little bit. (Using single quoutes around HTML trributes is uncanny).
echo ('<form name="whatever" action="next.php" method="get">');
while ($row = mysql_fetch_assoc ($qsq)) {
echo ('<input type="checkbox" name="choice[' . $row["question_id"] . ']" value="1" /> ' . $row["question_text"] . '<br />';
}
echo '<br />';
echo '<input type="submit" value="submit" /></form>';
Then you can iterate thru the values with a foreach loop in next.php.
echo ('<ul>');
foreach ($_GET["choice"] as $key => $value){
echo ('<li>' . $key . ' is ticked</li>');
}
echo ('</ul>');
Note that the array's keys hold the real information here,values wil only contain the number 1.
Take a look ath the source of resulting HTML. This is not theonly possible solution but good for learning purposes.