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!

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面