2014-02-08 159 views
0

我新来php.Please帮我出来这个! 我有一个学生申请表格在HTML和PHP ..与文本框和复选框。我无法读取复选框的值。请帮我的代码错了什么。这是我的HTML代码和PHP代码,以及使用多个复选框

的index.php

<div class="formWrapper"> 

    <div class="formLeft"> 
     <h4>Admission to LKG</h4> 

     <form name="LKGForm" id="LKGForm" method="POST" action="pdf/pdf.php"> 

     <div class="formItem"> 
      <label>Student Name</label> 
      <input type="text" name="LKGStudentName" id="LKGStudentName" /> 
     </div> 

     <div class="formItem"> 
      <label>Student Date of Birth</label> 
      <input type="text" name="LKGStudentDOB" id="LKGStudentDOB" /> 
      Ex: 01-Jan-2014 
     </div> 

     <div class="formItem"> 
      <label>Father Name</label> 
      <input type="text" name="LKGParentName" id="LKGParentName" /> 
     </div> 

     <div class="formItem"> 
      <label>Mother Name</label> 
      <input type="text" name="LKGMotherName" id="LKGMotherName" /> 
     </div> 

     <div class="formItem"> 
      <label>Email Address</label> 
      <input type="text" name="LKGEmail" id="LKGEmail" /> 
     </div> 

     <div class="formItem"> 
      <label>Mobile Number</label> 
      <input type="text" name="LKGContactNumber" id="LKGContactNumber" /> 
     </div> 

     <div class="formItem"> 
      <label>Residential Address</label> 
      <input type="text" name="LKGAddress" id="LKGAddress" /> 
     </div> 

     <div class="formItem"> 
      <label>City</label> 
      <input type="text" name="LKGCity" id="LKGCity" value="Bangalore" /> 
     </div> 

     <div class="formItem"> 
      <label>Pin Code</label> 
      <input type="text" name="LKGPincode" id="LKGPincode" /> 
     </div> 
      <label>Parent is an Alumnus (old Boy) of this school </label> &nbsp&nbsp 
     <input type="checkbox" name="LKGAlumnus" value="Yes" id="LKGAlmnus" >Yes &nbsp&nbsp 
      <input type="checkbox" name="LKGAlumnus" value="No" id="LKGAlmnus" >No &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 

     <div class="formItem"> 
      <input type="hidden" name="formType" id="formType" value="LKG" /> 
      <input type="button" name="LKGSubmit" id="LKGSubmit" value="Submit" class="formButton" /> 
     </div> 

     </form> 

pdf.php

$studentName = ""; 
    $studentDOB = ""; 
    $parentName = ""; 
    $motherName = ""; 
    $email = ""; 
    $contact = ""; 
    $address = ""; 
    $city = ""; 
    $pincode = ""; 
    $Almnus=""; 
    $formType = ""; 

if($_REQUEST["formType"] == "LKG") { 
    $studentName = $_REQUEST["LKGStudentName"]; 
    $studentDOB = $_REQUEST["LKGStudentDOB"]; 
    $parentName = $_REQUEST["LKGParentName"]; 
    $motherName = $_REQUEST["LKGMotherName"]; 
    $email = $_REQUEST["LKGEmail"]; 
    $contact = $_REQUEST["LKGContactNumber"]; 
    $address = $_REQUEST["LKGAddress"]; 
    $city = $_REQUEST["LKGCity"]; 
    $pincode = $_REQUEST["LKGPincode"]; 
    $Almnus= $_REQUEST["LKGAlmnus"]; 

    $formType = " LKG"; 
} 






     $username = "root"; 
     $password = ""; 
     $hostname = "localhost"; 
     $db = "abc"; 

     //connection to the database 

    $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); 
     mysql_select_db($db,$dbhandle) or die('cannot select db'); 
     mysql_query("INSERT INTO admissions (studentname, parentname, emailaddress, phone, type, dob, mothername, address, city, pincode, almnus) 
    VALUES('$studentName', '$parentName', '$email', '$contact', '$formType', '$studentDOB', '$motherName', '$address', '$city', '$pincode', '$Almnus') ") or die(mysql_error()); 

     // add a page 
     $pdf->AddPage(); 
     $html = ' 
        <h1>&nbsp;</h1> 
        <h1>&nbsp;</h1> 
        <h1>&nbsp;</h1> 
        <h1>&nbsp;</h1> 
        <h1>&nbsp;</h1> 

    $html .= $formType; 

    $html .= '</h3> 
    <table cellspacing="0" cellpadding="50"> 
    <tr> 
    <td> 
    <table cellspacing="0" cellpadding="10"> 
    <tr> 
    <td colspan="2" bgcolor="#f1f1f1" color="#000" align="left" style="border: 1px solid #ccc;border-top: 1px solid #ccc;"><h4>Online Admission Form Details</h4></td> 
    </tr> 
    <tr> 
    <td width="30%" style="border: 1px solid #ccc;">Student Name</td> 
    <td width="70%" style="border: 1px solid #ccc;">'; 

     $html .= $studentName; 
     $html .= '</td> 
             </tr> 
    <tr> 
    <td width="30%" style="border: 1px solid #ccc;">Student Date of Birth</td> 
    <td width="70%" style="border: 1px solid #ccc;">'; 

     $html .= $studentDOB; 
     $html .= '</td> 
    </tr> 
    <tr> 
    <td style="border: 1px solid #ccc;">Parent Name</td> 
    <td style="border: 1px solid #ccc;">  
    $html .= $parentName; 
    $html .= '</td> 
    </tr> 
    <tr> 
    <td style="border: 1px solid #ccc;">Mother Name</td> 
    <td style="border: 1px solid #ccc;">'; 
     $html .= $motherName; 
     $html .= '</td> 
    </tr> 
    <tr> 
    <td style="border: 1px solid #ccc;">Email Address</td> 
    <td style="border: 1px solid #ccc;">'; 
     $html .= $email; 
     $html .= '</td> 
    </tr> 
    <tr> 
    <td style="border: 1px solid #ccc;">Contact Number</td> 
    <td style="border: 1px solid #ccc;">'; 
     $html .= $contact; 
     $html .= '</td> 
    </tr> 
    <tr> 
    <td style="border: 1px solid #ccc;">Residential Address</td> 
    <td style="border: 1px solid #ccc;">'; 

    style="border: 1px solid #ccc;">'; 
      $html .= $address; 
      $html .= '</td> 
    </tr> 
    <tr> 
    <td style="border: 1px solid #ccc;">City</td> 
    <td style="border: 1px solid #ccc;">'; 
      $html .= $city; 
      $html .= '</td> 
    </tr> 
    <tr> 
    <td style="border: 1px solid #ccc;">Pin Code</td> 
    <td style="border: 1px solid #ccc;">'; 
      $html .= $pincode; 
      $html .= '</td> 
    </tr> 

    <tr> 
    <td style="border: 1px solid #ccc;">Almnus</td> 
    <td style="border: 1px solid #ccc;">'; 
      $html .= $Almnus; 
      $html .= '</td> 
     </tr> 
     </table> 
     </td> 
     </tr> 
     </table> 
+0

你搜索解决方案? http://stackoverflow.com/search?q=%5Bphp%5D+checkboxes – mplungjan

+0

亚..但我没有得到什么错我的代码.. – user3286350

+2

也许是在这种情况下使用单选按钮更好?您可以使用一个复选框来指示是/否,或者使用单选按钮提供两种选择。 –

回答

-1

你不能对chekboxs相同ID和名称(你不能在同一个页面中有两个相似的ID或名称) 所以命名他们是这样的:

name = "LKGAlumnusYes" id = "LKGAlumnusYes". 
name = "LKGAlumnusNo" id = "LKGAlumnusNo". 

希望能帮到你。

+0

我试过了它..问题是与PHP ..我应该如何得到值在PHP中。检查我的php代码如何在php中添加php – user3286350

+0

当然,你可以有相同的名称,但是你需要相应地对待它们。 – mplungjan

+0

尝试使用单选按钮。它更适合您的情况 –

0

你有一个错字导致名称不匹配:LKGAlumnus,然后LKGAlmnus。第一个有两个,第二个只有一个。

<input type="checkbox" name="LKGAlumnus" value="Yes" id="LKGAlmnus" >Yes &nbsp&nbsp 
     <input type="checkbox" name="LKGAlumnus" value="No" id="LKGAlmnus" >No &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 

//... 
$Almnus= $_REQUEST["LKGAlmnus"]; 
+0

名称被发送到服务器,所以它是他需要匹配的名称!也使它成为收音机,它会更好地工作 – mplungjan

0

您应该使用单选按钮代替的复选框,如果你想获得任一选项:

HTML:

<input type="radio" name="LKGAlumnus" value="Yes" id="LKGAlmnusYes" >Yes</input> 
<input type="radio" name="LKGAlumnus" value="No" id="LKGAlmnusNo" >No </input> 

PHP:

$Almnus= $_POST["LKGAlumnus"]; 
+0

这基本上是一个评论的概要,你没有使ID独特,所以你没有阅读所有正确的 – mplungjan

+0

哦,我没有注意到它已经存在.. – 2014-02-08 06:08:51

+0

修复了你的ID。 – mplungjan

1

因为它似乎没有一个答复者可以在一个答案中得到完整的答案,我想我必须这样做。

问题:

  1. 的PHP是用的不是名称标识访问请求
  2. 您可以使用两个复选框,而不是更合乎逻辑ONE复选框或两个无线电
  3. 的ID必须是唯一的

这里是PHP,你需要

$Almnus= $_REQUEST["LKGAlumnus"]; // watch the spelling, Get the NAME 

这里是一个复选框

<input type="checkbox" name="LKGAlumnus" value="Yes" id="LKGAlumnusYes" /> 
<label for="LKGAlumnusYes">Alumnus?</label> 

这里的解决方案是用两个无线电

Alumnus?<input type="radio" name="LKGAlumnus" value="Yes" id="LKGAlumnusYes"/> 
<label for="LKGAlumnusYes">Yes</label> 
<input type="radio" name="LKGAlumnus" value="No" id="LKGAlumnusNo" /> 
<label for="LKGAlumnusNo">No</label>