doujiang2643 2016-05-12 05:42
浏览 62
已采纳

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

my if(isset($_POST['height'])){ is just going straight to else{} what is the reason for this?. so basically it's suppose to detect the height of a div. Send it back to the PHP where it is compared to 1 and echo different statements depending on the product. I am wanting to keep it AJAX so that I am able to do detect the height of a div and send it to a PHP if statement without having to refresh the page. if you have any idea what's going on it would be much appreciated. papa bless.

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条回答 默认 最新

  • du8980919 2016-05-12 06:08
    关注

    Create 2 files in the same directory

    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 (with the html, body tag,etc)

    .....
    <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>
    ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择