2016-04-01 184 views
0

我是這個網站的新手,也是編程。將post數據傳遞給php函數

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Login page</title> 

<script> //This is the client side form validation with Javascript 
//This code is executed on the client's browser 
function validateForm() 
{ 
    var x=document.getElementById("username").value; 
    if (x==null || x=="") //check that the name field is not blank 
    { 
     alert("Your username must be filled out"); 
     return false; 
    } 
    var y =document.getElementById("password").value; 
    if (y==null || y=="") //check that the project field is not blank 
    { 
     alert("Your password must be filled out"); 
     return false; 
    } 
} 
</script> 

<?php //This is the server side form validation with PHP 
//This code executes on the web server 
//Write your server side form validation code here 
//checks form is submitted 

我想從用戶名和密碼獲取數據通過此功能進行處理。我幾個小時一直在搞這個,終於放棄了。你能幫忙的話,我會很高興。

​​
+1

功能checkUserData(){$用戶名= $ _ POST [」用戶名'];你的意思是這樣的嗎? – David

回答

1

您是否試圖返回$ uname?

function checkUserData($inputData) { 
    return stripslashes(trim(htmlspecialchars($inputData))); 
} 
echo checkUserData($_POST['uname']); 

我不明白你想用你的php做什麼。讓我知道這是否解決了你的問題,我會詳細說明我的答案。

1

我不知道你是否在你的文章中丟失了某些東西,或者你忘了將整個代碼添加到它中。但是你根本沒有得到任何後期變量。因此,爲了獲取數據並驗證你需要做的這個

$username = $_POST['uname']; 
checkUserData($username); 

這是不是100%清楚你的文章你正在嘗試做

+0

感謝這有助於。 –

+0

如果你正在使用mysql,不要忘記轉義你的字符串 – ntheg0d

0

我認爲你確實使用了服務器變量$彥博。

例子:

<?php 
    //Check if request is POST 
    if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
     $username = checkUserData($_POST['uname']); 
     $password = checkUserData($_POST['pwd']); 
    } 

    function checkUserData($inputData) { 
     $inputData = htmlspecialchars($inputData); 
     $inputData = trim($inputData); 
     $inputData = stripslashes($inputData); 
     return $inputData; 
    } 
?> 

希望這有助於

0

對於檢查所有提交的表單字段,你可以使用:

foreach($_POST as $key => $val) 
{ 
    checkUserData($val); 
}