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!

    评论

报告相同问题?

悬赏问题

  • ¥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里的文字?