繁体   English   中英

将发布数据传递给php函数

[英]Passing post data to php function

我是这个网站和编程的新手。

<!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

我试图通过此功能从用户名和密码获取数据以进行处理。 我已经把它弄弄了几个小时,终于放弃了。 你能帮忙的话,我会很高兴。

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

?>  

</head>
<body>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>"onsubmit="return validateForm()">
Username: <input type="text" name="uname" id="username" /><br/>
Password: <input type="text" name="pwd" id="password" maxlength="6" /><br/>
<input type="submit" value="Login"/>
<input type="Reset" value="Reset"/>
</form>

</body>
</html>

您是否要返回$ uname?

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

我不太了解您要使用php做什么。 让我知道这是否解决了您的问题,我将详细说明我的答案。

我不知道您是否在帖子中丢失了某些内容,或者忘记将整个代码添加到其中。 但是您根本没有得到任何post变量。 因此,为了获取数据并验证您需要执行此操作

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

从您的帖子中还不清楚100%您打算做什么

我认为您确实使用了服务器变量$ _POST。

例:

<?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;
   }
?>

希望这可以帮助

要检查所有过帐的表单字段,可以使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM