我想让我的单选按钮在我的php表单中有粘性?
我的表单有一些单选按钮,我希望使它们具有粘性,因此当用户在出现错误后返回表单再次填充时,他之前输入的值也应该是单选按钮的值。粘滞表单适用于所有其他输入,但不适用于单选按钮 html代码我想让我的单选按钮在我的php表单中有粘性?,php,html,visual-studio-code,Php,Html,Visual Studio Code,我的表单有一些单选按钮,我希望使它们具有粘性,因此当用户在出现错误后返回表单再次填充时,他之前输入的值也应该是单选按钮的值。粘滞表单适用于所有其他输入,但不适用于单选按钮 html代码 <div class='modal fade' id='staticBackdrop' data-bs-backdrop='static' data-bs-keyboard='false' tabindex='-1' aria-labelledby='staticBackdropLabel' aria-h
<div class='modal fade' id='staticBackdrop' data-bs-backdrop='static' data-bs-keyboard='false' tabindex='-1' aria-labelledby='staticBackdropLabel' aria-hidden='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<form action='Add.php' method='POST' class='form'>
<h2>Car Registration Form</h2>
<div class='mb-3'>
<input type='radio' name='Car' id='Toyota' value='$Models[Toyota]' />
<label for='Toyota' class='form-label'>Toyota</label>
<select name='Models[Toyota]' class='form-control'>
<option value='>None</option>
<option value='Camri'>Camri</option>
<option value='Corolla'>Corolla</option>
<option value='Estima'>Estima</option>
</select><br>
<input type='radio' name='Car' id='Nissan' value='$Models[Nissan]' />
<label for='Nissan' class='form-label'>Nissan</label>
<select name='Models[Nissan]' class='form-control'>
<option value='>None</option>
<option value='Micra'>Micra</option>
<option value='roller'>roller</option>
<option value='fushe'>fushe</option>
</select><br>
<input type='radio' name='Car' id='Mercedez' value='$Models[Mercedez]' />
<label for='Mercedez' value='$Models[Mercedez]' class='form-label'>Mercedez</label>
<select name='Models[Mercedez]' class='form-control'>
<option value='>None</option>
<option value='5series'>5series</option>
<option value='x7'>x7</option>
<option value='x5'>x5</option>
</select><br>
<label for='vehicleidentificationnumber' class='form-label'>vehicle identification number (VIN)</label>
<input type='text' name='vehicleidentificationnumber' value='$vehicleidentificationnumber' class='form-control' maxlength=20 required>
<label for='ManufacturingYear' class='form-label'>Manufacturing Year</label>
<input type='text' name='ManufacturingYear' value='$ManufacturingYear' class='form-control' maxlength=20 required>
<label for='Enginesize' class='form-label'>Engine size</label>
<input type='text' name='Enginesize' value='$Enginesize' class='form-control' maxlength=20 required>
<label for='TransmissionType' class='form-label'>TransmissionType</label><br>
<input type='radio' name='TransmissionType' id='TransmissionType' value='$Automatic' />
<label for='Automatic' name='Automatic' class='form-label'>Automatic</label><br>
<input type='radio' name='TransmissionType' id='TransmissionType' value='$Manual' />
<label for='Manual' class='form-label'>Manual</label><br>
<input type='radio' name='TransmissionType' id='TransmissionType' value='$Semi-Automatic' />
<label for='Semi-Automatic' class='form-label'>Semi-Automatic</label><br>
<label for='NoofSeats' class='form-label'>No. of Seats</label>
<input type='number' name='NoofSeats' value='$NoofSeats' class='form-control' maxlength=20 required>
<label for='Noofdoors' class='form-label'>No. of doors</label>
<input type='number' name='Noofdoors' value='$Noofdoors' class='form-control' maxlength=20 required>
<label for='Fueltype' class='form-label'>Fuel type</label>
<input type='text' name='Fueltype' value='$Fueltype' class='form-control' maxlength=20 required>
<label for='Colour' class='form-label'>Colour</label>
<input type='text' name='Colour' value='$Colour' class='form-control' maxlength=20 required>
<label for='RegistrationNumber' class='form-label'>Registration Number (use Dublin registration) </label>
<input type='text' name='RegistrationNumber' value='$RegistrationNumber' class='form-control' maxlength=20 required>
<label for='Dateoffirstregistration' class='form-label'>Date of first registration</label>
<input type='date' name='Dateoffirstregistration' value='$Dateoffirstregistration' class='form-control' maxlength=20 required>
</div>
<button type='button' class='btn btn-secondary' data-bs-dismiss='modal'>Close</button>
<button type='submit' name='insertdata' class='btn btn-primary'>Add Car</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div> ";
汽车登记表
丰田
没有一个
米卡拉
滚筒
福舍
梅塞德斯
你说的粘性是什么意思?通常粘性是指屏幕上的某个位置保持不变(例如页脚或边栏)通过CSS
-你的意思是让他们的选择保持不变吗?所以我的意思是粘性表单,当用户在表单中输入详细信息时,其中一个输入错误,其余输入正确,因此表单显示错误,用户之前输入的值应该在显示错误后显示出来。回答这个问题吗你的问题?
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // checking method
$errors = array(); // creating an errors array to record errors if there are any.
if (empty($_POST['vehicleidentificationnumber'])) {
$errors[] = 'vehicle identification number is required.';
} else {
// trim function removes whitespace and other predefined characters from both sides of a string.
$vehicleidentificationnumber = trim($_POST['vehicleidentificationnumber']);
// checking for the valid pattern from user
if (!preg_match('/^[a-zA-Z1-9]{17}$/', $vehicleidentificationnumber)) {
$errors[] = 'Invalid vehicle identification number! use only letters and numbers';
}
}
// check if the ManufacturingYear is provided and is valid
if (empty($_POST['ManufacturingYear'])) {
$errors[] = 'Manufacturing Year is required.';
} else {
$ManufacturingYear = trim($_POST['ManufacturingYear']);
if (!preg_match('/^[0-9]{4}$/', $ManufacturingYear)) {
$errors[] = 'Invalid Manufacturing Year! use only year format yyyy';
}
}
// check if the Enginesize is provided and is valid
if (empty($_POST['Enginesize'])) {
$errors[] = 'Enginesize is required.';
} else {
$Enginesize = trim($_POST['Enginesize']);
if (!preg_match('/^[1-9.L]*$/', $Enginesize)) {
$errors[] = 'Invalid Enginesize! use only letters/numbers.';
}
}
// check if the Transmissiontype is provided and is valid
if (empty($_POST['TransmissionType'])) {
$errors[] = 'Transmission type is required.';
} else {
$TransmissionType = trim($_POST['TransmissionType']);
if (!preg_match('/^[a-zA-Z]*$/', $TransmissionType)) {
$errors[] = 'Invalid Transmission type! use only letters.';
}
}
// check if the NoofSeats is provided and is valid
if (empty($_POST['NoofSeats'])) {
$errors[] = 'NoofSeats is required.';
} else {
$NoofSeats = trim($_POST['NoofSeats']);
if (!preg_match('/^[1-9]*$/', $NoofSeats)) {
$errors[] = 'Invalid NoofSeats! use only numbers';
}
}
// check if the Noofdoors is provided and is valid
if (empty($_POST['Noofdoors'])) {
$errors[] = 'Noofdoors is required.';
} else {
$Noofdoors = trim($_POST['Noofdoors']);
if (!preg_match('/^[1-9]*$/', $Noofdoors)) {
$errors[] = 'Invalid Noofdoors! use only numbers.';
}
}
// check if the Fueltype is provided and is valid
if (empty($_POST['Fueltype'])) {
$errors[] = 'Fueltype is required.';
} else {
$Fueltype = trim($_POST['Fueltype']);
if (!preg_match('/^[a-zA-Z]*$/', $Fueltype)) {
$errors[] = 'Invalid Fueltype! use only letters.';
}
}
// check if the Colour is provided and is valid
if (empty($_POST['Colour'])) {
$errors[] = 'Colour is required.';
} else {
$Colour = trim($_POST['Colour']);
if (!preg_match('/^[a-zA-Z]*$/', $Colour)) {
$errors[] = 'Invalid Colour! use only letters.';
}
}
// check if the RegistrationNumber is provided and is valid
if (empty($_POST['RegistrationNumber'])) {
$errors[] = 'RegistrationNumber is required.';
} else {
$RegistrationNumber = trim($_POST['RegistrationNumber']);
if (!preg_match('/^[0-9]{3}-[D]{1}-[0-9]{5}$/', $RegistrationNumber)) {
$errors[] = 'Invalid RegistrationNumber! use only following format 131-D-12345';
}
}
// check if the Dateoffirstregistration is provided and is valid
if (empty($_POST['Dateoffirstregistration'])) {
$errors[] = 'Dateoffirstregistration is required.';
} else {
$Dateoffirstregistration = trim($_POST['Dateoffirstregistration']);
if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $Dateoffirstregistration)) {
$errors[] = 'Invalid Dateoffirstregistration! use only date format dd/mm/yyyy';
}
}
if (empty($errors)) {
require('/home/s3022041/sqlC/dbConnect.php');
$Car = $_POST['Car'];
$Models = $_POST['Models'][$Car];
$vehicleidentificationnumber = $_POST['vehicleidentificationnumber'];
$ManufacturingYear = $_POST['ManufacturingYear'];
$Enginesize = $_POST['Enginesize'];
$TransmissionType = $_POST['TransmissionType'];
$NoofSeats = $_POST['NoofSeats'];
$Noofdoors = $_POST['Noofdoors'];
$Fueltype = $_POST['Fueltype'];
$Colour = $_POST['Colour'];
$RegistrationNumber = $_POST['RegistrationNumber'];
$Dateoffirstregistration = $_POST['Dateoffirstregistration'];
require('/home/s3022041/sqlC/dbConnect.php');
$query = "INSERT INTO cars (make, model, VIN, Manufacture_Year, Engine_Size, Transmission_Type, NoOfSeats, NoOfDoors, Fuel_Type, Colour, Registration_Number, DateOfRegestration)
VALUES ('$Car','$Models', '$vehicleidentificationnumber','$ManufacturingYear','$Enginesize','$TransmissionType','$NoofSeats','$Noofdoors','$Fueltype','$Colour','$RegistrationNumber','$Dateoffirstregistration');";
$query_run = mysqli_query($connection, $query);
if ($query_run) {
echo "<h2>Added successfully</h2>";
echo "<a href='index.php'>home</a>";
} else {
echo 'Error! ' . mysqli_error($connection);
}
mysqli_close($db_connection); // close the database connection
} else {
echo "<h3 class='errorH'>Error! <br>The following error(s) occurred:</h3>";
foreach ($errors as $msg) {
echo "<div class='error'>$msg </div>";
}