theweebokid
theweebokid

Reputation: 21

I'm getting error Call to undefined function

Hi I'm new to php and testing some of the codes I'm getting an error of undefined function

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

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
	<input type="text" placeholder="type your full name" name="fullname">
	<input type="submit" name="submit" value="submit">
</form>

<?php
	$getName = "" ;
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		$getName = "hello ".test_input($_POST['fullname']);  //Fatal error: Call to undefined function test_input() in C:\xampp\htdocs\ctPHP.php on line 15
	  
	 function test_input($data) {
		  $data = trim($data);
		  $data = stripslashes($data);
		  $data = htmlspecialchars($data);
		  return $data;
	 }
	} 
	echo $getName;
?>

</body>
</html>

I was wondering how to solve this problem , I want to echo out the typed name in the text field , how to solve this one thanks

Upvotes: 0

Views: 4129

Answers (1)

RJParikh
RJParikh

Reputation: 4166

Just you need to echo after function created.

First create function test_input() then use it after declaration/defination.

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

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <input type="text" placeholder="type your full name" name="fullname">
    <input type="submit" name="submit" value="submit">
</form>

<?php
    $getName = "" ;
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
         function test_input($data) {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          return $data;
     }
        $getName = "hello ".test_input($_POST['fullname']);  //Fatal error: Call to undefined function test_input() in C:\xampp\htdocs\ctPHP.php on line 15


    } 
    echo $getName;
?>

</body>
</html>

Upvotes: 1

Related Questions