Forum Moderators: coopster

Message Too Old, No Replies

PHP Echo Form With Variable From MySql

         

jbearnolimits

7:19 am on Sep 13, 2022 (gmt 0)

Top Contributors Of The Month



I'm having trouble with the syntax here. I'm trying to make a form to update the first name of a certain row in the database. I have no problem when I echo the name. But I can't seem to populate the form with the name already in it. Here is the relevant part of the code:

if ($_SERVER["REQUEST_METHOD"] == "GET") {
// collect value of input field
$id = $_GET['id'];
}

$sql = "SELECT * FROM `Contacts` WHERE id = $id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "

<form action='../CreateNew.php' method='post'>
<label for='First Name'>First name:</label><br>
<input type='text' id='First Name' name='FirstName' value='.$row["FirstName"].' ><br><br>
<input type='submit' value='Submit'>
</form>


How do I make the box have the first name already filled in?

topr8

10:31 am on Sep 13, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



do you need to put it in quotes perhaps ...


<input type='text' id='First Name' name='FirstName' value=\"'.$row["FirstName"].' \"><br><br>

jbearnolimits

12:22 pm on Sep 13, 2022 (gmt 0)

Top Contributors Of The Month



When doing that I get:

Parse error: syntax error, unexpected '"', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

Dimitri

12:36 pm on Sep 13, 2022 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



echo '<input type="text" id="First Name" name="FirstName" value="' . $row["FirstName"] . '">';


ps: assuming you are sure FirstName does not contain special characters, best practice would be to escape the value before inserting it, into the page. htmlspecialchars (or htmlentities if you are mixing different character encodings, which is bad practice).

jbearnolimits

1:10 pm on Sep 13, 2022 (gmt 0)

Top Contributors Of The Month



I managed to get it. For those that want to know the proper way was:

<label for=\"First Name\">First name:</label><br>
<input type=\"text\" id=\"First Name\" name=\"FirstName\" value=\"" . $row["FirstName"]. "\"><br>

mack

2:59 am on Sep 14, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Glad you were able to solve this. The \ prevents the following " from closing the echo.

another thing you can do is end the PHP and place the HTML for the form

?>
<form action='../CreateNew.php' method='post'>
<label for='First Name'>First name:</label><br>
<input type='text' id='First Name' name='FirstName' value='<?php echo"$omething";?> ><br><br>
<input type='submit' value='Submit'>
</form>

then restart php (if needed)
<?php

Mack.

jbearnolimits

1:44 am on Sep 19, 2022 (gmt 0)

Top Contributors Of The Month



Would you believe I was a webmaster that was working for a large corporation about a decade ago? This is what happens when you don't practice your craft. I'm having to relearn EVERYTHING. Right now I'm trying to figure out how to properly use htmlspecialchars in the form. I can find info everywhere talking about <form action="<php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>. But I don't remember how the ($_SERVER["PHP_SELF"]) part works if you are sending the input to a different page. I made another post for it here...but if a moderator wants to delete it and just have this thread be more of a relearning thread for me to ask the questions please feel free.