粘性表单数据问题php

时间:2010-10-29 19:53:38

标签: php

使用php和html尝试一些事情,我在尝试重新发布所有表单数据时遇到问题。我已经想出了如何做文本框。选择框,收音机和复选框仍然处于亏损状态。我会更好地重新发布HTML页面或让我的包含模板代码来处理它吗?

 <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="css/rocks.css">
    <title>About Us-- Rocks</title>
</head>
<body> 
  <?php include("templates/banner_navigation.php"); ?>
  <div id="links_group">
  <?php include("templates/menu.php"); ?> <!-- 1st include for navigation, stored in templates  -->
  </div>
   <div id="about_us_form">
       <?php include("templates/about_us_valid.php"); ?>
       <form action="<?php echo $PHP_SELF;?>" method="post">
       <table>
       <tr><td>First name:</td><td><input type="text" name="contact_name" title="Enter your first name here" value="<?php if (isset($_POST['contact_name'])) echo $_POST['contact_name']; ?>" /></td></tr>
       <tr><td>Email Address:</td><td><input type="text" name="contact_email" title="Enter your email here" value="<?php if (isset($_POST['contact_email'])) echo $_POST['contact_email']; ?>" ></td></tr>
       <tr><td>Phone Number:</td><td><input type="text" name="contact_phone_number" title="555-555-5555" value="<?php if (isset($_POST['contact_phone_number'])) echo $_POST['contact_phone_number']; ?>" /></td></tr>
       <tr><td>I prefer to be contacted by:</td><td><input type="radio" name="preference" value="Email" checked/>Email<input type="radio" name="preference" value="Phone" />Phone</td></tr>
       <tr><td>I am interested in:</td> <td><select name="select_rocks"><option value="gold">Gold</option>
             <option value="silver">Silver</option>
             <option value="thorium" selected="selected">Thorium</option>
             <option value="titanium">Titanium</option>
             </select></td></tr>
       <tr><td>  I would like to ask about:</td></tr>
       <tr><td><textarea name="that_textarea" rows ="10" cols="25" title="Enter your questions here!">
       </textarea></td></tr>
       <tr><td>I am also interested in:</td><td>
       <tr><td>Gemstones <input type="checkbox" name="checkbox1[]" value="gemstones" /></td><tr>
       <tr><td>Ore processing <input type="checkbox" name="checkbox1[]" value="oreprocessing" /></td></tr>
       <tr><td><input type="hidden" name="checkbox1[]" value="" /> </td><tr>

       <tr><td><input type="submit" value="submit" name="submitform" /></td></tr>

       </table><br/>
       </form>

  </div>
</body>

包含模板:

   <?php
   function check_for_cat($namecheck=false,$emailcheck=false){
       if ($namecheck) $name_field_error = "(((Name Invalid!)))";
       if ($emailcheck) $email_field_error = "(((Email Invalid!)))";
       //Start of form
       if($namecheck) echo "<tr><td>$name_field_error</td></tr>";
       if($emailcheck) echo "<tr><td>$email_field_error</td></tr>";

   }

       if (!isset($_POST['submitform'])) {
       check_for_cat();
       } else {
          $namecheck = false;
          $emailcheck = false;


          $contact_name = isset($_POST['contact_name']) ? trim($_POST['contact_name'])  : '';
          $contact_email = isset($_POST['contact_email']) ? trim($_POST['contact_email'])  : '';
       if (strlen($contact_name)<3)  $namecheck = true;
       if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $contact_email)) $emailcheck=true;
       if ($namecheck || $emailcheck ){
       check_for_cat($namecheck,$emailcheck);

       } else {
       echo '(((OMG the form is submitted!)))';
   }
}
   ?>

它可能很容易做,我只是看了很长时间的代码,谢谢你的帮助!如果可能放一些代码,谷歌和雅虎找不到我太多的东西。

1 个答案:

答案 0 :(得分:1)

使用选择选项...

无线电和复选框输入都使用checked =“checked”,以便检查它们。您必须使用一些PHP逻辑来确定用户的输入是否与必要的输入匹配,以检查收音机/复选框和下拉列表。例如:

<input type="checkbox" <?php if ($userval == $boxval) echo 'checked="checked"'; ?> />