2ページあります。最初のページには、次のような 2 つのテキスト フォームがあります。
<form name="NameForm">
Name: <input type = "text" name = "uname">
Location: <input type = "text" name = "ulocation">
<button type="button" onClick="MakeRequest()"">Save</button>
</form>
次のように、javascript を使用してページ番号 2 に情報をプッシュします (これは上記のコードと同じページにあることに注意してください)。
<script>
function MakeRequest()
{
// get values
var uname = document.NameForm.uname.value;
var ulocation = document.NameForm.ulocation.value;
// validation stuff here that detects browser
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
var url = "location.php?uname=" + uname + "&ulocation=" + ulocation;
xmlhttp.open("POST", url, true);
xmlhttp.send();
}
</script>
したがって、問題はこれです。すべてのサーバー通信を行うページ上の php スクリプトが、この投稿要求からの変数を読み取り、データベースに格納していません (データベース内のアイテムを表示する他の get メソッドは正常に動作します)。
if (isset($_POST['uname']))
{
$name = $_POST['uname'];
$location = $_POST['ulocation']
}
次に、クエリは次のようになります
//$table and the other undefined variables are the names of my table & columns
$query = "INSERT INTO $table ($tablename, $tablelocation) VALUES ('$name', '$location')";
基本的に、私はそのクエリを機能させようとしています。If ステートメントを削除すると、$name はデータベースに保存されますが、$location には保存されません。
編集:追加するのを忘れていました
<div id="result">
</div>