1

AJAX に問題があります。JavaScript ファイルがデータを PHP ファイルに送信できません。私のJavaScriptファイル:

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); 
}

alert(url);ショーupdate.php?color=#444444&url=http://somesite/image.jpg&img_path=Tile 1。_ 私のupdate.phpファイル:

<?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>';
*/  
?>

しかし、PHP からの応答には空の結果が表示されます。

Color Code :
Image Path :
Title :
4

3 に答える 3

1

画像パスは URL であり、色は含まれているのでエンコードする必要があります#( encodeURIComponentを使用) 。

クエリ文字列が すべて台無しになっているようcolorです。urlimg_pathcolorimg_pathtitle

また

var url = "update.php?color="+encodeURIComponent(color)+
          "&img_path="+encodeURIComponent(img_path)+"&title="+title;
于 2012-12-17T05:09:26.437 に答える
1

色変数のハッシュ記号がそれを捨てていると確信しています。

AJAX を忘れて、上記の get 変数を使用して直接 update.php に移動し、print_r($_GET) を実行すると、$_GET 変数に何も渡されていないことがわかります。

ハッシュを取り除くと、あなたはゴールデンです

つまり、ハッシュ (#) なしで 16 進数のカラー コードを送信します。

update.php?color=#444444 の代わりに update.php?color=444444

于 2012-12-17T05:10:20.070 に答える
0

postメソッドを試してみてください.urlには文字の受け渡しに制限があることを知っているかもしれませんが、postは必要なだけのデータを送信できます.

$_REQUESTそして、いずれかが機能するのを確認する代わりに、もう1つ使用してみて$_GETください。

于 2012-12-17T05:11:20.630 に答える