最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - console.log in php backend - Stack Overflow

programmeradmin5浏览0评论
  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 plete 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>"); 
  } 
} 
  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 plete 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>"); 
  } 
} 
Share Improve this question edited Jan 7, 2016 at 3:15 Donnie Gallocanta Jr. asked Jan 7, 2016 at 1:37 Donnie Gallocanta Jr.Donnie Gallocanta Jr. 291 gold badge1 silver badge10 bronze badges 12
  • So is the problem you're having that your console.log isn't firing when you load it into the browser with PHP? – Nick Zuber Commented Jan 7, 2016 at 1:40
  • Perhaps input the console.log argument as String? echo '<script>console.log("'.$_POST['name'].'");</script>'; – E. Sundin Commented Jan 7, 2016 at 1:41
  • not actually load the php.. but when I submitting a form. – Donnie Gallocanta Jr. Commented Jan 7, 2016 at 1:41
  • <script>console.log(".$_POST['name'].");</script> is working when I place in a index.html but my problem is when I place this into the target of a form – Donnie Gallocanta Jr. Commented Jan 7, 2016 at 1:43
  • @DonnieGallocantaJr. If you place the code directly in HTML it should work. I imagine it would not work within PHP because of the missing " around the String argument. In my previous ment I've included the necessary ". – E. Sundin Commented Jan 7, 2016 at 1:48
 |  Show 7 more ments

3 Answers 3

Reset to default 3

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>';

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.

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();
发布评论

评论列表(0)

  1. 暂无评论