php后端的console.log

  I did also creating a different class. but still not working...

but if I place the  '<script>console.log("message here");</script>' will work..

//index.html
assuming that this is a complete code.

    <form action="post.php" method="post">
    <input type="text" name="name" id="name"/>
    <input type="submit" value="submit"/>
    </form>

//post.php

    <?PHP 
if(isset($_POST['name'])){
        echo "<script>console.log('".$_POST['name']."');</script>";
    }
?>

my problem is , i cant use console.log in submitting a form. but if I did this in redirection It will work..

The Function under my class is ...

public function console($data) { 
  if(is_array($data) || is_object($data)) { 
     echo("<script>console.log('".json_encode($data)."');</script>"); 
  } else {
     echo("<script>console.log('".$data."');</script>"); 
  } 
} 

It does not work within PHP because of the missing " around the String argument of console.log.

The output would've been

<script>console.log(name);</script>

instead of

<script>console.log("name");</script>

Solution

echo '<script>console.log("'.$_POST['name'].'");</script>';

You seem to be trying to debug the $_POST variable, If thats the case, then Please note, that console.log() is a frontend debugging tool used in javascript, and has nothing to do with php.

Few good way of checking the content of variables in php.

1. print_r

This will output the content of a variable that can be an array, object or string in a nice human readable format.

echo '<pre>';
print_r($_POST);
echo '</pre>';
die();

2. var_dump

This will output the content of the variable with extra respective details like datatypes and length.

var_dump($_POST);
die();

If you are trying to debug or see the value that was posted from the front end to back end then you can simply use the chrome inspector.

  1. Right click anywhere in browser and click inspect element.
  2. click on network tab.
  3. submit your form with desired values.
  4. on the left click on the post.php.
  5. Click on the headers on the right and scroll down to find Form Data.
  6. You will have all your post variables listed there with respective values.