简体   繁体   中英

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?

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

I think you have do use the server variable $_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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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