This question already has an answer here:
Code updated to reflect double quote vs single quote.
Instead of populating with the date_requested value from the mysql table, I am getting the following.
Date <input type="text" id="datepicker" name="date_requested" value="$date_requested"> <br/><br/>
This line is placing $date_requested in the text box.
I have also tried
Date <input type="text" id="datepicker" name="date_requested" value="<?php echo "$date_requested" ?>"/> <br/><br/>
This line places php echo in the text box.
Any direction is appreciated. Here is the surrounding section of code:
if($_SERVER['REQUEST_METHOD'] == "POST")
{
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
$query = mysql_query("Select * from time_off_requests Where time_off_key='$time_off_key'"); // SQL Query
$date_requested = $_POST["date_requested"];
$time_off_begin = $_POST["time_off_begin"];
$time_off_end = $_POST["time_off_end"];//date
$use_pto = $_POST["use_pto"];
$user = $_SESSION["user"];
$time_off_key = $_SESSION["time_off_key"];
mysql_query("UPDATE time_off_requests SET date_requested='$date_requested', time_off_begin='$time_off_begin', time_off_end='$time_off_end', use_pto='$use_pto' WHERE time_off_key='$time_off_key'") ;
header("location: home.php");
}
if($tok_exists)
{
Print '
<form action="edit.php" method="POST">
Date <input type="text" id="datepicker" name="date_requested" value="<?php echo "$date_requested" ?>"/> <br/><br/>
All Day <input type="checkbox" name="all_day[]" value="no" /> <br/>
Start Time <input type="time" id="timepicker" name="time_off_begin" /> <br/>
End Time <input type="time" id="timepicker2" name="time_off_end" /> <br/> <br/>
Use PTO <Select name="use_pto">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select> <br/><br/>
<input type ="submit" value="Edit Request">
</form>
';
}
else
{
Print '<h2 align="center">There is no data to be edited.</h2>';
}
?>
</div>
You are mixing single and double quotes wrong. An easier way to set the values into the string from the variables is using variable interpolation, which requires double quotes (""
).
So
Print "<form action='edit.php' method='POST'>
Date <input type='text' id='datepicker' name='date_requested' value='$date_requested'/> <br/><br/>
</form>";
would indeed output
<form action='edit.php' method='POST'>
Date <input type='text' id='datepicker' name='date_requested' value='2018-12-12'/> <br/><br/>
</form>
This is because PHP sees the variable $date_requested
inside a normal string enclosed within double quotes and replaces it with its value. The single quotes around the variable are interpreted literally and outputted as the quotes enclosing the HTML attribute's value