1

次のJavaScript関数があります

function ajax_runs3(value){
    var ajaxRequest;  // The variable that makes Ajax possible!
    try{
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }

    var runs3= value;
    ajaxRequest.open("POST","runs3.php"+ runs3, true);
    ajaxRequest.send(null); 
}

また、PHPファイル

<?php
$servername = "localhost";
$username = "USER";
$password = "PASS";
$dbname = "labi8575_inventory";
$conn = mysql_connect($servername, $username, $password);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('labi8575_inventory');  
$runs3 = $_POST["runs3"];
$sql = mysql_query("UPDATE demo SET runs3 = '$runs3'");
$retval = mysqli_query( $sql, $conn );
?>

問題は、var runs3 を javascript 関数から php ファイルに渡すことができないことです。次のトピック ( ajaxRequest.open を使用して変数を php に送信する) に従っても試しました ajaxRequest.open("POST", "runs3.php?variable="+runs3) または AjaxRequest.open("POST" , "runs3.php?myvar=runs3", true); しかし、私の場合はうまくいきません。私の場合、何が悪いのか知っていますか?ご関心をお寄せいただきありがとうございます。

4

2 に答える 2

2

POST リクエストはパラメータに URL を使用しません! in-url パラメータを使用する GET メソッドです...

解決:

var runs3= value;
ajaxRequest.open("POST","runs3.php", true); //We open the url
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //IMPORTANT!! We add this header to tell to PHP that it is a "form" which sent requesdt
ajaxRequest.send("value=" + encodeURIComponent(runs3)); //Then we send DATA HERE  (encodeURIComponent encodes data to prevents URL-specific characters (for example '&'))

そして、PHPでruns3値を取得します$_POST["value"]

これが「通常」の方法です。

しかし、より柔軟なリクエスト形式が必要な場合は、データを JSON として送信することもできます。

var runs3 = {"val" : value};
ajaxRequest.open("POST","runs3.php", true); //We open the url
ajaxRequest.setRequestHeader("Content-type", "application/json");
ajaxRequest.send(JSON.stringify(runs3));

PHP 側: (ここで説明: PHP を使用した JSON POST の読み取り):

$request = file_get_contents('php://input'); //raw request data
$object  = json_decode($request, true);      //we convert it to associative array, by JSON

print_r($object); //Should return Array[1] {  "val" => YOUR_VALUE};

「通常の」方法ではありませんが、送信するデータの柔軟性が向上します(文字列ではなく生データを送信するため:オブジェクト/配列...)

于 2016-04-12T08:49:11.857 に答える
0

これを試して。あなたの関数(ajax_runs3)

function ajax_runs3(value){
    var ajaxRequest;  // The variable that makes Ajax possible!
    try{
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }

    var runs3= value;
    //ajaxRequest.open("POST","runs3.php"+ runs3, true);
   // ajaxRequest.send(null); 

    var url = "runs3.php";
    var params = "runs3="+value;
    ajaxRequest.open("POST", url, true);

    //Send the proper header information along with the request
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.setRequestHeader("Content-length", params.length);
    ajaxRequest.setRequestHeader("Connection", "close");

    ajaxRequest.onreadystatechange = function() {//Call a function when the state changes.
        if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
            alert(ajaxRequest.responseText);
        }
    }
    ajaxRequest.send(params);
}
于 2016-04-12T08:56:52.383 に答える