简体   繁体   English

将发布数据传递给php函数

[英]Passing post data to php function

I am new to this site and to programming. 我是这个网站和编程的新手。

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

I am trying to get the data up from the username and password to proccess through this function. 我试图通过此功能从用户名和密码获取数据以进行处理。 I have been messing around with it for hours now and have finally giving up. 我已经把它弄弄了几个小时,终于放弃了。 Any help would be nice. 你能帮忙的话,我会很高兴。

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>

Are you trying to return $uname? 您是否要返回$ uname?

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

I couldn't quite understand what you were trying to do with your php. 我不太了解您要使用php做什么。 Let me know if this solved your problem or not, and I will elaborate on my answer. 让我知道这是否解决了您的问题,我将详细说明我的答案。

I don't know if you are missing something in your post or you forgot to add the whole code to it. 我不知道您是否在帖子中丢失了某些内容,或者忘记将整个代码添加到其中。 But you are not getting any post variables at all. 但是您根本没有得到任何post变量。 So in order to get the data and validate you need to do this 因此,为了获取数据并验证您需要执行此操作

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

It is not 100% clear from your post what you are trying to do 从您的帖子中还不清楚100%您打算做什么

I think you have do use the server variable $_POST. 我认为您确实使用了服务器变量$ _POST。

Example: 例:

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

Hope this helps 希望这可以帮助

For checking all the posted form fields, you may use: 要检查所有过帐的表单字段,可以使用:

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

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

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