2014-11-04 3 views
-1

다음과 같이 양식을 만들었지 만 PHP를 사용하여 사용자 입력의 유효성을 검사해야합니다. 보안 조치로 양식 제출의 유효성을 검사하기 위해 javascript/HTML 5 양식 검증에만 의존해서는 안됩니다. firstname, lastName 및 email이 필요하다는 것을 확인하십시오. 2. 입력 된 경우 나이를 확인하십시오. 3.이처럼 보이도록 그들이PHP를 사용하여 입력 양식의 유효성을 검사하는 방법

<!DOCTYPE html> 

<html> 
<head> 
    <title>Page Title</title> 
</head> 

<?php 
$firstName=""; 
$lastName=""; 
$email=""; 
$age=""; 
$website=""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["firstName"])) { 
     $firstName = "First name is required"; 
    } 
    else { 
     $firstName = test_input($_POST["firstName"]); 
    } 
    if (empty($_POST["lastName"])) { 
     $lastName = "Last name is required"; 
    } 
    else { 
     $lastName = test_input($_POST["lastName"]); 
    } 
    if (empty($_POST["email"])) { 
     $email = "Email is required"; 
    } 
    else { 
     $email = test_input($_POST["email"]); 
    } 
    if (is_numeric ($_POST["age"])) {} 
    else { $age ="Age must be numeric"; 
    } 

} 
echo $firstName; 
echo $lastName; 
echo $email; 
echo $age; 

?> 



<form action="." method="POST"> 
<input type="text" name="firstName" placeholder="*First Name" /><br> 
<input type="text" name="lastName" placeholder="*Last Name" /><br> 
<input type="text" name="email" placeholder="*Email" /><br> 
<input type="text" name="age" placeholder="Age" /><br> 
<input type="text" "name="website" placeholder="Website" /><br> 
<input type="submit" name="submit" value="Submit" /> 
</form> 
<body> 





</body> 
</html> 

유효 확인하기 위해 이메일 및 웹 사이트 항목을 확인 : enter image description here

+1

로 변경해야합니다. 질문에 당신의 숙제를 뻔뻔하게 베껴 두었습니까? – DanielX2010

+0

나는 주어진 모든 것이 완료 할 작업이 아니었다. 지금까지 양식을 작성하고 지금까지 코드를 작성했습니다 – kp2630

+0

어떻게 firstName, lastName 및 email이 필요한지 그리고 입력 한 경우 해당 나이의 유효성을 검사하고 유효성을 확인하기 위해 전자 메일 및 웹 사이트 항목의 유효성을 검사하는 방법 – kp2630

답변

-2

난 당신이 PHP_SELF를 사용하여 시도 할 수 있다고 생각합니다. 간단한 예 :

HTML은

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='POST'> 
    <input type='submit' name='submit'/> 
</form> 

PHP

<?php 
    if (isset($_POST['submit'])) { 
     echo "your code here"; 
    } 

?> 
+1

이'

는 두 가지 이유로 실패합니다. 편집 : 우리는 하나의'method = POST' 따옴표를 사용합니다. –

+0

method = POST는 여전히 따옴표없이 작동합니다. – FortMauris

+1

나를 믿으십시오. 특정 브라우저는 그 점을 좋아하지 않을 것입니다. –

0

그들에게

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["firstName"])) { 
     $error['firstName'] = "First name is required"; 
    } 
    else { 
     $firstName = test_input($_POST["firstName"]); 
    } 
    if (empty($_POST["lastName"])) { 
     $error['lastName'] = "Last name is required"; 
    } 
    else { 
     $lastName = test_input($_POST["lastName"]); 
    } 
    if (empty($_POST["email"])) { 
     $error['email'] = "Email is required"; 
    } 
    else { 
     $email = test_input($_POST["email"]); 
    } 
    if (is_numeric ($_POST["age"])) { 

    } 
    else { $error['age'] ="Age must be numeric"; 
    } 

} 


?> 

<html> 
<body> 

<form action="" method="POST"> 
    <?php echo $error['firstName'] = "First name is required".'<br/>';?> 
<input type="text" name="firstName" placeholder="*First Name" /><br> 
<?php echo $error['lastName'] = "First name is required".'<br/>';?> 
<input type="text" name="lastName" placeholder="*Last Name" /><br> 
<?php echo $error['email'].'<br/>'?> 
<input type="text" name="email" placeholder="*Email" /><br> 
<?php echo $error['age'].'<br/>'?> 
<input type="text" name="age" placeholder="Age" /><br> 
<input type="text" "name="website" placeholder="Website" /><br> 
<input type="submit" name="submit" value="Submit" /> 
</form> 






</body> 
</html> 
0을 표시이

힌트는 다음 배열에 저장하려고

+0

폼 위에 ''을 넣는다.)'action = "."'action = ""- 나는 도트가 실제로 무엇을하는지 모른다. –

+0

당신을 진심으로 환영합니다. 그러나 여전히'test_input()'함수가있어'치명적 오류 : 정의되지 않은 함수 호출 test_input()'경고가 나옵니다. 그 기능은 존재하지 않습니다. –

+0

그/그녀는 그/그녀가 그/그녀 자신 등 [코멘트 당] (http://stackoverflow.com/questions/26727485/how-to-validate-an-input-form-using)을 썼다 고 말하지 않았다. -fp # comment42043789_26727485) –

1

여기는 스크립트 라이브러리에있는 양식이며 필요에 맞게 수정할 수 있습니다.

이상하게도 test_input()이라는 기능이 있으며 달성하고자하는 것을 할 것입니다.

(!) 참고 : 롤 자신의 $myemail = "[email protected]";

<?php 
ob_start(); 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<style> 
.error {color: #FF0000;} 
h6 
{ 
    font-family: bookman old style; 
    font-size:20px; 
    text-align: center; 
    font-weight: normal; 
} 
h5 
{ 
    font-family: bookman old style; 
    font-size:15px; 
    text-align: center; 
    font-weight: normal; 
} 
</style> 

<?php 
$nameErr = $emailErr = $websiteErr = $commentErr = $categoryErr = ""; 
$name = $email = $comment = $website = $category = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["name"])) { 
    $nameErr = "Name is required"; 


$Err = 1; 


    } else { 
    $name = test_input($_POST["name"]); 
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 
     $nameErr = "Only letters and white space allowed"; 
    } 
    } 

    if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 

$Err = 1; 


    } else { 
    $email = test_input($_POST["email"]); 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $emailErr = "Invalid email format"; 

$Err = 1; 

// die(); 


    } 
    } 

    if (empty($_POST["website"])) { 
    $websiteErr = "URL is required"; 

$Err = 1; 

    } else { 
    $website = test_input($_POST["website"]); 
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { 
     $websiteErr = "Invalid URL"; 
    } 
    } 

    if (empty($_POST["comment"])) { 
//  $comment = ""; 

    $commentErr = "Comment is required"; 

$Err = 1; 

    } else { 
    $comment = test_input($_POST["comment"]); 
    } 
// if (empty($_POST["category"])) { 

    if ($_POST["category"] == "") { 
    $categoryErr = "Category is required"; 

$Err = 1; 


    } else { 
    $category = test_input($_POST["category"]); 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> 

<h6>Link Submission</h6> 
<h5><span class="error">* required field.</span> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    Name Of Site: <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> 
    URL: <input type="text" name="website" value="<?php echo $website;?>"> 
    <span class="error">* <?php echo $websiteErr;?></span> 
    <br><br> 
    Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><span class="error">* <br><?php echo $commentErr;?></span> 
    <br><br> 

Category Of Site: <select size="1" name="category"> 
    <option value="<?php echo $category;?>"> -- Please select -- </option>  
    <option>Arts</option>  
    <option>Business</option>  
    <option>Computers</option>  
    <option>Games</option>  
    <option>Health</option>  
    <option>Home</option>  
    <option>Kids and Teens</option>  
    <option>News</option>  
    <option>Recreation</option>  
    <option>Reference</option>  
    <option>Science</option>  
    <option>Shopping</option>  
    <option>Society</option>  
    <option>Sports</option>  
    <option>World</option> 

    </select><span class="error">* <?php echo $categoryErr;?></span> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
</h5> 


<?php 

if(isset($_POST['submit'])){ 

if ($Err != 1){ 

$myemail = "[email protected]"; 
$subject = "Link Submission"; 
$message = "Your Link Submission form has been submitted by: 
Website Name: $name 
E-mail: $email 
URL: $website 
Category: $category 
Description: 
$comment"; 

$headers = "From: ". $name . " <" . $email . ">\r\n"; 

mail($myemail, $subject, $message, $headers); 
// header('Location: submit_thanks.php'); 

echo "OK"; 

} 

} 
?> 
+0

@ Arif_suhail_123 감사합니다 :) –

관련 문제