0

Trying to insert a car image from one of my databases into my webpage

get_new_car_pictures:

$make = $_REQUEST['make'];
$model = $_REQUEST['model'];

$query = "SELECT * FROM database_new WHERE make = $make AND model = $model";
$car = $db->get_row($query);

$img = $car['img_url'];


echo "<img src=".$img." />";

ajax function:

  function insertImageAJAX(){
      return $.ajax({
         type: 'GET',
         url: '/ajax/get_new_car_pictures.php',
         data: "model=" + params.model + "&make=" + params.make,
         success: function(data){
            $("#car-image".html(data));
         }
     }
  });

car-image is the div where I want the image to appear

I have tried to follow Ajax tutuorials online but for whatever reason, this is not working...

user3399235
  • 247
  • 1
  • 2
  • 8
  • 1
    I don't see where you're assigning anything to `params`. Also, you should use an object for `data:`, like this: `data: {model:params.model, make: params.make},`. Doing it this way lets jQuery take care of ULR-encoding where necessary. – Jonathan M Jul 16 '14 at 19:27
  • 3
    There is a syntax error in `$("#car-image".html(data));`. Should be `$("#car-image").html(data);`. Try seeing if there are other errors in your javascript console. – Danny Jul 16 '14 at 19:29
  • $("#car-image").html(data); instead of what you have. – Rodrigo Jul 16 '14 at 19:30
  • @JonathanM I agree is simpler to provide object to data property, but works fine with proper serialized string also. Just throwing this in since it isn't a codebreaker in this case – charlietfl Jul 16 '14 at 19:33
  • 2
    Also, as written, you are open to SQL injection. This comic (http://xkcd.com/327/ ) illustrates the nature of the problem. Otherwise search for preventing SQL injection and make the necessary adjustments. – earth_tom Jul 16 '14 at 19:33
  • 1
    @user3399235 The very first place to start is in developer tools (F12) of browser. You need to check for javascript errors and there you can inspect on network tab exactly what is sent and received ( if anything) and request status – charlietfl Jul 16 '14 at 19:36
  • @charlietfl, yeah. Just trying to head off another "why didn't it work" question. I suspect the OP may not be familiar with serialization. And we don't know if it's a code breaker, since we don't know the values of the variables. – Jonathan M Jul 16 '14 at 19:37
  • sorry I assigned params correctly earlier in my code but forgot to add it here. – user3399235 Jul 16 '14 at 19:41
  • 1
    @Danny -- to be technically correct (the best kind of correct), `$("#car-image".html(data))` is not *syntactically* incorrect. It is a semantic error, because the String object will not have a function called html, but the parser will handle the line just fine. – Michael Lorton Jul 16 '14 at 20:47

3 Answers3

1

use concatenation on query and varchar has to be in single quotation

$query = "SELECT * FROM database_new WHERE make = '".$make."' AND model = '".$model."'";

and also fix js

function insertImageAJAX(){
  return $.ajax({
     type: 'GET',
     url: '/ajax/get_new_car_pictures.php',
     data: {'model':params.model, 'make': params.make},
     success: function(data){
        $("#car-image").html(data);
     }
 }
 });
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
Muhammad Ali
  • 1,992
  • 1
  • 13
  • 20
1

For one thing, you've misplaced a parenthesis:

$("#car-image".html(data));

should be

$("#car-image").html(data);
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
0

Try adding an error callback to take a closer look at what's going on. jQuery Ajax error handling, show custom exception messages

Also, I'd recommend setting the image's src attribute with the response instead of setting its HTML.

Let me know what the error says and we can go from there.

Community
  • 1
  • 1
Doug Johnson
  • 558
  • 3
  • 9