我有一个表单,用户可以输入用户名并显示与其对应的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
}
?>
'成功:功能(数据){$( “#resulthere”)HTML(数据)。}' – mplungjan