What exactly does the PHP function test_input () do?

I know what to vote for, but I need to ask. What does the code below mean? In particular, I am confused

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

Does this mean that if the user has not added his comments, then the field for which the value is "$ comment" will be empty? And if the HAS user inserted a comment, then the value of "$ comment" will be passed through the test_input function? I looked at w3schools, but I'm not wiser. It's unbelievable that the NO site even describes what test_input does, and believe me, I looked at dozens of sites. In fact, I'm not the only SO user who thinks test_input is a cryptic function.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo 'posted';
    if (empty($_POST["comment"])) {
        $comment = "";
    } else {
        $comment = test_input($_POST["comment"]);
    }
+4
source share
4 answers

test_input - , , PHP. , , , , , test_input.

http://www.w3schools.com/php/php_form_validation.asp , test_input,

<?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;
}
?>

, , , , , .

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
     echo 'posted';

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

 function add_date($comment)
 {
      return $comment . date('d-m-Y G:i:s');
 }

, Hello this is a comment, $comment Hello this is a comment 28-11-2014 16:00:00

, , test_input - , w3schools, , - ..

+7

test_input() - , w3schools:

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

:

<?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;
  }
?>
  • trim($data) ( , , ) ( PHP trim()).
  • stripslashes($data) () ( stripslashes() PHP).
  • htmlspecialchars($data) HTML.
    • ( < >, htmlspecialchars() &lt; &gt;).
+4

test_input() php, , :

function test_input($input){
   $output = $input." Oh, I'm updated."; // Whatever process you want to make
   return $output;
}

test_input() PHP .

, , test_input().

, , , (api) , .

Maybe you can post the whole script, maybe we can find out where test_input().

+2
source

There is no PHP function like test_input ().

This may be a custom feature on the w3schools website. Just go to the site you specify and run the sample code, and in the tryit editor check the source code once, you can get this function there.

Several times, sites do not display all functions, giving an explanation, so just link to the source code

0
source

All Articles