1

The following form shows an error on clicking the submit button if we enter the name only and submit.The error shown is

Fatal error: Call to undefined function test_input() in C:\wamp\www\web\new9.php on line 11

Can anyone find the problem in the below code.

      <?php
// define variables and set to empty values
$name1Err = $email1Err  =  "";
$name1 = $email1 =  "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
 if (empty($_POST["input_1"]))
    {$name1Err = "Your name is required. Just the first will do. ";}
  else
    {$name1 = test_input($_POST["input_1"]);
     if (!preg_match("/^[a-zA-Z ]*$/",$name1))
       {       $name1Err = "Only letters and white space allowed"; }
    }

 if (empty($_POST["input_12"]))
    {$email1= "";}
  else
    {$email1 = test_input($_POST["input_12"]);
    // check if e-mail address syntax is valid
     if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email1))
       {  $emailErr = "Invalid email format"; }
    }




function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
}
?>
        <form method='post' enctype='multipart/form-data' action=''>
                name<input name='input_1' type='text' value='<?php echo $name1;?>' tabindex='1' />
                <div class="validation_message"><?php echo $name1Err ?></div>

                email<input name='input_12'  type='email' value='<?php echo $email1;?>'/><br/>
                <textarea name='input_5' tabindex='9'   rows='10' cols='50'></textarea>

            <input type='submit' value='Submit' tabindex='25' />

    </form>


<?php
echo "<h2>Your Input:</h2>";
echo "Name:".$name1;
echo "<br>";
echo "Email:".$email1;
echo "<br>";
?>
1
  • just a side note, your function could be optimised, $data = htmlentities(trim($data)); return $data; Commented Jan 15, 2014 at 14:04

4 Answers 4

8

Move your function out of your conditional if statement

It should be like this...

<?php
// define variables and set to empty values
$name1Err = $email1Err  =  "";
$name1 = $email1 =  "";

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

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// .... your remaining code .......... !

From the PHP Docs...

When a function is defined in a conditional manner ... Its definition must be processed prior to being called.

Source

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

1 Comment

You may want to explain the reason for needing to do this.
1

In general, functions are parsed first and can therefore be used in any order.

echo foo();
function foo() {return "bar";}

The above works fine.

However, unlike some languages like JavaScript, PHP allows you to conditionally define functions. You might do something like this:

if( $something) {
    function foo() {echo "bar";}
}
else {
    function foo() {echo "fish";}
}
foo();

It's a bad thing to do (personally I'd prefer anonymous functions, or putting the conditional inside the function), but it's allowed.

However, doing this means that the functions can no longer be grabbed. They MUST be defined before they can be used. Going back to our first example:

if( true) {
    echo foo();
    function foo() {return "bar";}
}

This will fail.

Comments

1

try

<form method='post' enctype='multipart/form-data' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'>

source:w3schools

Comments

0

you need to define the function first then you can use it , i just found this but when i did with javascript it work totally fine

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.