0

I am trying to pass a variable from my ajax to my php script. I can't see to get it to work. It keeps on giving me NULL when I do a var_dump on the variable.

JQUERY:

$(document).ready(function() {
        $('.trigger').click(function() {
                var id  = $(this).prev('.set-id').val();
                $.ajax({
                        type: "POST",
                        url: "../modules/Slide_Show/slide_show.php",
                        data: id
                });
                LinkUpload(id);
                function LinkUpload(id){
                        $("#link-upload").dialog();
                }
        });
});
</script>

PHP:

                $id = $_POST['id'];
                $query = mysql_query("SELECT * FROM xcart_slideshow_slides where slideid='$id'")or die(mysql_error());
                $sli = mysql_fetch_array($query);
                $slide_id = $sli['slideid'];
                $link = $sli['link'];
                var_dump($id);

I need the $id variable to post so I can dynamically change the dialog box when the click function is activated.

EDIT:

So I have changed some of my coding:

Jquery:

$(document).ready(function() {
        $('.trigger').click(function() {
                var id  = $(this).prev('.set-id').val();
                $.post(
                        "slide-show-link.php",
                        { id: id },
                        function(data,status){alert("Data: " + data + "\nStatus: " + status); }
                );
                // alert(id);
                LinkUpload(id);
        });
                function LinkUpload(id){
                        $("#link-upload").dialog();
                }
});

I wanted to see if the data was in fact being passed so I threw an alert in the .post. This is the error I'm getting now:

I have tried passing plain text and echoing it back on the page but it fails. It is just not passing.

griff4594
  • 484
  • 3
  • 15
  • http://api.jquery.com/jQuery.ajax/ -- the `data` parameter needs to be either a plain JavaScript object or a query string -- name-value pairs in either case. You're passing the value without naming it. – Blazemonger May 15 '13 at 15:43
  • why do you have the LinkUpload function inside click event? – Cheluis May 15 '13 at 15:47
  • please dont use mysql_* functions those are deprecated see this http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 and check this for jquery ajax post http://stackoverflow.com/questions/5004233/jquery-ajax-post-example/14217926#14217926 – NullPoiиteя May 15 '13 at 15:48
  • I fixed the LinkUpload function being in the click event, but regardless it functions the same tho. I did put a name and value to the data parameter but still getting NULL result. – griff4594 May 15 '13 at 15:53
  • 1
    You are using user input `$_POST['id']` directly in an SQL query without any form of escaping, this is an SQL injection vulnerability you need to fix. – Anigel May 15 '13 at 15:53
  • 1
    I'll fix that. Thank Anigel. – griff4594 May 15 '13 at 15:55

1 Answers1

7

Try this -

$.ajax({
     type: "POST",
     url: "../modules/Slide_Show/slide_show.php",
     data: { id : id }
});
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • Tried that too and still gives a null result. – griff4594 May 15 '13 at 15:46
  • Are you sure this `$(this).prev('.set-id').val();` gives you proper value ? – Adil Shaikh May 15 '13 at 15:49
  • Yes. I have an alert set up and it does give the proper value. I have the alert set to come up at the same time as the dialog box, and it shows the correct value. – griff4594 May 15 '13 at 15:54
  • Try this --> `data: "id="+id` – Adil Shaikh May 15 '13 at 15:55
  • no go pXL. Still nothing....so I tried just posting plain text to the data field `data: { id: "test" }` and it doesn't dump either. – griff4594 May 15 '13 at 16:00
  • I could try to post the entire ajax to the same page as the php, but what would I put as the URL? – griff4594 May 15 '13 at 16:04
  • Are you sure `slide_show.php` is called , do you see anything in network tab on developer tools on your browser ? – Adil Shaikh May 15 '13 at 16:07
  • how can I get the data to post straight to the same page as the script? – griff4594 May 15 '13 at 16:12
  • simply put path of the same php in url `-->` `url: "../modules/Slide_Show/thispage.php",` – Adil Shaikh May 15 '13 at 16:14
  • This is driving me crazy. I have everything right but the path. I am just trying to point it at a php file in the same directory, but because of the software I'm having to use, the script won't point right. Stupid template system.... – griff4594 May 15 '13 at 17:41
  • since I am storing the id in the ajax in the trigger, would I have to place it called inside of the LinkUpload() function? – griff4594 May 15 '13 at 17:53
  • just define `var id` out of you click handler – Adil Shaikh May 15 '13 at 17:56
  • It is right now. I still get Null, I just think it is a path problem. I have tried all kinds of combinations. – griff4594 May 15 '13 at 18:45
  • I added a function to display why I cannot get this to work: `function(data,status){alert("Data: " + data + "\nStatus: " + status); }`. So this is what I'm getting when I process now: `Access denied for user 'apache'@'localhost' (using password: NO)` . Why would it need to post a username and password to ajax? – griff4594 May 15 '13 at 19:10
  • Just try doing this in your php `-->` `$id = $_POST['id'];echo $id;` and see what happens. – Adil Shaikh May 15 '13 at 19:18
  • doesn't show anything. It's like the data just isn't going to the php script. I have both the php script and the js files in the same directory, so there shouldn't be a path problem. – griff4594 May 15 '13 at 19:24
  • did you get that `Access denied` this time ? – Adil Shaikh May 15 '13 at 19:25
  • even if I put in plain text for the data, it gives me the same error. – griff4594 May 15 '13 at 19:33
  • I got it figured out. I was pretty new to ajax at the time...but I am using it all the time now. It was a problem with pathing and with the php script. – griff4594 Sep 23 '13 at 18:24