0

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>
1
  • 1
    function checkUserData() { $username = $_POST['username']; } u mean something like that? Commented Apr 1, 2016 at 20:25

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget escape your string if you are working with mysql
1

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.

Comments

0

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

Comments

0

For checking all the posted form fields, you may use:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.