创建PHP粘贴表单

时间:2015-02-06 05:35:14

标签: php html

我试图在php中创建一个粘性表单,包括文本框,文本区域,单选按钮组,复选框组和下拉列表。我设法让粘性部分与文本框一起工作,但对于其他一切,它要么不在重新加载,要么在特定元素之后不显示内容。例如,我的代码如下,Male单选按钮后没有显示任何内容。只是文字"男",但之后什么都没有。我不确定我做错了什么,我不知道如何使剩余的元素变得粘稠(文本区域,复选框和单选按钮)。

<?php
function display_form()
{
   $self = $_SERVER['PHP_SELF'];
   global $nameErr,$emailErr,$subjectErr,$commentErr,$termErr;
   $name = isset($_POST['name']) ? $_POST['name'] : '';
   $email = isset($_POST['email']) ? $_POST['email'] : '';  
   $subject = isset($_POST['subject']) ? $_POST['subject'] : '';
   $comment = isset($_POST['comment']) ? $_POST['comment'] : '';
?>
<!doctype html>
<html>
<head>
    <style>
        .error {color: #FF0000;}
    </style>
</head>
<body>
<h1>Contact Us</h1>
<form action="<?php echo $self ?>" method="POST">
    <span class="error"> <?php echo "* Required Field(s)";?></span>
    <br><br>
        Name: <input type="text" name="name" value="<?php echo $name?>">
    <span class="error">* <?php echo $nameErr;?></span>
        <br><br>
        E-Mail: <input type="text" name="email" value="<?php echo $email?>">
    <span class="error">* <?php echo $emailErr;?></span>
        <br><br>
       Gender: Male <input type="radio" <?php if($gender==="Male") echo "selected='selected'"; ?> name="gender" value="Male">
       Female <input type="radio" <?php if($gender==="Female") echo "selected='selected'"; ?> name="gender" value="Female">

       <br><br>
       Subject: <select name="subject">
        <option value="select">Select...</option>
            <option value="useraccount">User Account</option>
            <option value="reportabuse">Report Abuse</option>
            <option value="reporterror">Report Error</option>
            <option value="jobs">Jobs</option>
            <option value="other">Other</option>
        </select>
    <span class="error">* <?php echo $subjectErr;?></span>
        <br><br>
        Comment: <textarea name="comment" rows="5" cols="40"></textarea>
    <span class="error">* <?php echo $commentErr;?></span>
        <br><br>
        <input type="checkbox" name="check_list[]" value="terms"><label>I understand the <a href="">Terms and Policies</a></label>
    <span class="error">* <?php echo $termErr;?></span> 
    <br>                
        <input type="checkbox" name="check_list[]" value="subscribe"><label>Subscribe to our Monthly Newsletters</label>
        <br><br>
    <input type="submit" name="button" value="Submit">
    </form>

</body>
</html>
<?php
}
?>

1 个答案:

答案 0 :(得分:0)

用此代码替换复选框

 Gender: Male <input type="radio" name="gender" <?php if($gender=="Male") echo"checked='checked'";?>value="Male"> 
 Female <input type="radio" name="gender"  <?php if($gender=="Female") echo"checked='checked'";?> value="Female">