Okay my code is not broken, however it can be improved. I have a form element inside a table, which, according to this post here is illegal, but for me it is working to some extent. I am using PHP and the situation is as follows:
Inside that form I have two elements one is comment
and another is order_date
; when I inspect the form in chrome developer tools it shows the correct structure, so the point in the earlier linked post is not happening here. When the form gets submitted, I can never get the values of these fields from the post array unless I initialize them first, I am initializing them within a $_SESSION
, what is weird is that when I initialize them using $_POST
it also does not work. What is causing this error? Thanks
code for initializing them:
if(empty($_SESSION['comment'])) {
$_SESSION['comment']='';
}
if(empty($_SESSION['order_date'])) {
$_SESSION['order_date']='';
}
code for getting their value after form is submitted:
if(isset($_POST['comment']) && isset($_POST['order_date'])) {
$_SESSION['comment'] = nl2br(filter_var($_POST['comment'],FILTER_SANITIZE_STRING));
$_SESSION['order_date'] = mysqli_real_escape_string($con,$_POST['order_date']);
}
Trying to get their value in:
if(isset($_POST["place_order"])) {
$comment = $_POST['comment'];
}
The above code doesn't work
This is the table row of the form:
<tr>
<td colspan="5" align="center">
<form method="POST" action="cart.php" class="form-group">
<label for="comment">Order comments </label>
<textarea name="comment" id="comment" class="form-control"
placeholder="Please enter any special instructions for the order" required>
<?php if (isset($_SESSION['comment'])) {
echo $_SESSION['comment'];
} ?>
</textarea> <br>
<label for="order_date">Delivery date</label>
<input type="date" class="form-control" style="max-width: 20%;"
name="order_date" id="date" required></input> <br />
<input type="submit" name="place_order" id="place_order"
class="btn logo-green" value="Place Order" />
</form>
</td>
</tr>
Thank you