2016-03-29 33 views
0

我已经在顶部提供了预标签,以便我可以看到什么值正在进行,当我点击ragister按钮时,所有值都是正确的,除了业余爱好,因为它给予“在“为什么这样,请让我知道,我是新来的PHP,请尽可能详细地解释我。问题在业余爱好,因为无法使用PHP获取选定的值

<?php 

$error_array = array(); 
$fname = $lname = $email = $dob = $Mchecked = $Fchecked = $hobbies =""; 

if(isset($_POST["sbt_save"])) 
{ 
echo '<pre>'; print_r($_POST); echo "</pre>"; 
    if($_POST['fname']=="") 
    {   
     $err ="Please Enter your first name"."<br>"; 
     array_push($error_array,$err); 
    } 
    else 
    { 
     $fname = test_input($_POST['fname']); 
    } 

    if($_POST['lname']=="") 
    { 
     $err ="Please Enter your last name"."<br>"; 
     array_push($error_array,$err); 
    } 
    else 
    { 
     $lname = test_input($_POST["lname"]); 
    } 

    if($_POST['email']=="") 
    {  
     $err ="Please Enter your email"."<br>"; 
     array_push($error_array,$err); 
    } 
    else 
    { 
     $email = test_input($_POST["email"]); 
    } 

    if($_POST['dob']=="") 
    { 
     $err ="Please Enter your date of birth"."<br>"; 
     array_push($error_array,$err); 
    } 
    else 
    { 
     $dob = test_input($_POST["dob"]); 
    } 

    if(!isset($_POST["gender"])) 
    { 
     $err ="Please select gender"."<br>"; 
     array_push($error_array,$err); 
    } 
    else 
    { 
     $gender = $_POST["gender"]; 
     if ($gender == "Male") 
     { 
      $Mchecked = "checked"; 
     } 
     else if ($gender == "Female") 
     { 
      $Fchecked = "checked"; 
     } 
    } 

    if(!isset($_POST['hobbies'])) 
    { 
     $err ="Please Enter your hobbies"."<br>"; 
     array_push($error_array,$err); 
    } 
    else 
    { 
     $hobbies = test_input($_POST['hobbies']); 
    } 

} 


function test_input($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 


?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Ragistration Form</title> 
<link rel="stylesheet" type="text/css" href="ragistration_form.css"> 
<script src="jquery-2.2.1.min.js"></script> 
<script> 
/* 
$(document).ready(function(event) 
{  

    $(".sbt_button").click(function(event) 
    {   
     var error_arr = [];    
     var email_value = $("#email").val();      
     var position_of_at = email_value.indexOf('@');   
     var position_of_dot = email_value.lastIndexOf('.'); 



     if($("#fname").val() == null || $("#fname").val() == "") 
     { 
      var err = "First Name"; 
      error_arr.push(err); 
     } 

     if($("#lname").val() == null || $("#lname").val() == "") 
     { 
      var err = "Last Name "; 
      error_arr.push(err); 
     } 
     if(position_of_at == -1 || position_of_dot == -1 || (position_of_at + 2) >= position_of_dot) 
     { 
      var err = "Email "; 
      error_arr.push(err); 
     } 

     if($("#dob").val() == null || $("#dob").val() == "") 
     { 
      var err = "Date of Birth "; 
      error_arr.push(err); 
     } 

     if(!$("input[type='radio']").is(":checked")) 
     { 
      var err = "Gender "; 
      error_arr.push(err); 
     } 

     if(!$("input[type='checkbox']").is(":checked")) 
     { 
      var err = "Hobbies "; 
      error_arr.push(err); 
     } 


     if(error_arr.length !=0) 
     { 
      event.preventDefault(); 
      alert(error_arr); 
     }   
    }); 
}); 
*/ 
</script> 
</head> 
<body> 
<form class="form" name="myForm" action="" method="post"> 
<table> 
<tr> 
<p class="heading">Ragistration Form</p> 
</tr> 

<?php 
if($error_array !="") 
{ 
    foreach($error_array as $value) 
    { 
     echo "<tr><td> ". $value. "</td></tr>"; 
    } 
} 
?> 

<tr> 
<td class="field_Name">First Name :<b style="color:red">*</b></td> 
<td><input type="text" name="fname" id="fname" class="inputfield_Name" /></td> 
</tr> 
<tr> 
<td class="field_Name">Last Name :<b style="color:red">*</b></td> 
<td><input type="text" name="lname" id="lname" class="inputfield_Name" /></td> 
</tr> 
<tr> 
<td class="field_Name">Email :<b style="color:red">*</b></td> 
<td><input type="text" name="email" id="email" class="inputfield_Name" /></td> 
</tr> 
<tr> 
<td class="field_Name">Date of Birth :<b style="color:red">*</b></td> 
<td><input type="date" name="dob" id="dob" class="inputfield_Name" /></td> 
</tr> 
<tr> 
<td class="field_Name">Gender :<b style="color:red">*</b></td> 
<td><input type="radio" name="gender" value="Male"class="inputfield_Name" <?php echo $Mchecked;?>/>Male 
    <input type="radio" name="gender" value="Female" <?php echo $Fchecked;?> />Female</td> 
</tr> 
<tr> 
<td class="field_Name">About Yourself :</td> 
<td><textarea name="abt" class="inputfield_Name"$></textarea></td> 
</tr> 
<tr> 
<td class="field_Name">Hobbies :<b style="color:red">*</b></td> 
<td><input name="hobbies" type="checkbox" id="hobbies" class="inputfield_Name" />Cricket 
    <input name="hobbies" type="checkbox" />Singing 
    <input name="hobbies" type="checkbox" />Travling</td> 
<tr> 
<td></td> 
<td> 
    <input name="hobbies" type="checkbox" class="inputfield_Name"/>Writing 
    <input name="hobbies" type="checkbox" />Teaching 
    <input name="hobbies" type="checkbox" />Driving 
</td> 
</tr> 
<tr> 
<td> 
</td> 
<td><input type="submit" value="Ragister" name="sbt_save" class="sbt_button"/></td> 
</td> 
</tr> 
</table> 
</form> 
</body> 
</html> 

回答

2

您需要添加值属性的复选框元素:

<tr> 
<td class="field_Name">Hobbies :<b style="color:red">*</b></td> 
<td><input name="hobbies" value="cricket" type="checkbox" id="hobbies" class="inputfield_Name" />Cricket 
    <input name="hobbies" value="singing" type="checkbox" />Singing 
    <input name="hobbies" value="travelling" type="checkbox" />Travling</td> 
<tr> 
<td></td> 
<td> 
    <input name="hobbies" value="writing" type="checkbox" class="inputfield_Name"/>Writing 
    <input name="hobbies" value="teaching" type="checkbox" />Teaching 
    <input name="hobbies" value="driving" type="checkbox" />Driving 
</td> 
</tr> 
+0

感谢名单,@Jamkelvl ......还有一两件事是有没有其他简单的方法验证在“性别”作为我”米无法理解这....在我的代码 – Shanky

+0

此链接显示一个类似于你的形式和讨论验证。也许这会有所帮助。 [PHP表单验证](http://www.w3schools.com/php/php_form_validation.asp) – lovermanthing

+0

您是否试图验证性别单选按钮是否已被选中?如果是这样,它看起来像你试图从jQuery推到一个PHP数组,这是不可能的。 PHP是服务器端语言,jQuery是客户端。如果你想覆盖那些没有启用javascript的用户,你只需要用jQuery或者PHP或者两者都进行验证。 –

相关问题