0

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.

2
  • You need to echo the variable... Commented Nov 15, 2012 at 16:29
  • Doing a var dump of $y['imageURL'] gives me two filenames. string(24) "Infinity.jpg" string(43) "Diamond.jpg" Commented Nov 15, 2012 at 16:37

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

Missing <?php ?> no ?

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

4 Comments

There is php tags wrapped around it. I just did not post it.
Have you tried to use json encode with json_encode($y) ? And then to pass your json variable to ajax ??
No, I have not. I'll look at that.
Ok I think it would be good with json. And use json_decode() in your download.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.