I am creating a form to update a database and used PHP to query the database and get current values before filling them in as the values already in the input boxes. I used echo to create the HTML elements while inserting the PHP variables however, the elements created aren't being displayed. Here are a couple of the inputs that I tried to make:
// Album Price
echo "<div class=\"row horizontal-center\">" .
"<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\"\n" .
"</div>";
// Album Genre
echo "<div class=\"row horizontal-center\">" .
"<input type=\"text\" value=\"" . htmlspecialchars($albumGenre) . "\" name=\"albumGenre\"\n" .
"</div>";
Thanks in advance :)
Your input elements are not properly closed with a > character. This code should work:
"<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\" />\n"
Note the /> added in the end of the line. (the / due to <input> not having a closing tag)
There's conflicting quotes in your HTML elements. Use single quotes for echo() and double quotes for HTML elements.
Also, you did not close the <input tag.
echo '<div class=\"row horizontal-center\">' .
'<input type=\"number\" step=\"0.01\" value=\"' . htmlspecialchars($albumPrice) . '\" name=\"albumPrice\" />\n' .
'</div>';
echo '<div class=\"row horizontal-center\">' .
'<input type=\"text\" value=\"' . htmlspecialchars($albumGenre) . '\" name=\"albumGenre\" />\n' .
'</div>';
Tip: You should turn on Error Reporting by adding this code to the top of your PHP files which will assist you in finding errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
Related
I have a form embedded in table that is created on the initial page load:
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols'>";
echo $resarr[$k]['typ'] . "</td></form>";
I submit the form when a cell in the table is clicked and want to pass the value of the cell that was clicked.
<script>
$(".hols").click(function() {
$("#showbooking").submit();
})
</script>
After the reload I am testing with this:
echo "<p> ID of clicked booking was: " . $_POST['clickedcalid']; . "</p>";
but the value is not being passed and I'm not sure why.
Possibly because the form is embedded in a table?
Firstly, the form element needs to go inside the td. Secondly, only the values of form elements are sent in a form submission, ie input, select, textarea etc. Try this:
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">';
echo '<form id="showbooking" action="calendar.php" method="post">';
echo '<input type="text" name="clickedcalid" value="' . $resarr[$k]['calid'] . '" />';
echo $resarr[$k]['typ'] . '</form></td>';
You can't put <form> inside a tr element, wrapping td. <tr> specification says
Permitted content: Zero or more <td> or <th> elements, or a mix of them
Also value attribute is not valid for td element. You need to restructure your markup.
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">' .
'<form id="showbooking" action="calendar.php" method="post">' .
'<input type="hidden" name="clickedcalid" value="' . $resarr[$k]['calid'] . '">' .
$resarr[$k]['typ'] .
'</form>' .
'</td>';
To get value from $_POST you need to have input tag
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td><input type='text' name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols' />";
echo $resarr[$k]['typ'] . "</td></form>";
Thanks,
Naumche
I have a select value 'post', when someone wants to change the status of the post from unsolved to solved they have to choose a post en than click 'change to solved'
The problem is when I click on the button, it doesn't change because PHP takes not the whole title of the post. So when my post is called 'Photoshop crashes', it only sends 'Photoshop'. That's why it doesn't update in my database, the query can't find the right post, when the title of the post is only 1 word, my table gets updated.
<?php
if (isset($_POST['btnSolved']))
{
// wanneer er op de knop geklikt is proberen we user te saven in de databank
if (!empty($_POST['btnSolved']))
{
$solved = $_POST['unsolved'];
$conn = new mysqli("localhost","root","root","PhpProject");
if ($conn -> connect_errno) {
throw new Exception("No connection with database!");
} else {
$sql = "UPDATE Posts SET status = 'Solved' WHERE post='".$solved."'";
}
}
$conn->query($sql);
}
?>
In my body:
<h3>Change status</h3>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<?php
if(mysqli_num_rows($showBugs) > 0)
{
echo "<select name= unsolved>";
while ($row = mysqli_fetch_assoc($showBugs))
{
echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";
}
echo "</select>";
}
?>
<br />
<input class="btn btn-info dropdown-toggle" type="submit" name="btnSolved" value="Change to solved" />
</form>
This is what I get when I do a print of the $sql
UPDATE Posts SET status = 'Solved' WHERE post='Photoshop'
Does someone know why PHP can post 1 word, but not the whole title? It might be something stupid, but I don't know how to fix this.
Your problem is a absence of the quotes in your html.
Fix this:
echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";
to this:
echo '<option value="' $row['subject'] . '">' . $row['subject'] . "</option>";
Try enclosing the value in single quotes,
echo "<option value='" . $row['subject'] . "'>" . $row['subject'] . "</option>";
The browser won't parse this properly if $row['subject'] comprises two words:
echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";
Your browser will read
<option value=two words>
as
<option value=two>
and not know what to do with words
use escaped double quotes
echo "<option value=\"" . $row['subject'] . "\">" . $row['subject'] . "</option>";
You need to add quotes for the value attribute. And if you have quotes in your value you need to pass $row['subject'] through htmlentities() like this:
echo '<option value="' . htmlentities($row['subject']) . '">' . htmlentities($row['subject']) . "</option>";
In this case htmlentities() for the label is not needed but it's good practice.
I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.
I am using the following code to fill the drop-down box and select a file and download it. Its working perfect.But i tried using every file to display as link and download it on clicking the link.
echo "<form id=\"form\" name=\"psform\" action=\"download_logic.php\" method=\"post\"><label>Select File: </label><select name=\"file\" >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id=\"submit\" type=\"submit\" name=\"filesubmit\" value=\"Download\" /> </form>";
its giving me errors ....any help please....
I am using following code:
while ($row = mysql_fetch_array($result)) {
echo "" . $row['fileshare'] . "";
}
Here is the error in the <a> tag*** Error (unexpected T_ENCAPSED_AND_WHITESPACE)*
this should work:
echo '' . $row['fileshare'] . '';
I'm really sorry I can't tell you why it happens, but if you change all of the \" to ' it should work for you.
echo "<form id='form' name='psform' action='download_logic.php' method='post'><label>Select File: </label><select name='file' >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id='submit' type='submit' name='filesubmit' value='Download' /> </form>";
As for the next part, you can't add a variable to a string just by writing
"variable: . $take['variable']"
If you wish to add variables to string close the writen part of the string and then add the variable, like this:
"variable: ". $take['variable']
Here is how it sould look like:
while ($row = mysql_fetch_array($result)) {
echo "<a href='download_logic.php?f=". $row['location'] . $row['fileshare'] ."'>" . $row['fileshare'] . "</a>";
}
I have "n" number of rows being returned by a sql statement. Works great. For every row returned, I would like the user to pick an option (either option 'a' or option 'b')
As standard practice a while loop is used to iterate through each row, and I attempt to name my options by the row number. However, I run into problems when I attempt to get the value of the radio button the user has selected.
I can't seem to dynamically get the value of the radio button named 1 in the first loop (or any loop with it's appropriate name) of the where clause.
Code snippet here:
$counter = 1;
while($row = mysql_fetch_array($result))
{
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $counter . ">";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['d_id'] ."> ";
echo "<br>";
echo "<input type='hidden' name='choice' value=" . $_GET["m" . $counter] . " >";
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
$counter ++;
}
updatepics.php:
Game
<?php
echo $_POST['game'];
echo " " . $_POST['player'] . " picks: ";
echo " " . $_POST['choice'] ;
?>
output of updatepics.php...
Game 1 granny picks:
Notice 'choice' is null
According to the HTML4 specification, names attributes are defined as CDATA and CDATA is defined as follows:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Modify your code to have all name values start with at least one letter.
I bailed out from dynamically creating a radio button name. Now all radio buttons have the same name through each iteration of the loop.
It seems to work passing the correct value at the correct time even if you select all of the buttons are set before hitting the submit button for each iteration.
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='mw' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $row['f_id'] . ">";
echo "<input type='hidden' name='player' value='" . $user . "' >";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='mw' value= ". $row['d_id'] ."> ";
echo " " . $row['line'];
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
updatepics.php is just as simple, and it works:
$_POST['mw']