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']."'>
";
Related
I have a form where you need to be able to select the mentor, and add a name. The selecting a mentor is working fine, apart from one part: It only shows the last name ('achternaam') and it needs to show the first name ('voornaam') too. What did I do wrong?
<form action="index.php" method="post">
<p>
<label for="mentor">Mentor:</label>
<select name="mentor" id="mentor">
<?php
if ($result->num_rows > 0) {
//output data of each row
while ($mentors = $result->fetch_assoc()) {
echo '<option value="' . $$mentors['voornaam'] . '">' . $mentors['achternaam'] . '</option>';
}
} else {
echo "0 results";
}
?>
</select> <br>
<label for="klasnaam">Klasnaam:</label>
<input type="text" id="klasnaam" name="klasnaam"> <br>
<input type="hidden" name="action" value="addClass" />
<input type="submit" value="Submit">
<?php
?>
</p>
</form>
The <option> element displays what is written between the opening and closing tag. In your current version you only write the last name between the tags.
To fix this, add the first name:
echo '<option value="' . $mentors['voornaam'] . '">' . $mentors['voornaam'] . ' ' . $mentors['achternaam'] . '</option>';
The value attribute is not used for displaying. It is the representation of the data when submitting the form.
Note that I also changed $$mentors['voornaam'] to $mentors['voornaam']. There was a $ too much.
This question already has answers here:
How to set HTML value attribute (with spaces)
(6 answers)
Closed 11 months ago.
In input type text i am getting just one string. for ex if in database name field have "abc xyz". So its only showing abc not full name this happen for all the fields. Please check my code also.
while($row = mysqli_fetch_array($result))
{
//print_r($row['name']);
echo "<tr>";
echo "<td> ". $row['id'] . "</td>";
echo "<td> <input type=text placeholder = name onchange= nameValidation(this) value = ".$row["name"]." disabled> </td>";
echo "<td> <input type='email' placeholder = 'email' value = ". $row['email'] ." onchange= nameValidation(this) disabled> </td>";
echo '<td> <input type="button" value="edit" onclick="edit(this)">
<input type="button" value="update" onclick="update(this)" style="display:none;">
<input type="button" value="Delete" onclick="delete1(this)" style="display:none;">
</td>';
echo "</tr>";
}
echo "</table>";
The HTML specification says:
Attributes are placed inside the start tag, and consist of a name and a value, separated by an "=" character. The attribute value can remain unquoted if it doesn't contain ASCII whitespace or any of " ' ` = < or >. Otherwise, it has to be quoted using either single or double quotes. The value, along with the "=" character, can be omitted altogether if the value is the empty string.
In this case, the text "abc xyz" does contain a white space and must be quoted. But it is not in your code. After PHP is done with parsing, your input tag will be:
<input type=text placeholder = name onchange= nameValidation(this) value = abc xyz disabled>
Where, "abc" will be interpreted as value and "xyz" will be interpreted an another unknown attribute.
To rectify this, you need to quote the value with \" as follows:
echo "<td> <input type=text placeholder=name onchange=nameValidation(this) value=\"" . $row["name"] . "\" disabled> </td>";
Or you can switch to single quotes:
echo '<td>';
echo '<input type="text" placeholder="Enter your name" onchange="nameValidation(this)" value="' . $row['name'] . '" disabled>';
echo '</td>';
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.
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.
So i have these radio buttons in XHTML that I want to put into a PHP function to generate and I can't get it to work.
In XHTML it looks like this and is working;
<p><input type="radio" value="<? echo blabla; ?>" name="radioA" checked="checked" /></p>
<input type="hidden" value="<? echo $value; ?>" name="hiddenA[]" />
In PHP I need to set the "radioA" and "hiddenA" to variables respectively "radioB"/"hiddenB", "radioC"/"hiddenC" and so on for my code to work. This is what I have so far but it is not working. The first radio name is a string, but the second one is array. Thanks in advance.
function radio($Radio, $Array) {
echo '<p><input type="radio" value="$value>" name="$Radio" />', $value, '</p>';
echo '<input type="hidden" value="$value" name="$Array" />';
}
I guess what I'm trying to do is to return the name of the variable as a string. $_POST['hiddenA'] ===> hiddenA[]
Basic php syntax:
$a = 'hello';
echo '$a'; // outputs the literal characters $ and a
echo "$a"; // outputs "hello"
You have a few syntax issues here:
function radio($Radio, $Array) {
echo '<p><input type="radio" value="$Value>" name="$Radio" />', $Value, '</p>';
echo '<input type="hidden" value="$Value" name="$Array" />';
}
You have to use "" not '' when embedding variables:
echo "My name is {$name}.";
In PHP the . character is used to append strings not ,
echo "My name is " . $name . " and I am cool";
If you try to echo an array you will get the word Array not the array itself. Instead you can echo a value in the array:
echo "$array[0]";
You are trying to use a variable that is not defined in your example: $Value. This would give you an VariableUndefined exception.
It is generally bad practice to use capital letters at the start of variable names in PHP. So $Array should be $array.
With these in mind you code should look something like:
function radio($radio, $array) {
$value1 = $array[0];
$value2 = $array[1];
echo "<p><input type=\"radio\" value=\"$value1>\" name=\"$radio\" />$value1</p>";
echo "<input type=\"hidden\" value=\"$value2\" name=\"$value2\" />";
}
From the sounds of your example you need a for loop somewhere.