dpf7891 2018-03-27 11:21
浏览 52
已采纳

如果只是非空的话,如何通过Ajax发送php变量

So i have 4 php variables named $description,$hscode,$from,$to I wants to send these variables through a AJAX request.My incomplete attempt is like this:

function searchFilter(page_num) {
  page_num = page_num?page_num:0;

    $.ajax({
        type: 'POST',
        url: 'getData.php',
       data: { page : +page_num, "desc" : "<?php  print $description ?>","hscode" : "<?php  print $hscode ?>","from" : "<?php  print $from ?>","to" : "<?php $to ?>" },

        beforeSend: function () {
            $('.container-fluid').waitMe({
                   effect : 'stretch',
                   bg : 'rgba(255,255,255,0.7)'
                });
             },
        success: function (html) {
            $('#posts_content').html(html);
            $('.container-fluid').waitMe("hide");
        }
    });
}

The problem is, PHP variables are dynamic means they are not always set sometime only $description is set, sometime only $hscode is set ,sometime $description and $hscode only set,There are 6 combination of this type.Please help in this situation how can i send this request.

POSSIBLE Answer:

code is working fine but i am confused is it a right way to do it,Please correct me if i am wrong,my working code is :

function searchFilter(page_num) {
  page_num = page_num?page_num:0;

    $.ajax({
        type: 'POST',
        url: 'getData.php',
        data :  { page : +page_num, 
                  <?php if(isset($description)){ echo '"desc"'.' : '. "'$description'" . ',';} ?>
                  <?php if(isset($hscode)){ echo '"hscode"'.' : '. "'$hscode'" . ',';} ?>
                  <?php if(isset($from)){ echo '"from"'.' : '. "'$from'" . ',';} ?>
                  <?php if(isset($to)){ echo '"to"'.' : '. "'$to'" . ',';} ?> },


        beforeSend: function () {
            $('.container-fluid').waitMe({
                   effect : 'stretch',
                   bg : 'rgba(255,255,255,0.7)'
                });
             },
        success: function (html) {
            $('#posts_content').html(html);
            $('.container-fluid').waitMe("hide");
        }
    });
}
  • 写回答

2条回答 默认 最新

  • douju1968 2018-03-27 11:35
    关注

    First you can check your variables are set or not, if its set then on;y append those variables to ajax data. Try following code:

    function searchFilter(page_num) {
        page_num = page_num?page_num:0;
        send_data = {};
        send_data.page = +page_num;
        <?php if (isset($description)): ?>
        send_data.desc = "<?php echo $description ?>";
        <?php endif; ?>
        <?php if (isset($hscode)): ?>
        send_data.hscode = "<?php echo $hscode ?>";
        <?php endif; ?>
        <?php if (isset($from)): ?>
        send_data.from = "<?php echo $from ?>";
        <?php endif; ?>
    <?php if (isset($to)): ?>
    send_data.to = "<?php echo $to ?>";
    <?php endif; ?>
    
          $.ajax({
              type: 'POST',
              url: 'getData.php',
             data: send_data,
    
              beforeSend: function () {
                  $('.container-fluid').waitMe({
                         effect : 'stretch',
                         bg : 'rgba(255,255,255,0.7)'
                      });
                   },
              success: function (html) {
                  $('#posts_content').html(html);
                  $('.container-fluid').waitMe("hide");
              }
          });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?