简体   繁体   English

将PHP变量传递给AJAX

[英]Passing PHP variables to AJAX

I am trying my best to learn AJAX and passing these php variables through to another php file. 我正在努力学习AJAX,并将这些php变量传递给另一个php文件。 I am struggling though with some code. 我在用一些代码挣扎。

Here is my problem. 这是我的问题。 I have a button when onClick that does a javascript function onClick时有一个执行javascript功能的按钮

<input type='image' src='images/download-all.png' alt='Submit' onclick='download(".$y['imageURL'].")'>

The $y['imageURL'] is from code that pulls in results from a table. $ y ['imageURL']来自从表中提取结果的代码。

$sql1 = "SELECT * FROM digital_materials WHERE id = '".$x['itemID']."'";
$res1 = mysql_query($sql1);
$y = mysql_fetch_assoc($res1);

Because I am running a while loop before hand I get two arrays back, that both have imageURL keys. 因为我先运行了while循环,所以我得到了两个数组,它们都具有imageURL键。 I am partially using someone else's code here, so if there is something just point it out. 我在这里部分地使用了别人的代码,所以如果有什么需要指出。

Here is my download function. 这是我的下载功能。

function download(x)
    {
        $.ajax({
                url:'download.php',
                data:"image="+x,
                success:function(e){
                        alert("Hey, this worked.");
                },
                error:function(e, f, g){
                    alert("Error removing from cart, please try again. "+e+" : "+f+" : "+g);
                }
        });
    }

How do I pass both of these keys from the array to my php file to be processed? 如何将这两个键从数组传递到我的PHP文件进行处理? Right now it is just giving me this on my source code. 现在,这只是在我的源代码上给了我。

You can send arrays in query strings like that 您可以像这样在查询字符串中发送数组

image[first]=image.jpg&image[second]=image.png

You will then be able to access each image through PHP super variable $_GET or $_POST depending on the method set on your ajax request (I think it's $_GET by default with jQuery) 然后,您将能够通过PHP超变量$_GET$_POST访问每个图像,具体取决于对ajax请求设置的方法(我认为jQuery默认为$_GET

缺少<?php ?>否?

<input type='image' src='images/download-all.png' alt='Submit' onclick='download("<?php echo $y['imageURL']; ?>")'>

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

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