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

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号