2016-04-01 52 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); 
}