0

I am setting up a function where clicking a button sets an image to a fullscreen background etc. It gets the image_name from mysql, I can alert the filename so I know it is returning the correct information.

If I hard code the image_name it works perfectly, but if I return it directly from the db via ajax call it doesn't. I am completely lost as to why? Can anyone suggest where I might be going wrong.

$.ajax({
            type: "POST",
            url: "ajax.php",
            data: { value: value, id: id },
            success: function(image_name){      
            $('.click').hide(); //This will hide the button specified in html
            $('body').css({"background-image":"url(../article_images/"+image_name+")",
            "background-repeat":"no-repeat",
            "background-position":"center center",
            "background-attachment":"fixed",
            "-webkit-background-size":"cover",
            "-moz-background-size":"cover",
            "-o-background-size":"cover",
            "background-size":"cover"});
            }
            });
1
  • What is value of the variable, are you sure you get the filename? You can check this with the Developertools in chrome. Commented Sep 24, 2015 at 6:38

2 Answers 2

1

The value in url() needs to be in quotes:

$('body').css({"background-image":'url("../article_images/'+image_name+'")',
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Spencer, just finished working it out myself.
0

Found a fix:

instead of using

echo "$image_name";

in the php and jquery

$('body').css({"background-image":"url(../article_images/"+image_name+")",

I had a lightbulb moment and tried:

echo "../article_images/$image_name";

in the php and jquery

$('body').css({"background-image":"url("+image_name+")",

Probably not the right or best way to do it but it works - Finally!

Comments

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.