Here is how I am echoing back a 'normal' table cell in PHP:
Code:
echo "<tr>";
echo "<td>Some cell data</td>";
echo "</tr>\n";
Attempt to echo a button within a cell:
echo "<tr>";
echo "<td><form action="insertdata.php?page=add" method="post">
Username: <input type="text" name="username" >
<input type="submit" >
</form>
</td>";
echo "</tr>\n";
Thank you for any help, this is quite confusing to me. I am a beginner when it comes to PHP so please pardon any errors and feel free to kindly correct me wherever a correction is needed!
You need to either escape your double quotes " that appear inside your string surrounded by double quotes, or use a different quote style (i.e. single quotes ') to surround the string that contains the double quotes. It's actually made quite obvious what the problem is by the syntax highlighting on this site.
You could either do this:
echo "<tr>";
echo "<td><form action=\"insertdata.php?page=add\" method=\"post\">
Username: <input type=\"text\" name=\"username\" >
<input type=\"submit\" >
</form>
</td>";
echo "</tr>\n";
...or this...
echo "<tr>";
echo '<td><form action="insertdata.php?page=add" method="post">
Username: <input type="text" name="username" >
<input type="submit" >
</form>
</td>';
echo "</tr>\n";
I suggest you read this thoroughly so you no what you can and can't legally do with strings in PHP.
As a side note, it is a bad idea to put new-line literals inside quoted strings, as it can lead to cross-platform compatibility wierdness. It's not really a problem with web output like this, but it's a bad habit to get into. You should either use \r,\n and \r\n as appropriate, or if you must put literals into the source code, use heredoc syntax. Remember that escape sequences like \r\n are only interpolated in double quoted strings!!!
Related
<?php
if(isset($_POST['btnLogin'])){
$myVariable = $_POST['fieldParameter'];
if(condition){
//do something
}else{
echo "
<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='<?php echo $myVariable; ?>'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>
";
}
}
?>
Notice that the variable $myVariable is contained in the main IF block. I'm trying to send the value of $myVariable to submit.php as hidden field.
Also, i enclosed all the html tags using one echo statement with double quotes.
I found related questions here in SO but can't find similar to embedding php within a long echo of html tags
I tried to put value='<?php echo $studentNo; ?>' with no success.
I want to access it in a submit.php file like this,
submit.php
<?php
$aVariable = $_POST['myVariable'];
echo $aVariable;
?>
How can I pass the value contained in $myVariable as hidden field? Is there something wrong with the way I use double and single quotes?
If you are already echoing a string you shouldn't put <?php echo "" ?> inside it again. You should concatenate your string instead. But in your case you don't even need to do that, because you're using double quotes for echoing which means you can simply just write your variable in it.
echo "<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='$myVariable;'/>
<br/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>
</form>";
If you were using single quotes for your echo, it would look like this:
echo '<form method="POST" action="submit.php"><br/>
<input type="hidden" name="myVariable" value="' . $myVariable . '"/><br/>
<input type="submit" name="btnSubmit" id="submit" value="Submit">
</form>';
You just need to type $myVariable instead of in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the
You can try these variants (simplified):
// code before
echo "<input type='hidden' name='myVariable' value='$myVariable'/>";
// code after
// OR //
// code before
?>
<input type='hidden' name='myVariable' value='<?= $myVariable ?>'/>
<?php
// code after
Note that the quotes you use in HTML don't affect PHP, as long as you escape them properly (use \" and \' where appropriate).
Okay, so typically I would write the following:
<input type='text' class='form-control' name='name' value='<?=$user['name'];?>'>
However, because I am using ' in my HTML, and if the name has a ' in it, (i.e. the last name is O'Brian for instance) It doesn't echo correctly, because the value is ending the input abruptly.
Of course a simple solution is to use " quotation marks with my html, but that doesn't help - because what about when I want to echo quotation marks as well? What can I do?
Use <input type='text' class='form-control' name='name' value='<?php echo htmlentities($user['name'], ENT_QUOTES); ?>'>
I have read a lot of question here but I couldn't get anything to work. I have such a instructions:
$query = "SELECT nazwa,rok_prod,wypornosc FROM statek where id_statek=$id";
$wynik = pg_query($query);
$liczba_kolumn = pg_num_fields($wynik);
echo "<form action=edos.php method=post>";
echo "<table border width=1>";
for($k = 0;$k<$liczba_kolumn; $k++)
{
echo "<tr>";
echo "<td>";
echo pg_field_name($wynik,$k);
echo "</td>";
echo "<td>";
echo "<input type=text name=".pg_field_name($wynik,$k) "value=".pg_fetch_result($wynik,0,$k).">";
echo "</td>";
echo "</tr>";
}
echo "</table>";
And I want to display values from SELECT in fields, that I could change it later - it is for editing form. I have tried in a lot of ways:
<input type="text" name="name" value="<?php echo $name;?>" />
or
<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />
but nothing is working. I have tested pg_fetch_result($wynik,0,$k) and there is what I want but how to display it and make it editable?
First, I recommend you to read about PHP Strings, especially the difference between single and double quoted strings and how to escape characters.
Second, you're forgetting to add the double quotes around the HTML attribute values, forgot a dot and am missing a space. This line:
echo "<input type=text name=".pg_field_name($wynik,$k) "value=".pg_fetch_result($wynik,0,$k).">";
Would output something like <input type=text name=foovalue=bar>, assuming that your first function call will return foo and the second returns bar.
What you need is, for example:
echo "<input type=\"text\" name=\"".pg_field_name($wynik,$k)."\" value=\"".pg_fetch_result($wynik,0,$k)."\">";
Which should output <input type="text" name="foo" value="bar">
As this tends to get messy and is therefore error-prone, I would recommend you to look into parsing strings with functions like printf.
Apart from that, I am guessing that your functions pf_fetch_name and pg_fetch_result are not returning anything (or an empty string), and therefore you get empty input fields. Hence, the error might lie within these function and/or the SQL queries they are (probably?) carrying out. This is what you should look into.
Edit:
To get things a bit tidier, I would further recommend to avoid all the echos. This can be done by simply having the actual HTML markup outside of the <?php ?> tags and then injecting your values with the shorthand tags <?= ?>. A shortened example:
<?php
// Some PHP code
for ($i = 1; $i <= $something; ++$i) {
?>
<table>
<tr>
<td><?= fetch_foo(); ?></td>
<td><?= fetch_bar(); ?></td>
</tr>
<table>
<?php
} // closing the for loop
?>
I've been hours trying to figure out how to solve one thing, first I had this that works:
echo "<p>$valor[nombre_categoria]
<input type='button' value='modifica'
onclick='location.href=\"mod_cat.php?categ=\""
,'</p>\n";
And then I tried to send with the link a variable but I can't figure it out how to use the quotes, double quotes and backslashes.
echo "<p>$valor[nombre_categoria]<input type='button' value='modifica'
onclick='location.href=\"mod_cat.php?categ=".
$valor[nombre_categoria]."\'</p>\n";
I'm sure the solution its easy but I cant figure it out thanks for reading
This works for me:
$valor['nombre_categoria'] = "hello";
echo "<p>{$valor['nombre_categoria']}
<input type='button' value='modifica'
onclick=\"location.href='mod_cat.php?categ={$valor['nombre_categoria']}'\"></p>\n";
or this:
echo "<p>".$valor['nombre_categoria']."
<input type='button' value='modifica'
onclick=\"location.href='mod_cat.php?categ=".
$valor['nombre_categoria']."'\"></p>\n";
outputs: (line break added for readability here)
<input type="button" value="modifica"
onclick="location.href='mod_cat.php?categ=hello'">
When you use double quotes, you have to use { and } :
echo "Hello {$foo['bar']}";
You can skip { and } if your variable is "simple" :
echo "Hello $foo";
In my opinion, it's always better to use concat :
echo 'Hello '.$foo;
Regards
I highly recommend you break your variables out a quote your array elements
echo '<p>' . $valor['nombre_categoria'] . '<input type="button" value="modifica" onclick="location.href=\'mod_cat.php?categ=' . $valor['nombre_categoria'] . '\'"/></p>' . "\n";
Easier to read
I pulling text that is stored in my database using MySQL select so it is displayed in a form so it can be edited.
When the form is set to the string held in the database gets truncated at the first space so "Foo Bar" would be displayed as "Foo". This doesn't happen when using the tag.
I have made sure that the text field is big enough to hold the entire string and the number of charters isn't limited.
In the database the whole word is stored and no truncation is happening. I set the type to varchar(30) which is enough space to store the whole word. I have also tried changing the type to text and I still have the problem.
I can't seam to find a solution anywhere does anyone have an idea of why this may be happening?
<?php echo "<input type=\"text\" name=\"title[" . $row["id"] . "]\" value=" . $row["title"] . ">"; ?>
You should provide a sample of the HTML produced from your php page. Most likely, your value string is not being quoted, e.g. your HTML form looks like:
<input type=text value=Foo Bar>
instead of
<input type=text value="Foo Bar">
This would produce exactly the effect you are seeing
UPDATE: Based on your example in the comment, you are indeed missing the quotes:
Old code with the problem (missing quotes around value attribute's value):
<?php echo "<input type=\"text\" name=\"title[" . $row["id"] . "]\"
value=" . $row["title"] . ">"; ?>
Fixed code:
<?php echo "<input type=\"text\" name=\"title[" . $row["id"] . "]\"
value=\"" . $row["title"] . "\">"; ?>
Note that you already had the name attribute correctly quoted, but value was not.
Without the relevant code, this is just a guess, but are you setting your form input value attributes without using quotes?
For example, are you doing this?
<input type="text" value=<? echo $value; ?> name="formInput" />
Instead of the correct syntax, which is this?
<input type="text" value="<? echo $value; ?>" name="formInput" />