简体   繁体   English

从jQuery到PHP的变量加载到div中以通过echo写入

[英]variable from jQuery to PHP loaded inside a div to write by echo

I want to access a photo gallery by clicking on the gallery icon, and for this I use the following jQuery code: 我想通过单击图库图标来访问照片库,为此,我使用以下jQuery代码:

<script type="text/javascript">
  $(document).ready(function(){
      $('.galbt a').on('click', function(e) {
          var gallery = $(this).parent().find("p").text();
          //$("#section").load("gallery.php"); // I tried puting this here
          $.ajax
          ({
              url: "gallery.php",
              type: "POST",
              data: { "galeria" : gallery },
              success: function(){$("#section").load("gallery.php");} // or here
          })              
      });
  });
</script>

The php file takes the galery name, scann the directory and build a modal carousel by echoing it. php文件采用画廊名称,扫描目录并通过回显方式构建模式轮播。 PHP page loads but doesn't shows anything (I mean it shows a little html part that doesn't depend of the variable, but does not show the part that needs the variable to be shown). PHP页面加载但不显示任何内容(我的意思是,它显示了一个不依赖于变量的html部分,但未显示需要显示变量的部分)。 I tryed loading it first and then posting the variable, or first posting the variable and then loading it, but no change have been seen. 我尝试先加载它,然后发布变量,或者先发布变量,然后加载它,但是没有发现变化。

And another question I got is... there is any difference if I change the extension to html or php? 我得到的另一个问题是...如果将扩展名更改为html或php,会有什么区别? Because php file has a combined code and I tried loading it as both, like PHP or HTML and result is the same (nothing is shown). 因为php文件具有组合的代码,所以我尝试同时将其加载,例如PHP或HTML,结果是相同的(未显示任何内容)。

Thanks. 谢谢。

.load() method is another ajax call that you are doing and that is not dependent on your ajax call parameters. .load()方法是您正在执行的另一个ajax调用,它不依赖于您的ajax调用参数。 if you want output based on your parameter than you can try this code : 如果您想基于参数输出,则可以尝试以下代码:

 $.ajax ({
   url: "gallery.php",
   type: "POST",
   data: { "galeria" : gallery },
   success: function(data) {
      $("#section").html(data);
   }
 });

You are missing return part of ajax function(data) . 您缺少ajax 函数(数据)的返回部分。 Try this. 尝试这个。

$.ajax ({
       url: "gallery.php",
       type: "POST",
       data: { "galeria" : gallery },
       success: function(data) {
          $("#section").load(data);
       }
});

1) Your are using ajax inside ajax(load) which is not a problem but in your case you don't need. 1)您正在ajax(load)内使用ajax,这不是问题,但在您的情况下则不需要。

2) your $("#section").load("gallery.php"); 2)您的$("#section").load("gallery.php"); is not at all using the key you are passing in your outer ajax. 根本不使用您在外部ajax中传递的密钥。

first Option: 第一种选择:

use output(echo) from gallery.php in success : 使用output(echo)gallery.phpsuccess

 $.ajax
          ({
              url: "gallery.php",
              type: "POST",
              data: { "galeria" : gallery },
              success: function(data){
                         $("#section").html(data);//html content returned from gallery.php
                      } 
          })   

or second Option : 或第二个选项:

$('.galbt a').on('click', function(e) {
          var gallery = $(this).parent().find("p").text();
         $("#section").load("gallery.php?galeria="+gallery);//use key with $_GET and process.
      });      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM