ユーザーがユーザー名を入力できるフォームがあり、それに対応するIDが表示されます。私はAJAXを使ってデータを提出し、ページを更新せずに結果(ID)を表示しています。AJAXサブミットの結果が表示されない
コンソールにエラーは表示されませんが、フォームの送信後にIDが表示されません。
HTML:
<form method="post" id="form">
<input type="text" id="username">
<input type="submit">
<form>
<div id="resulthere"></div> <!-- where the ID is supposed to be displayed -->
AJAX/jQueryの:
<script>
$(document).ready(function(){
$("form#form").submit(function(event) {
event.preventDefault();
var username = $("#username").val();
$.ajax({
type: "POST",
url: "result.php",
data: "username=" + username,
success: $("#resulthere").html(event)
});
});
});
</script>
PHP(results.php):
<?php
if(!empty($_POST['username'])) { // checks to make sure the username isn't empty
$json = file_get_contents('http://example.com?username='.$_POST['username']);
$json2 = json_decode($json);
$id = $json2->ID; // gets the ID
echo $id; // displays the ID
}
?>
'成功へお願いします? – mplungjan