-7
<html>
<head>
<title>
Registration
</title>
</head>
<?php
if(isset($_POST['submit buttton'])){
    processForm();
}else{
    displayForm();
}


/*check out this function. Here in array doesnot work*/

function validateField($fieldname,$missingfield){
    if(in_array($fieldname,$missingfield)){
        echo 'class = "error"';
    }
}
function setValue($fieldname){
    if(isset($_POST[$fieldname])){
        echo $_POST[$fieldname];
    }
}

function setChecked($fieldname,$fieldvalue){
    if((isset($_POST[$fieldname]) and ($_POST[fieldname] == $fieldvalue)){
        echo 'checked = "checked"';
    }
}
function setSelected( $fieldName, $fieldValue ) {
    if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
        echo ' selected="selected"';
    }
}
function processForm(){
    $requiredfields = ("firstname","lastname","password1","password2","gender");
    $missingfields = array();
    foreach($missingfields as $missingfield){
        if((!isset($_POST[$requiredfield]) or $_POST[$requiredfield]){
            $missingfields[] = $requiredfield ;
        }
    }
    if ( $missingFields ) {
        displayForm( $missingfields );
    } else {
    displayThanks();
    }
}

function displayForm($missingfields){
    <?php
        <h1>Membership Form</h1>
        <?php if ( $missingFields ) { ?>
        <p class="error">There were some problems with the form you submitted.
        Please complete the fields highlighted below and  click Send Details to
        resend the form.</p>
        <?php } else { ?>
        <p>Thanks for choosing to join The Widget Club. To register, please
        fill in your details below and click Send Details. Fields marked with an
        asterisk (*) are required.</p>
    ?>

<?php } ?>

    <form action="registration.php" method="post">
    <div style="width: 30em;">
    <label for="firstName"<?php validateField( "firstName",
                        $missingFields ) ?>>First name *</label>
    <input type="text"  name="firstName" id="firstName"
                        value="<?php setValue( "firstName" ) ?>" />
    <label for="lastName"<?php validateField( "lastName",
$missingFields ) ?>>Last name *</label>
    <input type="text" name="lastName" id="lastName" value=
    "<?php setValue( "lastName" ) ?>" />
    <label for="password1"<?php if ( $missingFields ) echo
    ' class="error"’ ?>>Choose a password *</label>
    <input type="password" name="password1" id="password1" value="" />
    <label for="password2"<?php if ( $missingFields ) echo
    ' class="error"’ ?>>Retype password *</label>
    <input type="password" name="password2" id="password2" value="" />
    <label<?php validateField( "gender", $missingFields ) ?>>Your
    gender: *</label>
    <label for="genderMale">Male</label>
    <input type="radio" name="gender" id="genderMale" value=
    "M"<?php setChecked( "gender", "M" )?>/>
    <label for="genderFemale">Female</label>
    <input type="radio" name="gender" id="genderFemale" value=
    "F"<?php setChecked( "gender", "F" )?> />
    <label for="favoriteWidget">What’s your favorite widget? *</label>
    <select name="favoriteWidget" id="favoriteWidget" size="1">
    <option value="superWidget"<?php setSelected( "favoriteWidget",
    "superWidget" ) ?>>The SuperWidget</option>
    ?>

</html>

ファイルの最初の実行では、in_array 関数が機能していないようです。つまり、isset() のような関数は太字で表示されますが、in_array 関数が検出されないようです。ブラウザには次のエラーが表示されます。

( ! ) 解析エラー: 構文エラー、15 行目の C:\wamp\www\reg.php の予期しない '{'。

4

3 に答える 3

2
$requiredfields = ("firstname","lastname","password1","password2","gender"); 

この行は無効です。arrayの前にキーワードが必要(です。

一般に、ブラケット マッチングを備えたコード エディターを使用します。)}]これにより、欠落している余分な({[ものをより簡単に見つけることができます。同様の方法で HTML タグのマッチングを含むものもあります。

http://notepad-plus-plus.org/が良い例です。

于 2012-06-17T00:07:47.853 に答える
1
function setChecked($fieldname,$fieldvalue){
    if((isset($_POST[$fieldname]) and ($_POST[fieldname] == $fieldvalue)){

2 番目(は、if((削除する必要があります。

array次の$requiredfields = ...行も追加する必要があります。

$requiredfields = array("firstname","lastname","password1","password2","gender");
于 2012-06-17T00:07:07.650 に答える
0

前に述べたように:ではなく使用し&& , ||ますand , or。あなたは必要ですarray('a', 'b', 'c')、あなたは大文字、小文字に非常に注意する必要があります:(n / N)$fieldnameと同じではありません。$fieldNameこれが役立つはずであることを確認する他のいくつかの要素があります:

<html>
<head>
<title>Registration</title>
<style type="text/css">.error{color:#F00}</style>
</head>
<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    processForm();
}
else {
    displayForm();
}

function validateField($error)
{
    if($error)
    {
        return 'class="error"';
    }
}

function setValue($fieldname)
{
    if(isset($_POST[$fieldname]))
    {
        return $_POST[$fieldname];
    }
}

function setChecked($fieldname,$fieldvalue)
{
    if(isset($_POST[$fieldname]) && $_POST[$fieldname] == $fieldvalue)
    {
        return ' checked="checked"';
    }
}

function setSelected( $fieldname, $fieldvalue )
{
    if ( isset( $_POST[$fieldname] ) && $_POST[$fieldname] == $fieldvalue )
    {
        return ' selected="selected"';
    }
}

function processForm()
{
    $requiredfields = array("firstName", "lastName", "password1", "password2", "gender");
    $missingfields = array();
    $missingfields['title'] = false;

    foreach($requiredfields as $requiredfield)
    {
        if(!isset($_POST[$requiredfield]) || empty($_POST[$requiredfield]))
        {
            $missingfields[$requiredfield] = true;
            $missingfields['title'] = true;
        }
        else {
            $missingfields[$requiredfield] = false;
        }
    }

    if ($missingfields['title'])
    {
        displayForm( $missingfields );
    } 
    else {
        echo displayThanks();
    }
}

function displayThanks()
{
    return '<h1>Everything is OK !</h1>';
}

function displayForm($missingfields)
{
?>
    <h1>Membership Form</h1>
    <?php if ($missingfields['title']) { ?>
    <p class="error">There were some problems with the form you submitted.
    Please complete the fields highlighted below and  click Send Details to
    resend the form.</p>
    <?php } else { ?>
    <p>Thanks for choosing to join The Widget Club. To register, please
    fill in your details below and click Send Details. Fields marked with an
    asterisk (*) are required.</p>
    <?php } ?>

    <form action="registration.php" method="post">
        <div style="width: 30em;">
            <label for="firstName"<?php echo validateField( $missingfields['firstName'] ) ?>>First name *</label>
            <input type="text"  name="firstName" id="firstName"  value="<?php echo setValue( "firstName" ) ?>" />
            <br>
            <label for="lastName"<?php echo validateField( $missingfields['lastName'] ) ?>>Last name *</label>
            <input type="text" name="lastName" id="lastName" value="<?php echo setValue( "lastName" ) ?>" />
            <br>
            <label for="password1"<?php echo validateField( $missingfields['password1'] ); ?>>Choose a password *</label>
            <input type="password" name="password1" id="password1" value="" />
            <br>
            <label for="password2"<?php echo validateField( $missingfields['password2'] ); ?>>Retype password *</label>
            <input type="password" name="password2" id="password2" value="" />
            <br>
            <label<?php echo validateField( $missingfields['gender'] ) ?>>Your gender: *</label>
            <label for="genderMale">Male</label>
            <input type="radio" name="gender" id="genderMale" value="M" <?php echo setChecked( "gender", "M" )?>/>
            <label for="genderFemale">Female</label>
            <input type="radio" name="gender" id="genderFemale" value="F" <?php echo setChecked( "gender", "F" )?> />
            <br>
            <label for="favoriteWidget">What’s your favorite widget? *</label>
            <select name="favoriteWidget" id="favoriteWidget" size="1">
                <option value="superWidget"<?php echo setSelected( "favoriteWidget", "superWidget" ) ?>>The SuperWidget</option>
            </select>
            <br>
            <input type="submit" value="Sign up">
        </div>
    </form>
<?php } ?>
</html>

それが役に立てば幸い。

于 2012-06-17T01:13:32.563 に答える