html and php form populate dropdown list with value user already provide in form if error happens during form submission
Get the solution ↓↓↓sorry if the title is bit confusing.i have html form with dropdown and text field and what i want is if user submit the form with errors. an error message will be displayed. Any fields already completed will be left unchanged, allowing the user to simply adjust her input and re-submit the form without having to enter all of the data again,i have manage to do with text field but i having hard time with dropdown list. here is my html code
<form method="POST" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h4>Choose Type of product</h4>
<div class="row">
<div class="col">
<div class="form-group">
<label >Select type of Product: </label>
<select id="slct3" class="browser-default custom
select" name="Productytype"
onchange="populate1(this.id,'slct4')" >
<span class="error"><?php
echo $ProductytypeErr;?></span>
<option value="">Select</option>
<option value="Import">Import </option>
<option value="Export"></option>
</select>
<span class="error"><?php echo $ProductytypeErr;?>
</span>
</div>
</div>
<div class="col">
<div class="form-group">
<label>Choose Document Type: </label>
<select id="slct4"
class="browser-default custom-select"
name="Documenttype" value="<?php echo
htmlspecialchars($Documenttype);?>"></select>
</div>
<span class="error"><?php echo
$Documenttype;?></span>
</div>
</div>
<h4>Exporter Detail</h4>
<div class="row">
<div class="col">
<div class="form-group">
<label>Name</label>
<input type="text" name="ExportName" class="form-control" value="<?=htmlspecialchars($ExportName);?>">
<span class="error" style="color:red"><?php echo $ExportNameErr;?></span>
</div>
</div>
</div>
</form>
it's long form but that is the main part
here is my php code
<?php
// variable for storing user input
$Productytype = $Documenttype = $ExportName = $ExportEmail = "";
// variable for storing error messages
$ProductytypeErr = $DocumenttypeErr = $ExportNameErr =
$ExportEmailErr="";
$ProductytypeErr = $DocumenttypeErr = $ExportNameErr =
$ExportEmailErr="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Productytype"])) {
$ProductytypeErr = "Missing";
} else {
$Productytype = $_POST["Productytype"];
}
if (isset($_REQUEST['Documenttype']) &&
$_REQUEST['Documenttype'] == '0') {
$DocumenttypeErr = "Missing";
} else {
$DocumenttypeErr = $_POST["DocumenttypeErr"];
}
if (empty($_POST["ExportName"])) {
$ExportNameErr = "Please Enter name of the exporter";
} else {
$ExportName = $_POST["ExportName"];
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: please make sure the php redis extension is installed and enabled.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.