Jquery 通过ajax发送特定的php变量

Jquery 通过ajax发送特定的php变量,jquery,ajax,Jquery,Ajax,我有很多项目,我想在使用ajax单击后发布具体的项目信息。通常我通过属性数据名、数据日期等来存储它的信息。我使用jquery通过.attr()来获取它。如果文本太长,或者如果每个项目都有一个文本数组{“Text1”、“Text2”、“Text3”…“Text20”},我不知道如何处理。我无法将其存储为属性。我不知道该把它放在哪里,或者如何&如何通过jQuery获得它 我的物品看起来很简单,就像这样 <div class="item"> <a data-name="item

我有很多项目,我想在使用ajax单击后发布具体的项目信息。通常我通过属性数据名、数据日期等来存储它的信息。我使用jquery通过
.attr()
来获取它。如果文本太长,或者如果每个项目都有一个文本数组
{“Text1”、“Text2”、“Text3”…“Text20”}
,我不知道如何处理。我无法将其存储为属性。我不知道该把它放在哪里,或者如何&如何通过jQuery获得它

我的物品看起来很简单,就像这样

 <div class="item">
  <a data-name="item1" data-available="1" data-color="blue">Item1</a>
 </div>
 <div class="item">
  <a data-name="item2" data-available="1" data-color="pink">Item2</a>
 </div>

项目1
项目2
这是我的jquery

 jQuery(".item a").on('click', function () {  
          var url = "/test.php";          
          var item_name = jQuery(this).attr("data-name");    

          jQuery("#display").html("<img id='loading_gif' src='/loading.gif'>");
          jQuery("#display").load(url,{item_name:item_name});  
          return false; 
    });
jQuery(“.item a”)。在('click',函数(){
var url=“/test.php”;
var item_name=jQuery(this).attr(“数据名”);
jQuery(“#display”).html(”;
jQuery(“#display”).load(url,{item_name:item_name});
返回false;
});

您可以使用ajax将json数据发送到php文件

jQuery(".item a").on('click', function () {
    var available = $(this).attr("data-available"),
        color = $(this).attr("data-color");

    $.ajax({
        url: 'url_to_your_phpfile.php',
        type: 'post',
        data: {
            avail: available,
            color: color
        },
        success: function (data){
            console.log(data); // do something 
        }
    });
}
在PHP文件中,只需使用
$\u POST['avail']
$\u POST['color']

编辑

由于您希望将数据从php访问到javascript,因此可以将其存储到javascript变量中

<?php 
    $products = array(
       array("id" => 1, "name" => "Item 1", "color" => "red",  "desc" => "some large text..."),
       array("id" => 2, "name" => "Item 2", "color" => "blue", "desc" => "some large text...")
    );
    echo '<script>var products = '.json_encode($products).'</script>';
    // and then display 
    foreach($products as $product):
      echo '<div class="item"><a data-id="'.$product->id.'"  data-color="'.$product->color.'">'.$product->name.'</a></div>';
    endforeach;

您可以使用ajax将json数据发送到php文件

jQuery(".item a").on('click', function () {
    var available = $(this).attr("data-available"),
        color = $(this).attr("data-color");

    $.ajax({
        url: 'url_to_your_phpfile.php',
        type: 'post',
        data: {
            avail: available,
            color: color
        },
        success: function (data){
            console.log(data); // do something 
        }
    });
}
在PHP文件中,只需使用
$\u POST['avail']
$\u POST['color']

编辑

由于您希望将数据从php访问到javascript,因此可以将其存储到javascript变量中

<?php 
    $products = array(
       array("id" => 1, "name" => "Item 1", "color" => "red",  "desc" => "some large text..."),
       array("id" => 2, "name" => "Item 2", "color" => "blue", "desc" => "some large text...")
    );
    echo '<script>var products = '.json_encode($products).'</script>';
    // and then display 
    foreach($products as $product):
      echo '<div class="item"><a data-id="'.$product->id.'"  data-color="'.$product->color.'">'.$product->name.'</a></div>';
    endforeach;

如果您的属性值太长,您可能需要一个
来存储该值如果您的属性值太长,您可能需要一个
来存储该值Hi dexterb,感谢您的回答,我的意思是我已经可以发布可用的日期了。。但是如果值太长或者是数组,我不知道如何存储它,然后通过jquery获得它,如果我也将它作为属性,那么我的每个标记都非常大。hi@Yoona,我在我的答案中添加了一个编辑,除了
id
或您使用的任何唯一标识符之外,您不必将它们作为每个
a
标记的属性。嗨,德克斯特,谢谢您的回答,我的意思是我已经可以发布可用的日期了。。但是如果值太长或者是数组,我不知道如何存储它,然后通过jquery获得它,如果我也将它作为属性,那么我的每个标记都非常大。hi@Yoona,我在我的答案中添加了一个编辑,除了
id
或您使用的任何唯一标识符之外,您不必将它们作为每个
a
标记的属性。