douzhang7728 2015-03-04 16:04
浏览 75

使用jQuery Ajax将javascript变量发送到PHP脚本

I'm trying to send a JavaScript variable to a php script using ajax. This is my first time using ajax and I don't know where I went wrong. Here's my code

function selectcat(v) {
        $.ajax({

        type: "GET",
        url: "myurl.php",
        dataType: "script",
        data: { "selected_category" :  v}
    }).done(function() { 
    window.location.href = "http://mywebsite.com";

    });
        }

All help is appreciated

Here's the HTML

<ul class="cat">
<li class="opt" onclick="selectcat('option1')">option1</li>
<li class="opt" onclick="selectcat('option2')">Option 2</li>

</ul>

This is the ajax php file

<?php
session_start();
$ctgry = $_GET['selected_category'];
$_SESSION['select_cat'] = $ctgry;
?>
  • 写回答

4条回答

  • dongzhong8691 2015-03-04 16:21
    关注

    Hi this is what I do to call an ajax request You can post data or load a file using following code:

     $("#button click or any other event").click(function(){
    try
      {
        $.post("my php page address",
          {
             'Status':'6',
             'var one':$("#myid or .class").val().trim(),
             'var 2':'var 2'
         }, function(data){
               data=data.trim();
          //   alert(data);
    // this  data is data that the server sends back in case of ajax request you 
    //can send any type of data whether json or or json array or any other type 
    //of data
    
             });
        }
        catch(ex)
        {
            alert(ex);
        }
      });
    

    I hope this help!

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛