与jQuery阿贾克斯发送多个变量

与jQuery阿贾克斯发送多个变量

本文介绍了与jQuery阿贾克斯发送多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常规概述

大家好,我有4个跨度中值的动态输入但是对于这个问题的目的,我把一些价值。我想从跨度中获得的价值,使用Ajax的PHP文件发送给他们。如果他们满足的PHP文件,然后报警一些消息所设定的标准。这是我迄今为止..

Hi all, I have 4 spans in which values are inputted dynamically but for the purpose of this question i put in some values. I want to get the value from the spans, send them using ajax to a php file. If they meet the criteria set in the php file then the alert some message. This is what i have so far..

的HTML

这基本上包含了跨度及跨度内的一些数字,按钮下方有一个onclick事件来调用名为检查的JavaScript函数。

This basically contains the span and some numbers within the span, the button below has an onclick event to call a JavaScript function called check.

 <span id="first" name="first">40</span>
      <span id="second" name="second">50</span>
      <span id="third" name="third">30</span>
      <span id="fourth" name="fourth">40</span></center>
      <input type="button" id="button" name="button" onClick="check()"/>

的JavaSript

此基本上存储每个跨度字段中指定的变量的值,然后尝试使用AJAX,然后它会提醒在检索成功消息,同为一个失败消息的消息发送这些变量

This basically stores the value of each span field in a specified variable then tries to send these variable using ajax, then it alerts a message in retrieving a success message and same for a failed message.

 function check() {
      var    one = $('#first').val();
       var   two = $('#second').val();
       var  three = $('#third').val();
       var  four = $('#fourth').val();

    $.post("test.php",{ data : "one"+"&two"+"&three"+"&four" } ,function(data)
        {

         if (data=="yay") //for no input
         { alert("yay");
         }
         else
         {
         alert("nay");
         }

         }


        }

的PHP 这些都是条件跨度领域必须满足的值,则AJAX功能检索成功,从这里失败的信息。

The PHPThese are the conditions the values in the span fields have to meet, the ajax function retrieves the success and failed messages from here.

  $one = $_POST["first"];
        $two = $_POST["second"];
         $three =$_POST["third"];
         $four = $_POST["fourth"];
         if($one > 5)        {

         echo "yay";
                           }
     elseif($two > 10 )        {

         echo "yay";  }

             elseif($three > 15 )        {

         echo "yay";  }

             elseif($four > 20 )        {

         echo "yay";  }

           else{
               echo "nay";
           }

问题

在没有显得有点调试它确实是,PHP是得到任何值,这意味着该数据没有被正确传递。我不认为这是通过AJAX通过多个项目的方式。有谁知道我可以解决这一问题?在adance非常感谢。

After a bit of debugging it doesn't seem that the php is getting any value which means the data hasn't been passed properly. I don't think that's the way to pass multiple items through ajax. Does anyone know how i can fix this?Many Thanks in adance..

推荐答案

。员额 可以是参数映射到该请求一起发送:

The second argument of .post can be a map of parameters to send along with the request:

$.post("test.php", {
    first: one,
    second: two,
    third: three,
    fourth: four
}, function(data) {
    //Done!
});

在地图上的按键(如第一第二等)的名称与您会能够从你的PHP脚本访问的值。

The keys of the map (e.g. first, second etc.) are the names with which you'll be able to access the values from your PHP script.

这篇关于与jQuery阿贾克斯发送多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:48