1

編集/更新:

phpコードをprocess.phpファイルからcontact.phpファイルの先頭に移動しましたが、機能しました。では、contact.phpページにリダイレクトされていないprocess.phpファイルには何が欠けていますか?

これはcontact.phpの私のhtmlです

<?php echo $message; ?>    
<form action="process.php" method="post" name="sign_up">
                                <input type="text" name="first_name" placeholder="First Name" value="<?php echo $_POST[first_name]; ?>" required/>
                                <input type="text" name="last_name" placeholder="Last Name" value="<?php echo $_POST[last_name]; ?>" required/><br>
                            <label class="bill-address">Billing Address:<br>
                                <input type="text" name="address1" placeholder="Address 1" value="<?php echo $_POST[address1]; ?>" required/><br>
                                <input type="text" name="address2" placeholder="Address 2" value="<?php echo $_POST[address2]; ?>" /><br>
                                <input type="text" name="city" placeholder="City" value="<?php echo $_POST[city]; ?>" required/>
                            </label>
                            <?php  
                             $state_list = array('AL'=>"Alabama",  
                                        'AK'=>"Alaska",  
                                        'AZ'=>"Arizona",  
                                        'AR'=>"Arkansas",  
                                        'WV'=>"West Virginia",  
                                        'WI'=>"Wisconsin",  
                                        'WY'=>"Wyoming");
                            ?>
                            <select name="state">
                            <?php
                                while(list($k,$v) = each($state_list)) {
                                    $selected = '';
                                    if ($k == $_POST[state]) {
                                        $selected = ' selected="true"';
                                    }
                                echo "<option value=\"$k\"$selected>$v</option>\n";
                                } 
                            ?>
                            </select>
            <input type="text" name="zip" placeholder="Zip Code" value="<?php echo $_POST[zip]; ?>" required/>
            <br style="clear: left;" />
            <input type="email" name="email" placeholder="you@youremail.com" value="<?php echo $_POST[email]; ?>" required/>
            <input type="tel" name="phone" placeholder="Phone" value="<?php echo $_POST[phone]; ?>" required/>
    <h3>Choose your Package</h3>
        <select name="package">
            <option value="Free">Free!</option>
            <option value="Basic">Basic</option>
            <option value="Corporate">Corporate</option>
            <option value="Enterprise">Enterprise</option>
            <option value="Enterprise_20">Enterprise 20</option>
            <option value="Enterprise_50">Enterprise 50</option>
            <option value="Enterprise_100">Enterprise 100</option>
        </select>
    <h3>Add Media Package?</h3>
            <input type="radio" name="Yes" value="yes" />Yes
            <input type="radio" name="No" value="no" />No
    <button type="submit" class="btn">Send &raquo;</button>
    <?php echo $success_message; ?>
</form>

そしてこれは私のprocess.phpです

    //validate email
function is_valid_email($email) {

        $result = true;
        $pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
        if(!preg_match($pattern, $email)) {
            $result = false;
        }
        return $result;
    }

//when submit has been pressed, begin form validate
if(isset($_POST['submit'])) {

    $valid = true;
    $message = '';
    if ( $_POST['first_name'] == "" ) {
        $message .= "Please include your first name. ";
        $valid = false; 
    }
    if ( $_POST['last_name'] == "" ) {
        $message .= "Please include your last name. ";
        $valid = false; 
    }
    if ( $_POST['address1'] == "" ) {
        $message .= "Please include your billing address. ";
        $valid = false; 
    }
    if ( $_POST['city'] == "" ) {
        $message .= "Please enter a city. ";
        $valid = false; 
    }
    if ( $_POST['state'] == "" ) {
        $message .= "Please select a state. ";
        $valid = false; 
    }
    if ( $_POST['zip'] == "" ) {
        $message .= "Please include a zip code. ";
        $valid = false; 
    }
    if ( $_POST['phone'] == "" ) {
        $message .= "Please include your phone number. ";
        $valid = false; 
    }
    if ( !is_valid_email($_POST['email']) ) {
        $message .= "A valid email is required. "; 
        $valid = false; 
    }
    if ( $_POST['package'] == "" ) {
        $message .= "You forgot to select a service package. ";
        $valid = false; 
    }

    if ( $valid == true ) {
        $success_message = 'Brilliant I say! We will be in contact with you shortly.';

        //clear form when submission is successful
        unset($_POST);
    }
}

動作していません。また、html5検証も機能していません。フォームのマークアップに何か問題がありますか?

4

3 に答える 3

3

をクリックするsubmitと、ブラウザはに移動しprocess.phpます。からのすべてのコードcontact.phpが忘れられ、新しいページが生成されます。

2つのページの間に暗黙のリンクはありません。からのメッセージはに表示されprocess.phpませんcontact.php。現在、何もprocess.phpありませんechoので、おそらく空白のページに到達しています。

これを行う別の方法は、次のように2つのページをマージすることです。

<?php
    //validate email
function is_valid_email($email) {

        $result = true;
        $pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
        if(!preg_match($pattern, $email)) {
            $result = false;
        }
        return $result;
    }

//when submit has been pressed, begin form validate
if(isset($_POST['submit'])) {

    $valid = true;
    $message = '';
    if ( $_POST['first_name'] == "" ) {
        $message .= "Please include your first name. ";
        $valid = false; 
    }
    if ( $_POST['last_name'] == "" ) {
        $message .= "Please include your last name. ";
        $valid = false; 
    }
    if ( $_POST['address1'] == "" ) {
        $message .= "Please include your billing address. ";
        $valid = false; 
    }
    if ( $_POST['city'] == "" ) {
        $message .= "Please enter a city. ";
        $valid = false; 
    }
    if ( $_POST['state'] == "" ) {
        $message .= "Please select a state. ";
        $valid = false; 
    }
    if ( $_POST['zip'] == "" ) {
        $message .= "Please include a zip code. ";
        $valid = false; 
    }
    if ( $_POST['phone'] == "" ) {
        $message .= "Please include your phone number. ";
        $valid = false; 
    }
    if ( !is_valid_email($_POST['email']) ) {
        $message .= "A valid email is required. "; 
        $valid = false; 
    }
    if ( $_POST['package'] == "" ) {
        $message .= "You forgot to select a service package. ";
        $valid = false; 
    }

    if ( $valid == true ) {
        $success_message = 'Brilliant I say! We will be in contact with you shortly.';

        //clear form when submission is successful
                          //don't clear this, you need this to re-populate the page below
        //unset($_POST);
    }
}

?><!doctype html>
<html>
<head>
...
</head>
<body>
<?php echo $message; ?>    
<form action="contact.php" method="post" name="sign_up">
    <input type="text" name="first_name" placeholder="First Name" value="<?php echo $_POST[first_name]; ?>" required/>
    <input type="text" name="last_name" placeholder="Last Name" value="<?php echo $_POST[last_name]; ?>" required/><br>
    <label class="bill-address">Billing Address:<br>
        <input type="text" name="address1" placeholder="Address 1" value="<?php echo $_POST[address1]; ?>" required/><br>
        <input type="text" name="address2" placeholder="Address 2" value="<?php echo $_POST[address2]; ?>" /><br>
        <input type="text" name="city" placeholder="City" value="<?php echo $_POST[city]; ?>" required/>
    </label>
<?php  
$state_list = array('AL'=>"Alabama",  
                                        'AK'=>"Alaska",  
                                        'AZ'=>"Arizona",  
                                        'AR'=>"Arkansas",  
                                        'WV'=>"West Virginia",  
                                        'WI'=>"Wisconsin",  
                                        'WY'=>"Wyoming");
                            ?>
                            <select name="state">
                            <?php
                                while(list($k,$v) = each($state_list)) {
                                    $selected = '';
                                    if ($k == $_POST[state]) {
                                        $selected = ' selected="true"';
                                    }
                                echo "<option value=\"$k\"$selected>$v</option>\n";
                                } 
                            ?>
                            </select>
            <input type="text" name="zip" placeholder="Zip Code" value="<?php echo $_POST[zip]; ?>" required/>
            <br style="clear: left;" />
            <input type="email" name="email" placeholder="you@youremail.com" value="<?php echo $_POST[email]; ?>" required/>
            <input type="tel" name="phone" placeholder="Phone" value="<?php echo $_POST[phone]; ?>" required/>
    <h3>Choose your Package</h3>
        <select name="package">
            <option value="Free">Free!</option>
            <option value="Basic">Basic</option>
            <option value="Corporate">Corporate</option>
            <option value="Enterprise">Enterprise</option>
            <option value="Enterprise_20">Enterprise 20</option>
            <option value="Enterprise_50">Enterprise 50</option>
            <option value="Enterprise_100">Enterprise 100</option>
        </select>
    <h3>Add Media Package?</h3>
            <input type="radio" name="Yes" value="yes" />Yes
            <input type="radio" name="No" value="no" />No
    <button type="submit" class="btn">Send &raquo;</button>
    <?php echo $success_message; ?>
</form>
</body>
</html>

これで変数$message$success_message変数が保存され、下のページマークアップに表示されます。

于 2012-11-09T18:05:43.470 に答える
0
     //validate email
     function is_valid_email($email) {

    $result = true;
    $pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
    if(!preg_match($pattern, $email)) {
        $result = false;
    }
    return $result;
}

   //when submit has been pressed, begin form validate
   if(isset($_POST['submit'])) {

  $valid = true;
  $message = '';
  if ( $_POST['first_name'] == "" ) {
    $message = "Please include your first name. ";
   echo $message;
    $valid = false; 
  }
if ( $_POST['last_name'] == "" ) {
    $message = "Please include your last name. ";
    echo $message;
    $valid = false; 
}
if ( $_POST['address1'] == "" ) {
    $message = "Please include your billing address. ";
    echo $message;
    $valid = false; 
  }
  if ( $_POST['city'] == "" ) {
    $message = "Please enter a city. ";
    echo $message;
    $valid = false; 
  }
  if ( $_POST['state'] == "" ) {
    $message = "Please select a state. ";
  echo $message;
    $valid = false; 
}
if ( $_POST['zip'] == "" ) {
    $message = "Please include a zip code. ";
   echo $message;
    $valid = false; 
}
if ( $_POST['phone'] == "" ) {
    $message = "Please include your phone number. ";<
    echo $message;
    $valid = false; 
}
if ( !is_valid_email($_POST['email']) ) {
    $message = "A valid email is required. ";
    echo $message; 
    $valid = false; 
}
if ( $_POST['package'] == "" ) {
    $message = "You forgot to select a service package. ";
    echo $message;
    $valid = false; 
}

if ( $valid == true ) {
    $success_message = 'Brilliant I say! We will be in contact with you shortly.';
    echo $success_message;

    //clear form when submission is successful
    unset($_POST);
}

}

echo $message検証中にエコーするために追加しまし.=たが、間違った方法だと思いました。そうしないと、すべてのエラーメッセージを含むメッセージが表示されます..

于 2012-11-09T17:56:58.637 に答える
0

これを行の後に追加してみてくださいunset( $_POST )

header('Location: contact.php');

これでページに戻るはずですcontact.php

編集:

ただし、コードが完全に希望どおりに機能するようにするには、ここで何をしますか。

<?php 
    session_start();

    if (isset ($_SESSION['message'])) {
        echo $_SESSION['message'];
        session_destroy();
    }
?>    
<form action="process.php" method="post" name="sign_up">
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $_POST[first_name]; ?>" />
<input type="text" name="last_name" placeholder="Last Name" value="<?php echo $_POST[last_name]; ?>" /><br>
<label class="bill-address">Billing Address:<br>
<input type="text" name="address1" placeholder="Address 1" value="<?php echo $_POST[address1]; ?>" /><br>
<input type="text" name="address2" placeholder="Address 2" value="<?php echo $_POST[address2]; ?>" /><br>
<input type="text" name="city" placeholder="City" value="<?php echo $_POST[city]; ?>" />
    </label>
<?php  
   $state_list = array('AL'=>"Alabama",  
    'AK'=>"Alaska",  
    'AZ'=>"Arizona",  
    'AR'=>"Arkansas",  
    'WV'=>"West Virginia",  
    'WI'=>"Wisconsin",  
    'WY'=>"Wyoming");
?>
     <select name="state">
 <?php
    while(list($k,$v) = each($state_list)) {
    $selected = '';
    if ($k == $_POST[state]) {
   $selected = ' selected="true"';
}
 echo "<option value=\"$k\"$selected>$v</option>\n";
} 
?>
</select>
<input type="text" name="zip" placeholder="Zip Code" value="<?php echo $_POST[zip]; ?>" />
<br style="clear: left;" />
<input type="email" name="email" placeholder="you@youremail.com" value="<?php echo $_POST[email]; ?>" />
<input type="tel" name="phone" placeholder="Phone" value="<?php echo $_POST[phone]; ?>" />
<h3>Choose your Package</h3>
<select name="package">
           <option value="Free">Free!</option>
            <option value="Basic">Basic</option>
            <option value="Corporate">Corporate</option>
            <option value="Enterprise">Enterprise</option>
            <option value="Enterprise_20">Enterprise 20</option>
            <option value="Enterprise_50">Enterprise 50</option>
            <option value="Enterprise_100">Enterprise 100</option>
</select>
    <h3>Add Media Package?</h3>
            <input type="radio" name="Yes" value="yes" />Yes
            <input type="radio" name="No" value="no" />No
    <button type="submit" class="btn">Send &raquo;</button>
<?php 
    //session already started on line 2

    if (isset( $_SESSION['success'] )) {
        echo $_SESSION['success'];
        session_destroy();
    }
?>

</form>

contact.php、および

<?php

//when submit has been pressed, begin form validate else return to contact.php
  if ( $_SERVER[ 'REQUEST_METHOD' ] == "POST" ) {
    session_start();


    //validate email
function is_valid_email($email) {

        $result = true;
        $pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
        if(!preg_match($pattern, $email)) {
            $result = false;
        }
        return $result;
    }


    $valid = true;
    $message = '';
    if ( $_POST['first_name'] == "" ) {
        $message .= "Please include your first name. ";
        $valid = false; 
    }
    if ( $_POST['last_name'] == "" ) {
        $message .= "Please include your last name. ";
        $valid = false; 
    }
    if ( $_POST['address1'] == "" ) {
        $message .= "Please include your billing address. ";
        $valid = false; 
    }
    if ( $_POST['city'] == "" ) {
        $message .= "Please enter a city. ";
        $valid = false; 
    }
    if ( $_POST['state'] == "" ) {
        $message .= "Please select a state. ";
        $valid = false; 
    }
    if ( $_POST['zip'] == "" ) {
        $message .= "Please include a zip code. ";
        $valid = false; 
    }
    if ( $_POST['phone'] == "" ) {
        $message .= "Please include your phone number. ";
        $valid = false; 
    }
    if ( !is_valid_email($_POST['email']) ) {
        $message .= "A valid email is required. "; 
        $valid = false; 
    }
    if ( $_POST['package'] == "" ) {
        $message .= "You forgot to select a service package. ";
        $valid = false; 
    }

    if ( $valid == true ) {
        $success_message = 'Brilliant I say! We will be in contact with you shortly.';

        //clear form when submission is successful
        unset($_POST);
        $_SESSION['success']=$success_message;
    }
    else {
        $_SESSION['message'] = $message;
    }
    header('Location: contact.php');
} // end of if ( $_SERVER[ 'REQUEST_METHOD' ] == "POST" )
else header('Location: contact.php');

?>

process.php

次のコントロールは、コードで最大の混乱を引き起こしたと思います。

//when submit has been pressed, begin form validate
if(isset($_POST['submit']))

交換するとすぐに、すべてがうまく機能し始めました。

于 2012-11-10T21:40:45.227 に答える