-3

I'm trying to create a form for a non-profit. I just can't seem to figure out what's wrong with this function I copied and pasted from W3Schools.

I've wrapped everything else in their own PHP tags and am still getting the error, so it's somewhere right here.

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

I'm sure you get this question just about every night but, I've been searching for hours and still can't figure it out. I wish there were a tool somewhere to find where you messed up.

EDIT

<?php
//define variables and set to empty values
$nameerr = $teamnameerr = $teamcaptainerr = $addresserr = $phoneerr = "";
$name = $teamname = $teamcaptain = $address = $phone = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameerr = "Name is required"; }
        else { 
        $name = test_input($_POST["name"]);
        }
    if (empty($_POST["teamname"])) {
        $teamnameerr = "Teamname is required"; }
        else { 
        $name = test_input($_POST["teamname"]);
        }
    if (empty($_POST["teamcaptain"])) {
        $teamcaptainerr = "Name is required"; }
        else { 
        $name = test_input($_POST["teamcaptain"]);
        }
    if (empty($_POST["address"])) {
        $addresserr = "Adress is required"; }
        else { 
        $name = test_input($_POST["address"]);
        }
    if (empty($_POST["phone"])) {
        $phoneerr = "Phone number is required"; }
        else { 
        $name = test_input($_POST["phone"]);
        }?><?php
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
  • 2
    There's nothing here that would give that error. Most likely you've got an unmatched quote somewhere further up your code. –  Aug 19 '14 at 04:03
  • The function is fine: http://3v4l.org/KYBUE. The problem is elsewhere. Show the rest of your code, and tell us what line the error is on. – Mark Miller Aug 19 '14 at 04:03
  • put all your codes so that we can debug – John Robertson Aug 19 '14 at 04:04
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – scrowler Aug 19 '14 at 04:07
  • Your error should tell you a line number. Go to that line, if you don't find a syntax error there, check the line before it that has PHP. Continue until you find it. – scrowler Aug 19 '14 at 04:08
  • `I wish there were a tool somewhere` <- there are so many. get a good PHP editor, it can point the syntax errors. – bansi Aug 19 '14 at 04:15
  • It shows me as line 37, I thought that the comment when I edited would show on here somewhere. Line 37 is the last line, so I'm missing something silly somewhere, Haven't closed something properly? – Dallas Price Aug 19 '14 at 04:26

1 Answers1

0

You have a missing closing brace } to go just before your first closing ?> tag.

This is to match the closing for:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

<?php
//define variables and set to empty values
$nameerr = $teamnameerr = $teamcaptainerr = $addresserr = $phoneerr = "";
$name = $teamname = $teamcaptain = $address = $phone = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameerr = "Name is required"; }
        else { 
        $name = test_input($_POST["name"]);
        }
    if (empty($_POST["teamname"])) {
        $teamnameerr = "Teamname is required"; }
        else { 
        $name = test_input($_POST["teamname"]);
        }
    if (empty($_POST["teamcaptain"])) {
        $teamcaptainerr = "Name is required"; }
        else { 
        $name = test_input($_POST["teamcaptain"]);
        }
    if (empty($_POST["address"])) {
        $addresserr = "Adress is required"; }
        else { 
        $name = test_input($_POST["address"]);
        }
    if (empty($_POST["phone"])) {
        $phoneerr = "Phone number is required"; }
        else { 
        $name = test_input($_POST["phone"]);
        }

} // <- this one was missing

?>

<?php
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

Footnotes:

You can get a copy of Notepad++ http://notepad-plus-plus.org/ which will help in pair matching.

It's a free source code editor and a good tool to use when writing code.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You're awesome! Can't thank you enough! So odd, why wouldn't that show up on line 30? – Dallas Price Aug 19 '14 at 04:34
  • @DallasPrice You're welcome. Is that the error number it gave you? – Funk Forty Niner Aug 19 '14 at 04:36
  • Said 37, the last line in Dreamweaver. Is Notepad++ better to use for coding? – Dallas Price Aug 19 '14 at 04:39
  • @DallasPrice We can close the question by ticking the checkmark, marking as accepted and solved. – Funk Forty Niner Aug 19 '14 at 04:40
  • @DallasPrice Better; well that's by personal taste really. I've been using it for a few years and am quite happy with it. Line 30 is where the last brace is located (in your original code), so PHP's trying to go further, then it finds the closing `?>` tag; so it figures the error is on that line. – Funk Forty Niner Aug 19 '14 at 04:41
  • Gotcha, is notebook++ the one with really nice color coding? – Dallas Price Aug 19 '14 at 04:46
  • @DallasPrice Yes, it has different colors for different tags, which is configurable. I left a link for it at the bottom of my answer. There are also plug-ins for it, which are listed on their website. – Funk Forty Niner Aug 19 '14 at 04:47
  • @DallasPrice Here's a few links for the plugins http://sourceforge.net/projects/npp-plugins/ and http://sourceforge.net/projects/notepad-plus/#add-ons-plugins - You can also Google "Notepad++ plugins" to further your research; enjoy! – Funk Forty Niner Aug 19 '14 at 04:52
  • Ugh, I had more on another account but, I couldn't remember the e-mail or password I used to sign up. So I can't without more messages. I guess I'm done with this topic anyway. I would like to know though, is it better to have ^this registration code run on the same page page that I'm working on or if it's better to keep them as two separate files. It'd be really nice to integrate it right into the page with include_once. I only work with php like once a year so it gets so confusing and my syntax gets rusty. – Dallas Price Aug 19 '14 at 06:40
  • @DallasPrice It's usually best to use seperate files. However, using include/require, you may encounter some issues. If something is out of scope for instance; but not impossible depending on what it is exactly you wish to be included. – Funk Forty Niner Aug 19 '14 at 06:43