1

I have a problem in my AJAX . My javascript file is unable to send data to the PHP file. My javascript file :

function updateTile(tile_no){

    var ajaxRequest;

    var color = document.getElementById("color_"+tile_no).value;
    //alert($color);
    var img_path = document.getElementById("url_"+tile_no).value;
    //alert(url);
    var title = document.getElementById("title_"+tile_no).value;

    try{

    ajaxRequest = new XMLHttpRequest();
    } catch (e){

        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){

                alert("Browser Error !");
                return false;
            }
        }
    }

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){


            document.getElementById("box_tile"+tile_no).innerHTML = ajaxRequest.responseText;
        }
        else
        {
            document.getElementById("box_tile"+tile_no).innerHTML = '<img src=ajax-loader.gif /> <br /> ';
        }
    }

    var url = "update.php?color="+color+"&url="+img_path+"&img_path="+title;

    alert(url);

    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null); 
}

The alert(url); shows update.php?color=#444444&url=http://somesite/image.jpg&img_path=Tile 1. My update.php file :

<?php


    $color_code = $_GET['color'];

    $img_url = $_GET['img_path'];

    $title = $_GET['title'];

    echo 'Color Code :'.$color_code;
    echo '<br />Image Path :'.$img_url;
    echo '<br />Title :'.$title;

/*
echo '<div style="background:'.$color_code.';width:100%;height:100%;vertical-align:bottom;color:#f8f8f8;font-family:Trebuchet MS;"><img src="'.$img_url.'" /><span>'.$title.'</span></div>';
*/  
?>

But the response from the PHP shows empty results !

Color Code :
Image Path :
Title :
1
  • Use url encoding for sending GET data. Or else use POST method. Commented Dec 17, 2012 at 5:08

3 Answers 3

1

You should encode the image path as it is a url and the colour as it contains a # (use encodeURIComponent)

Your query string seems all messed up, you pass color, url and img_path but expect color, img_path and title

Also

var url = "update.php?color="+encodeURIComponent(color)+
          "&img_path="+encodeURIComponent(img_path)+"&title="+title;
Sign up to request clarification or add additional context in comments.

Comments

1

I'm pretty sure that the hash symbol in your colour variable is throwing it off.

Forget AJAX, go to your update.php directly with the above get variables and do a print_r($_GET) and you'll see that nothing is being passed into the $_GET variable.

Get rid of the hash and you're golden

That is to say, send the hex color code without the hash (#)

update.php?color=444444 instead of update.php?color=#444444

3 Comments

Yes..you are right HighParkCoder.. Why this hash symbol is not allowing to send data? Thanks .
Anything after a hash is ignored. It's also not necessary in your case.
Also disregard the comment below to interchange $_REQUEST with $_GET. That is bad advice. $_REQUEST will also return cookie variables which can really mess things up if you have a cookie variable that shares the same namespace as a $_GET variable and it will overwrite it. I learned that lesson the hard way! Very frustrating
0

Try post method because as u might know url has some limitation over passing character while post can send as much data as u want.

And one more thing try to use $_REQUEST instead of $_GET see either will work.

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.