2014-11-15 294 views
0

我已經找到一個問題,「檢查PHP中是否至少勾選了一個複選框」,但這並不符合我的需求,因爲我是一個有組織的人員,而且沒有看起來像是一個有組織的做法。我有此複選框代碼:檢查PHP中是否至少選中了一個複選框

<h1>_________</h1> 
    <p>Which records would you like to see?<br /> 
    Enter as little or as many criteria as you want and then press Submit.</p> 
    <form method = "get" action = "http://www.its-a-secret.com/validation.php"> 
    <div> 
     <span> 
     <label for = "lastname" id = "txtlabel">Last Name</label> 
     <input type = "text" name = "lastname" /> 
     </span> 
     <span> 
     <label for = "firstname" id = "txtlabel">First Name</label> 
     <input type = "text" name = "firstname" /> 
     </span> 
     <span> 
     <label for = "company" id = "txtlabel">Company</label> 
     <?php 
      session_start(); 
      mysql_connect("______","_______","________"); 
      mysql_select_db("____"); 
      $sql = "SELECT DISTINCT company FROM _____"; 
      $result = mysql_query($sql); 
      print "<select name = 'company' id = 'company'> \n"; 
      print "   <option></option> \n"; 
      while ($row = mysql_fetch_array($result)) { 
      print "   <option value='" . $row['company'] . "'>" . $row['company'] . "</option> \n"; 
      } 
      print "   </select> \n"; 
     ?> 
     </span> 
     <span id = "spanaccttype"> 
     <label for = "accttype" id = "txtlabel">Account type</label> 
     <?php 
      mysql_connect("_____","_____","_______"); 
      mysql_select_db("_____"); 
      $sql = "SELECT DISTINCT accttype FROM _____"; 
      $result = mysql_query($sql); 
      print "<select name = 'accttype' id = 'accttype'> \n"; 
      print "   <option></option> \n"; 
      while ($row = mysql_fetch_array($result)) { 
      print "   <option value='" . $row['accttype'] . "'>" . $row['accttype'] . "</option> \n"; 
      } 
      print "   </select> \n"; 
     ?> 
     </span> 
     <span id = "spanproductname"> 
     <label for = "productname" id = "txtlabel">Product Name</label> 
     <?php 
      mysql_connect("______","______","______"); 
      mysql_select_db("_____"); 
      $sql = "SELECT DISTINCT productname FROM ______"; 
      $result = mysql_query($sql); 
      print "<select name = 'productname' id = 'productname'> \n"; 
      print "   <option></option> \n"; 
      while ($row = mysql_fetch_array($result)) { 
      print "   <option value='" . $row['productname'] . "'>" . $row['productname'] . "</option> \n"; 
      } 
      print "   </select> \n"; 
     ?> 
     </span> 
     <span id = "spansharetype"> 
     <label for = "sharetype" id = "txtlabel">Share Type</label> 
     <?php 
      mysql_connect("_____","_____","______"); 
      mysql_select_db("_____"); 
      $sql = "SELECT DISTINCT sharetype FROM _____"; 
      $result = mysql_query($sql); 
      print "<select name = 'sharetype' id = 'sharetype'> \n"; 
      print "   <option></option> \n"; 
      while ($row = mysql_fetch_array($result)) { 
      print "   <option value='" . $row['sharetype'] . "'>" . $row['sharetype'] . "</option> \n"; 
      } 
      print "   </select> \n"; 
     ?> 
     </span> 
     <span id = "spanclosed"> 
     <label for = "closed" id = "txtlabel">Closed?</label> 
     <?php 
      mysql_connect("_________","______","__"); 
      mysql_select_db("____"); 
      $sql = "SELECT DISTINCT closed FROM _____"; 
      $result = mysql_query($sql); 
      print "<select name = 'closed' id = 'closed'> \n"; 
      print "   <option></option> \n"; 
      while ($row = mysql_fetch_array($result)) { 
      print "   <option value='" . $row['closed'] . "'>" . $row['closed'] . "</option> \n"; 
      } 
      print "   </select> \n"; 
     ?> 
     </span> 
     <span id = "spanvaluegreaterthan"> 
     <label for = "valuegreaterthan" id = "txtlabel">Value Greater Than $</label> 
     <input type = "number" name = "valuegreaterthan" /> 
     </span> 
     <br /> 
    <span id = "spanwhatcolumnstoshowquestion">What columns would you like to show? (Note if none are checked none of the below will be shown.)</span> 
      <table id = "showwhichcolumns"> 
      <tr> 
       <td><input type = "checkbox" id = "showcolumncompany" name = "showcolumncompany" value = "showcolumncompany" title = "Show column company" /><label for = "showcolumncompany" id = "check">Company</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumnaccountnumber" name = "showcolumnaccountnumber" value = "showcolumnaccountnumber" title = "Show column account number" /><label for = "showcolumnaccountnumber" id = "check">Account #</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumncontractdate" name = "showcolumncontractdate" value = "showcolumncontractdate" title = "Show column contract date" /><label for = "showcolumncontractdate" id = "check">Contract Date</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumnclosed" name = "showcolumnclosed" value = "showcolumnclosed" title = "Show column close" /><label for = "showcolumnclosed" id = "check">Closed?</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumndateclosed" name = "showcolumndateclosed" value = "showcolumndateclosed" /><label for = "showcolumndateclosed" id = "check">Date Closed</label></td> 
      </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumndob" name = "showcolumndob" value = "showcolumndob" /><label for = "showcolumndob" id = "check">DOB</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumnproductname" name = "showcolumnproductname" value = "showcolumnproductname" /><label for = "showcolumnproductname" id = "check">Product Name</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumncommissionpercent" name = "showcolumncommissionpercent" value = "showcolumncommissionpercent" /><label for = "showcolumncommissionpercent" id = "check">Commission %</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumncommissionpayoutpercent" name = "showcolumncommissionpayoutpercent" value = "showcolumncommissionpayoutpercent" /><label for = "showcolumncommissionpayoutpercent" id = "check">Commission Payout (%)</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumncommissionpayoutdollars" name = "showcolumncommissionpayoutdollars" value = "showcolumncommissionpayoutdollars" /><label for = "showcolumncommissionpayoutdollars" id = "check">Commission Payout ($)</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumntrailcomm" name = "showcolumntrailcomm" value = "showcolumntrailcomm" /><label for = "showcolumntrailcomm" id = "check">Trail Comm?</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumnguaranteepercent" name = "showcolumnguaranteepercent" value = "showcolumnguaranteepercent" /><label for = "showcolumnguaranteepercent" id = "check">Guarantee %</label></td> 
       </tr> 
       <tr> 
       <td><input type = "checkbox" id = "showcolumnaccounttype" name = "showcolumnaccounttype" value = "showcolumnaccounttype" title = "Show column account type" /><label for = "showcolumnaccounttype" id = "check">Account Type</label></td> 
      </table> 

      <p> 
      <input type = "submit" value = "Submit" id = "gobutton" /> 
      </p> 

的形式提交到一個名爲頁面validation.php,這是該頁面的代碼(我幾乎沒有任何東西):

<?php 
     $lastname = $_REQUEST["lastname"]; 

     $firstname = $_REQUEST["firstname"]; 

     $company = $_REQUEST["company"]; 

     $accttype = $_REQUEST["accttype"]; 

     $productname = $_REQUEST["productname"]; 

     $sharetype = $_REQUEST["sharetype"]; 

     $closed = $_REQUEST["closed"]; 

     $valuegreaterthan = $_REQUEST["valuegreaterthan"]; 
     header("Location: http://www.its-a-secret/?lastname=" . htmlspecialchars($lastname) . "&firstname=" . htmlspecialchars($firstname) . "&company=" . htmlspecialchars($company)); 
    ?> 

我想刪除HTML特殊字符而不是更改它們,我還希望First Name和Last Name只通過告訴用戶只有一個大寫字母,如果他們不這樣做,我希望SQL能夠使用名字或姓氏都是大寫或者是像這樣瘋狂的東西。如果有的話,我也希望名字和姓氏有所有的空間。我希望至少有一個複選框被選中。我也希望有一個正數,它是數字文本框中的一個數字。

回答

1

我不知道你正在嘗試做的,但這裏是我的猜測代碼:

if(isset($_GET['showcolumnxx']) || isset($_GET['showcolumnxx']) || isset($_GET['showcolumnxx'] || etc.)) { 
    $newUrl = "http://something.site.end/otherpageshere/[screenAfterValidation].php"; 
    header('Location: ' . $newUrl); 
    } else { 
     print "<p style = "color: red;">You must at least check one checkbox.</p>\n"; 

    } 
+1

這如果任何複選框張貼基本檢查(檢查) –

+1

如果你解釋代碼 –

+1

如果用戶已經在他對HTML的複選框提交的任何數據基本上,這檢查那將是有益的。 –

1

這可能會有幫助。我只是在同一頁面迴應。你可以根據你的需要改變它。

<?php 
if(!empty($_GET['check'])) { 
    foreach($_GET['check'] as $check) { 
     echo $check."<br />"; 
    } 
} 
?> 

<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
    <span id = "spanwhatcolumnstoshowquestion">What columns would you like to show? (Note if none are checked none of the below will be shown.)</span> 
     <table id = "showwhichcolumns"> 
     <tr> 
      <td><input type = "checkbox" id = "showcolumncompany" name = "check[]" value = "showcolumncompany" title = "Show column company" /><label for = "showcolumncompany" id = "check">Company</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumnaccountnumber" name = "check[]" value = "showcolumnaccountnumber" title = "Show column account number" /><label for = "showcolumnaccountnumber" id = "check">Account #</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumncontractdate" name = "check[]" value = "showcolumncontractdate" title = "Show column contract date" /><label for = "showcolumncontractdate" id = "check">Contract Date</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumnclosed" name = "check[]" value = "showcolumnclosed" title = "Show column close" /><label for = "showcolumnclosed" id = "check">Closed?</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumndateclosed" name = "check[]" value = "showcolumndateclosed" /><label for = "showcolumndateclosed" id = "check">Date Closed</label></td> 
     </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumndob" name = "check[]" value = "showcolumndob" /><label for = "showcolumndob" id = "check">DOB</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumnproductname" name = "check[]" value = "showcolumnproductname" /><label for = "showcolumnproductname" id = "check">Product Name</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumncommissionpercent" name = "check[]" value = "showcolumncommissionpercent" /><label for = "showcolumncommissionpercent" id = "check">Commission %</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumncommissionpayoutpercent" name = "check[]" value = "showcolumncommissionpayoutpercent" /><label for = "showcolumncommissionpayoutpercent" id = "check">Commission Payout (%)</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumncommissionpayoutdollars" name = "check[]" value = "showcolumncommissionpayoutdollars" /><label for = "showcolumncommissionpayoutdollars" id = "check">Commission Payout ($)</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumntrailcomm" name = "check[]" value = "showcolumntrailcomm" /><label for = "showcolumntrailcomm" id = "check">Trail Comm?</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumnguaranteepercent" name = "check[]" value = "showcolumnguaranteepercent" /><label for = "showcolumnguaranteepercent" id = "check">Guarantee %</label></td> 
      </tr> 
      <tr> 
      <td><input type = "checkbox" id = "showcolumnaccounttype" name = "check[]" value = "showcolumnaccounttype" title = "Show column account type" /><label for = "showcolumnaccounttype" id = "check">Account Type</label></td> 
     </table> 

     <p> 
     <input type = "submit" value = "Submit" id = "gobutton" /> 
     </p> 
</form> 
+0

@ewe,你沒有要求安全。任何方式,爲了獲得乾淨的數據,你有很多選項,如htmlspecialchars(),htmlentities(),stripcslashes或者你可以做你自己的自定義函數。 –

+0

好的,謝謝。該代碼看起來有點腥。 –

相關問題