1

I can't seem to get this form to work properly. My web scripting knowledge is pretty limited as I'm still a student. I searched and found this Post Self Form Validation and Submission in PHP which is the same assignment but this person had a different problem.

For some reason when I try to test out my page "> Name: shows up as my first line but the "> is the closing for the html form opening tag.

I also have this a .html because I want to submit it as one document and not have the PHP code in a separate .php document.

edit: @ Quentin - This is not related to the duplicate article that this was marked as. I'm not running an Apache server. That person solved his problem by restarting the XAMPP service I'm Windows 10 and cannot possibly have that service running.

I want it to run off one document, thus why I was using PHP_SELF. When I save the file as a .php it doesn't do anything but display the code.

I did inspect the code in Chrome, the only way I know how to debug it. And I couldn't find anything wrong with it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Assignment 6.2 PHP Validation</title>
</head>

<body>

<?php
// define variables and set to empty values
$nameErr = $addressErr = $phoneErr = $zipErr = "";
$name = $address = $phone = $zip = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
 if (empty($_POST["name"])) {
  $nameErr = "Name is required";
  } else {
  $name = test_input($_POST["name"]);
 // check if name only contains letters and whitespace
  if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed"; 
  }
 }
 //check name field
if (empty($_POST["address"])) {
$emailErr = "Address is required";
} 

// check phone number field
 if (empty($_POST['phone'])) {
 $phoneErr = "Phone number is required";
 } else {
 $phone = test_input($_POST['phone']);
if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $phone)) {
$phoneErr = "must be in ddd-ddd-dddd format";
}
}
//check zip code field
if (empty($_POST['zip'])) {
$zipErr = "Zip code is required";
} else {
$zip = test_input($_POST['zip']);
if(!preg_match("/^[0-9]{5}-[0-9]{4}$/", $)) {
 $zipErr = "Zip code must be in ddddd-dddd format";
}
}
?>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Address:
<input type="text" name="address">
<span class="error">* <?php echo $addressErr;?></span>
<br><br>
 Phone Number:
 <input type="text" name="phone">
 <span class="error"><?php echo $phoneErr;?></span>
 <br><br>
 Zip Code:
 <input type="text" name="zip">
 <span class="error"><?php echo $zipErr;?></span>

 <br><br>
 <input type="submit" name="submit" value="Submit"> 

 </form>


 </body>

</html>
Community
  • 1
  • 1
barrym07
  • 13
  • 4
  • Isn't this what teachers are paid to do? Help their students? This is a *school* assignment, correct? – mferly Apr 22 '16 at 19:27
  • your html is probably corrupted/bad somewhere. do a view-source in the browser to see what your script actually output, and look for html syntax errors. you're probably missing a `>` or whatever somewhere earlier in the code. – Marc B Apr 22 '16 at 19:29
  • .html files will not run PHP scripts, they have to be have a `.php` extension for Apache to call the PHP interpreter – RiggsFolly Apr 22 '16 at 19:31
  • you have two syntax errors. first if is not closed and `$` should be replace by `$zip` in regex. Also you file extension must be `.php` not `.html` to run php code – Alive to die - Anant Apr 22 '16 at 19:31
  • @Anant What does that matter, he is not running the PHP, that will be the subject of Question 2 – RiggsFolly Apr 22 '16 at 19:32
  • And your XHTML document won't pass. Any reason you're using an XHTML `doctype`? `` is a void element, and in XHTML, needs to be closed. – mferly Apr 22 '16 at 19:32
  • @Marcus assignment requirement is to use XHTML. – barrym07 Apr 22 '16 at 21:14
  • @Anant I'm not sure what you mean in regards to $zip, and when I save it as .php and open it up in a web browser it only displays code and not the form. – barrym07 Apr 22 '16 at 21:16

2 Answers2

2

If you have php anywhere in the code, it can't be saved as an .html file. Save it as a .php file and then Apache will call the PHP interpreter.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
stackunderflow
  • 422
  • 2
  • 5
  • 19
1

since you're saving the page as an .html extension, it's very likely that your php is not being processed. Try saving the page as an .php

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • I'm trying to run it within the same page and not two separate pages. When I save it as .php it just shows up as the code when I look at it in the web browser. – barrym07 Apr 22 '16 at 21:05
  • If this page is showing you raw php code in the browser, then it's most likely not being run from a web server with php installed on it. You can't just open a php page from your file system and open it in the browser like a regular html file. I agree that you will find the answer you are looking for [here](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – Jeff Puckett Apr 22 '16 at 21:09
  • I'm required to submit it as XHTML, I uploaded it to the schools server that hosts the assignments as .php and it must not be running php services because it won't display. – barrym07 Apr 22 '16 at 21:23
  • then you're probably not allowed to use php to complete the assignment. try searching for client side validation, in particular javascript will achieve your goals. – Jeff Puckett Apr 22 '16 at 21:28
  • Jeff that makes sense to me but here are the details: Create an XHTML form that collects a name, address, phone number, and zip code. The phone number must be input in the format of ddd-ddd-dddd and the zip code must be in the format of ddddd-dddd. Write a PHP script that checks the submitted information and verifies the format of the phone number and zip code. That is why I wrote the code like I did. – barrym07 Apr 22 '16 at 21:46
  • you should certainly contact your instructor to clarify the assignment guidelines because php code will not normally execute inside a file with an `html` extension. perhaps the server has been configured specially though. Have you tried using the `.xhtml` file extension per the assignment instructions? – Jeff Puckett Apr 22 '16 at 21:48