Javascript变量到PHP变量 - Post if语句不起作用

时间:2016-05-12 05:42:25

标签: javascript php jquery ajax post

我的if(isset($_POST['height'])){只是直接else{}这是什么原因?所以基本上它假设检测div的高度。将其发送回PHP,并将其与1进行比较,并根据产品回应不同的语句。我想保持它AJAX,以便我能够检测div的高度并将其发送到PHP if语句而无需刷新页面。如果你知道发生了什么,我将不胜感激。爸爸保佑。

PHP

       <?php

    if(isset($_POST['height'])){
        $solo_height = $_POST['height'];

        if ($solo_height > 1){
            //HERE YOU SHOULD ECHO BACK A RESPONSE TO THE AJAX CALL                  
            echo '<div class="view_full_post">View Full Post</div>';
            // WHAT DOES $this->output($var); DO??? I HAVE NO CLUE...
            // SO JUST ECHO THE DIV JUST FOR TESTING...
        }else{
            echo "The Height is Less than a Thousand... What do we do sir???";
        }
    }
?>

的Ajax

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
        (function ($) {
        $(document).ready(function(e) {
            view_height = $("#view_height").height();
            $.ajax({
                type: "POST",
                url: "index.php",  // POINTS TO THE PROCESSING FILE WE CREATED ABOVE
                data:{ height: view_height },
                success: function(data){
                    console.log(view_height);      // NOW YOU SHOULD BE ABLE TO GET A RESPONSE....
                    // UPDATE THE DIV THAT YOU WANTED TO...
                    // ==> HERE IS THE NEW UPDATE REFLECTING YOUR USE CASE:
                    // ==> ADD THE DIV RIGHT AT THE TOP OF THE BODY ELEMENT...
                },              

                error: function (jqXHR, textStatus, errorThrown) {
                    console.log('The following error occured: ' + textStatus, errorThrown);
                },

                complete: function (jqXHR, textStatus) {
                    console.log('The Request Round-Trip has successfully Completed... Happy Coding....');                   
                }
            });

        });
    })(jQuery);

        </script>

1 个答案:

答案 0 :(得分:0)

在同一目录中创建2个文件

file1.php

     <?php

    if(isset($_POST['height'])){
        $solo_height = $_POST['height'];

        if ($solo_height > 1){
            echo '<div class="view_full_post">View Full Post</div>';
        }else{
            echo "The Height is Less than a Thousand... What do we do sir???";
        }
    }
?>

index.php(带有html,body标签等)

.....
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
        $(document).ready(function(e) {
            view_height = $("#view_height").height();
            $.ajax({
                type: "POST",
                url: "file1.php",  // POINTS TO THE PROCESSING FILE WE CREATED ABOVE
                data:{ height: view_height },
                success: function(data){
                    console.log(view_height); 
                    $('body').append('<h1>'+data+'</h1>');
                },              

                error: function (jqXHR, textStatus, errorThrown) {
                    console.log('The following error occured: ' + textStatus, errorThrown);
                },

                complete: function (jqXHR, textStatus) {
                    console.log('The Request Round-Trip has successfully Completed... Happy Coding....');                   
                }
            });

        });
        </script>
....