wowzuzz
wowzuzz

Reputation: 1388

Passing PHP variables to AJAX

I am trying my best to learn AJAX and passing these php variables through to another php file. I am struggling though with some code.

Here is my problem. I have a button when onClick that does a javascript function

<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.

$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. 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? Right now it is just giving me this on my source code.

Upvotes: 0

Views: 189

Answers (2)

JF Dion
JF Dion

Reputation: 4054

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)

Upvotes: 1

Cedrun
Cedrun

Reputation: 467

Missing <?php ?> no ?

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

Upvotes: 0

Related Questions