-4

I'm doing an assignment in which I am required to use separate html and php files but am having trouble understanding how to validate my forms input as all examples I've found have both html and php in one file.

I've been following the example on w3schools:http://www.w3schools.com/php/php_form_validation.asp

My w3_example.html:

    <!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 

<?php
// define variables and set to empty values
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="w3_example.php"> 
   Name: <input type="text" name="name">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   Website: <input type="text" name="website">
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

</body>
</html>

My w3_example.php:

<!DOCTYPE HTML> 
<html>
<head>
</head>
<body> 

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
   }

   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
   }

   if (empty($_POST["website"])) {
     $website = "";
   } else {
     $website = test_input($_POST["website"]);
   }

   if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }

   if (empty($_POST["gender"])) {
     $genderErr = "Gender is required";
   } else {
     $gender = test_input($_POST["gender"]);
   }
}




echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;

?>
</body>
</html>

I now get an "Call to undefined function test_input()" error on this line $name = test_input($_POST["name"]); in w3_example.php

Pamela Keogh
  • 133
  • 1
  • 2
  • 10
  • instead of showing us a working code, could you show us what you have tried and what the error's are? – Carl0s1z Mar 20 '16 at 17:45
  • *It's when I try and split it into separate html and php files that I run into trouble"* - Being? – Funk Forty Niner Mar 20 '16 at 17:45
  • The action attribute of the form can be pointed to another PHP script. – frz3993 Mar 20 '16 at 17:46
  • Could you please just explain you problem. What do you mean by split it? Split what? And why? – Ali Farhoudi Mar 20 '16 at 17:48
  • C Travel, I've basically split the above into two files, example.html and example.php but as the action in the html file = "", I don't know how to call my example.php. – Pamela Keogh Mar 20 '16 at 17:49
  • `action = "file.php"`. – Funk Forty Niner Mar 20 '16 at 17:50
  • Fred -ii-, I get the following: Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server. If you think this is a server error, please contact the webmaster. Error 403 localhost Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.1 – Pamela Keogh Mar 20 '16 at 17:51
  • folder/file permissions/path and `action="file.php"` was an "example" not the actual file that you want to use, being example.php and check for lettercase when on a NIX system. `file.php` and `File.php` are two different animals. – Funk Forty Niner Mar 20 '16 at 17:52
  • Ah, I'm not that dumb! This is my code: action="w3_example.php" but when I click on submit, it just refreshes the page and displays the "Your input" whereas I want the form to still be displayed with the incorrectly filled fields highlighted – Pamela Keogh Mar 20 '16 at 18:02
  • I never or anyone did say you were "dumb". Seeing comments in the answer below, you're trying to run this as `file:///file.xxx` in your browser, rather than `http://localhost/file.xxx`. If you're going to want to run this off your own machine, you need to install a webserver and PHP/Apache. – Funk Forty Niner Mar 20 '16 at 18:31
  • possible duplicate of [PHP code is not being executed I can see it on source code of page](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page) – Funk Forty Niner Mar 20 '16 at 18:32

1 Answers1

4

Just a tip! Because you studying as I can imagine, always care about from where you study. w3schools doesn't have a good quality code. Use http://php.net/manual/en/tutorial.forms.php from example which is the official PHP documentation. Their sample codes, comments etc. are by far better than w3schools.

I checked again your sample code:

Create 2 files:

  1. index.html / index.php (in this example it doesn't care if it is .html or .php format.
  2. handler.php

You 1st file: index.html / index.php

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<h2>PHP Form Validation Example</h2>
<form method="post" action="handler.php">
   Name: <input type="text" name="name">
   <br><br>
   E-mail: <input type="text" name="email">
   <br><br>
   Website: <input type="text" name="website">
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Your 2nd file: handler.php

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $website = test_input($_POST["website"]);
   $comment = test_input($_POST["comment"]);
   $gender = test_input($_POST["gender"]);
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

This option will show your inputs in the form in a different page-URL.

That will work!

By the way, I really don't like that poor sample code. Please, use:

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if (isset($_POST["submit"])){
       $name = test_input($_POST["name"]);
       $email = test_input($_POST["email"]);
       $website = test_input($_POST["website"]);
       $comment = test_input($_POST["comment"]);
       $gender = test_input($_POST["gender"]);

        echo "<h2>Your Input:</h2>";
        echo $name;
        echo "<br>";
        echo $email;
        echo "<br>";
        echo $website;
        echo "<br>";
        echo $comment;
        echo "<br>";
        echo $gender;

    }

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

instead of

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $website = test_input($_POST["website"]);
   $comment = test_input($_POST["comment"]);
   $gender = test_input($_POST["gender"]);
}
Greg
  • 343
  • 3
  • 17
  • I've done this but I then lose my form – Pamela Keogh Mar 20 '16 at 17:55
  • what do you mean you are gonna lose your form ?? – Greg Mar 20 '16 at 17:55
  • let me edit my question – Greg Mar 20 '16 at 17:55
  • Sorry, when I hit submit, it just shows me the blank page with the heading Your input. I want it to highlight any fields not filled in before refreshing page – Pamela Keogh Mar 20 '16 at 18:00
  • you see a WSOD (White Screen of Death) ? your error log? – Greg Mar 20 '16 at 18:00
  • I've done exactly as you have done above. Don't understand your question your error log? When I click on submit I get "Your Input:" only – Pamela Keogh Mar 20 '16 at 18:07
  • Ah, sorry. I misunderstand your previous comment. I thought that you meant you have a blank white page only. – Greg Mar 20 '16 at 18:09
  • It is quite logic to have an answer : "Your Input" bacause these variables are not initialized at all. – Greg Mar 20 '16 at 18:10
  • All the following variables are declared on the myPhp.php file. $name; $email; $website; $comment; $gender . So, move these variebles to the myPhp.php as well. – Greg Mar 20 '16 at 18:12
  • If you want to debug it, probably these POST variables could work for you $_POST["name"]; $_POST["email"]; $_POST["website"]; $_POST["comment"]; $_POST["gender"]; – Greg Mar 20 '16 at 18:14
  • I hadn't the function test_input in the form part as you have, but now I get an undefined function error on the return. By the way, is there any reason you have both the files with php extensions? – Pamela Keogh Mar 20 '16 at 18:17
  • You can not run PHP inside a html or htm file. Unless you have set in your apache (for example) a php handler (http://stackoverflow.com/questions/6295141/server-not-parsing-html-as-php) – Greg Mar 20 '16 at 18:22
  • And you will probably need to have some code in the myForm.php that it would be PHP code. Your *test function* for example is php code! – Greg Mar 20 '16 at 18:23
  • By the way, you know that you problably need to redirect your user into the myForm.php page when he hits the submits and your "things" were done.. right?? – Greg Mar 20 '16 at 18:25
  • You will need at the bottom of the myPhp.php when the (isset($_POST["submit"])) { //code code , and header('Location: http://example.com/myForm.php'); } – Greg Mar 20 '16 at 18:25
  • As I can see, you added the source link. Thats good because I will solve your problem later ;) – Greg Mar 20 '16 at 18:29
  • Oh I'm getting even more confused now. Do I need to declare the variables in both the myform and myphp file? – Pamela Keogh Mar 20 '16 at 18:29
  • Nope. You have to understand that, IF you dont declare and use variables in php, they return null. This is the reason that these variables doenst show anything with the echo function. – Greg Mar 20 '16 at 18:31
  • I will check later the whole code and i will update my question with a solution to your problem. Till them, keep studying in variables and how to print variables from one file to another. – Greg Mar 20 '16 at 18:32
  • **By the way, feel free to upvote and mark it as an answer if it fits on your problem ;)** – Greg Mar 20 '16 at 21:13
  • Greg, I really appreciate all your help and the link for better code is great. The main thing I was trying to do was the validation of the form input fields and still be able to highlight the errors on the form but all that has been removed in your code. – Pamela Keogh Mar 20 '16 at 21:26
  • 1
    Pamela, I think that it is not possible to be honest if you want to have your php code in separate file. You are using POST actions. If you use GET actions , or maybe GET actions inside the handler.php file, you can get the errors. But even you have send the variables with a GET to your url youj have to write PHP code into the index.php – Greg Mar 20 '16 at 21:54
  • Are you sure that your excersise says that you must not have PHP code into the index.html ?? – Greg Mar 20 '16 at 21:55
  • They is another option, if you include the hanlder.php file into the index.php and create a function printMyInputs() and call it on index.php... But still, you have to use some php code inside the index file. – Greg Mar 20 '16 at 22:01