2015-12-04 74 views
0

我剛剛從這個網站上看到了這些代碼,並且正在考慮將其實施到我的項目中,但是我無法在下一頁中獲得我的數據。我現在用的是test.php的作爲指數和submit.php作爲一個頁面,將捕獲後如何從提交表單中提取數據PHP驗證後的數據

這裏是代碼

test.php的

<!DOCTYPE HTML> 
<html> 
<head> 
<style> 
.error {color: #FF0000;} 
</style> 
</head> 
<body> 

<?php 
// Initialize variables and set to empty strings 
$firstName=$lastName=""; 
$firstNameErr=$lastNameErr=""; 

// Validate input and sanitize 
if ($_SERVER['REQUEST_METHOD']== "POST") { 
    $valid = true; //Your indicator for your condition, actually it depends on what you need. I am just used to this method. 

    if (empty($_POST["firstName"])) { 
     $firstNameErr = "First name is required"; 
     $valid = false; //false 
    } 
    else { 
     $firstName = test_input($_POST["firstName"]); 
    } 
    if (empty($_POST["lastName"])) { 
     $lastNameErr = "Last name is required"; 
     $valid = false; 
    } 
    else { 
     $lastName = test_input($_POST["lastName"]); 
    } 

    //if valid then redirect 
    if($valid){ 
    header('Location: submit.php'); 
    exit(); 
    } 
} 

// Sanitize data 
function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 

<h2>Find Customer</h2> 
<p><span class="error">* required</span></p> 
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post"> 
First Name: <input type="text" name="firstName" value="<?php echo $firstName; ?>"><span class="error">* <?php echo $firstNameErr; ?></span><br><br> 
Last Name: <input type="text" name="lastName" value="<?php echo $lastName; ?>"><span class="error">* <?php echo $lastNameErr; ?><br><br> 
<input type="submit"> 
</form> 

</body> 
</html> 

submit.php

<?php 
$test=$_POST['lastName']; 
echo $test; 

?>

+0

您要發佈帖子'test.php'的ð不'submit.php'。 –

回答

0

您的表單操作應該是您想要獲取表單提交的文件v alues。

改變這個<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post"><form action="submit.php" method="post">

+0

如果我會這樣做,我不能驗證表格之前發送到submit.php –

+0

把你的驗證和提交在一個頁面中,提交後驗證它,如果有任何錯誤返回並顯示它。 –

+0

或者你需要在這裏再次發送submit.php:-if($ valid){ header('Location:submit.php'); exit(); }如果你永遠不會將它發送到submit.php它將如何得到它? –