weixin_33708432 2015-02-01 04:19 采纳率: 0%
浏览 13

在JavaScript中传递值

I'm trying the tutorial from infotuts here: http://www.infotuts.com/ajax-table-add-edit-delete-rows-dynamically-jquery-php/

And there's a javascript like this:

 $(function(){
 $.ajax({
         url:"DbManipulate.php",
                  type:"POST",
                  data:"actionfunction=showData",
        cache: false,
        success: function(response){

          $('#demoajax').html(response);
          createInput();

        }

       });

Now I want to add a parameter so that the line: url:"DbManipulate.php" becomes url:"DbManipulate.php?q=[some value]

I tried to alter the script like this:

 var cat=2;

 $(function(){
 $.ajax({
         url:"DbManipulate.php?q="+cat.val(),
                  type:"POST",
                  data:"actionfunction=showData",
        cache: false,
        success: function(response){

          $('#demoajax').html(response);
          createInput();

        }

       });

But it doesn't work. The variable cat never gets into the function. How to pass the variable "cat" so that the DbManipulate.php file receives the $q variable and I can use it using $_GET?

Thank you

  • 写回答

1条回答 默认 最新

  • 游.程 2015-02-01 04:25
    关注

    Try simply this way to sent your data variable(cat) using GET Method

    var cat=2;
    $(function(){
     $.ajax({
             url:"DbManipulate.php",
             type:"GET",
             data:{actionfunction:showData,cat:cat},
             cache: false,
             success: function(response){
              console.log(response);
              $('#demoajax').html(response);
              createInput();
            }
           });
    
     // in DbManipulate.php, try to catch cat using $_GET like this
      $cat=$_GET['cat'];
     //do further processing
    

    EDIT

    cat=2;
    url="DbManipulate.php";
    function yourFunc(cat,url){
    $.ajax({
        type: "GET",
        url: url+'?q='+cat,
        dataType: "json",
        cache: false,
        success: function (response) {
            $('#demoajax').html(response);
                  createInput();
        }
    });
    }
    //in DbManipulate.php 
     $cat=$_GET['q'];
    

    More Info:http://api.jquery.com/jquery.ajax/

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度