配送情報見出しでフォーム情報を表示しようとしていますが、名前、連絡先などの確認が必要です。表示できますが、確認できません。空白の場合は、支払いを除いて空白の値が表示され、出荷方法はデフォルト値が選択されて表示され、もう1つはエラーが表示されません。 fnameが必要です、私は少し混乱しています。空白のフォームを投稿しても、それを再度form2.phpページにリダイレクトするということを誰かが追加できますか?PHPでエラーを検証して表示することができません
は、これは私のform2.phpページ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="welcome2.php" method="post">
<label>First name: </label>
<input type="text" name="fname"><br>
<label>Lastname name: </label>
<input type="text" name="lname"><br>
<label>Email: </label>
<input type="email" name="email"><br>
<label>Contact No </label>
<input type="text" name="cno"><br>
<label>Address </label>
<input type="text" name="addr"><br>
<label>City </label>
<input type="text" name="city"><br>
<label>State </label>
<input type="text" name="state"><br>
<label>Country </label>
<input type="text" name="country"><br>
<label>Zip Code </label>
<input type="text" name="zipcode"><br>
<label>Credit Card Number </label>
<input type="text" name="ccno"><br>
<label>Payment Option </label>
<select name="Payment_option">
<option value="Cash On Delivery">Cash On Delivery</option>
<option value="Online">Online</option>
</select> <br>
<label>Shipping Method </label>
<select name="Shipping_Method">
<option value="TCS">TCS</option>
<option value="Leapord">Leapord</option>
<option value="FEDEX">FEDEX</option>
</select> <br>
<button type="submit" name="sub">Submit</button>
</form>
<?php
if(isset($_SESSION['errors']))
{
foreach($_SESSION['errors'] as $key => $error)
{
echo $error."<br>";
}
unset($_SESSION['errors']);
}
?>
</body>
</html>
であり、これはあなたがPHPファイルの先頭に値を設定し、私のwelcome2.phpページ
<?php session_start();
$fname = "";
$lname = "";
$email = "";
$cno = "";
$addr = "";
$city = "";
$state = "";
$country = "";
$zipcode = "";
$ccno = "";
extract($_POST);
$errors = array();
if(isset($_POST['fname'])){
$_SESSION['fname'] = $_POST['fname'];
}if(isset($_POST['lname'])){
$_SESSION['lname'] = $_POST['lname'];
}if(isset($_POST['email'])){
$_SESSION['email'] = $_POST['email'];
}if(isset($_POST['cno'])){
$_SESSION['cno'] = $_POST['cno'];
}if(isset($_POST['addr'])){
$_SESSION['addr'] = $_POST['addr'];
}if(isset($_POST['city'])){
$_SESSION['city'] = $_POST['city'];
}if(isset($_POST['state'])){
$_SESSION['state'] = $_POST['state'];
}if(isset($_POST['country'])){
$_SESSION['country'] = $_POST['country'];
}if(isset($_POST['zipcode'])){
$_SESSION['zipcode'] = $_POST['zipcode'];
}if(isset($_POST['ccno'])){
$_SESSION['ccno'] = $_POST['ccno'];
}
if(isset($_POST['sub']))
{
if(!$fname)
$errors[] = "First name is required";
}
if(!$lname)
{
$errors[] = "Last name is required";
}
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$errors = "Only letters and white space allowed";
}
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$errors = "Only letters and white space allowed";
}
if(!$email)
{
$errors[] = "Email is required";
}
if(!$cno)
{
$errors[] = "Contact is required";
}if (strlen($cno)<=5)
{
$errors[] ="Contact contain more than 11 characters";
}
if(!$addr)
{
$errors[] = "Address is required";
}
if(!$city)
{
$errors[] = "City is required";
}
if(!$state)
{
$errors[] = "State is required";
}
if(!$country)
{
$errors[] = "Country is required";
}
if(!$zipcode)
{
$errors[] = "Zip Code is required";
}
if(!$ccno)
{
$errors[] = "Credit Card Number is required";
}?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1> Shipping Information </h1>
<?php echo $fname; echo "<br>";
echo $lname; echo "<br>";
echo $email; echo "<br>";
echo $cno; echo "<br>";
echo $addr; echo "<br>";
echo $city; echo "<br>";
echo $state; echo "<br>";
echo $country; echo "<br>";
echo $zipcode; echo "<br>";
echo $ccno; echo "<br>";
$option1 = isset($_POST['Payment_option']) ? $_POST['Payment_option'] : false;
if ($option1) {
echo htmlentities($_POST['Payment_option'], ENT_QUOTES, "UTF-8");
} else {
echo "Payment Method is required";
} echo "<br>";
$option2 = isset($_POST['Shipping_Method']) ? $_POST['Shipping_Method'] : false;
if ($option2) {
echo htmlentities($_POST['Shipping_Method'], ENT_QUOTES, "UTF-8");
} else {
echo "Shipping Method is required";
}
?>
まだエラーが表示されていない –
@SyedKhizerすべてのフォームデータを取得し、エラーを正しく表示していますか? – Ice76