php - Keyup AJAX/PHP/MYSQL 上的帖子 ID

标签 php mysql ajax

我有以下代码。目前它只提交 first_name 这正是我想要的,并且它在不刷新页面的情况下即时执行。

但是我需要通过隐藏的 id 字段在同一个 ajax 请求中解析一个 ID 号。

任何人都可以提出任何想法来帮助我吗?

提前致谢

代码:

<html>
<head>
<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "my_parse_file.php";
        var fn = document.getElementById("first_name").value;
    var vars = "firstname="+fn+;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables     in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("status").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
<input type="hidden" name="my_id" value="1234">
First Name: <input id="first_name" name="first_name" type="text" onkeyup="ajax_post();">  <br><br>
<div id="status"></div>
</body>
</html>

最佳答案

使用jquery

<!DOCTYPE html>
<html>
<head>
	<title>TITLE</title>
	<script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript"></script>
	<script type="text/javascript">
function ajax_post(){
	var $elementStatus = $('#status');
	$elementStatus.html('processing...');
	var url = 'my_parse_file.php';
	var data = { firstname : $('#first_name').val() };
	var success = function(responseText, textStatus, jqXHR) {
		if(jqXHR.status != 200)
			return;

		$elementStatus.html(responseText);
	};
	$.post(url, data, success);
};
	</script>
</head>
<body>
	<h2>Ajax Post to PHP and Get Return Data</h2>
	<input type="hidden" name="my_id" value="1234">
	First Name: <input id="first_name" name="first_name" type="text" onkeyup="ajax_post();">  <br><br>
	<div id="status"></div>
</body>
</html>

关于php - Keyup AJAX/PHP/MYSQL 上的帖子 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42394971/

相关文章:

php - 在 php 中从 mysql 查询创建二维数组

mysql - 我可以使用多个外键,其值来自 MySql 中的同一个表吗

PHP 头文件问题

php - 更新 laravel Controller 中的复选框

MySql查询: Select top 3 rows from table for each category

C# UWP - 如何将移动模拟器连接到本地 MySQL 数据库?

javascript - jasmine.js expect() 在异步回调中不起作用

javascript - 使用随机用户生成器使用 angular.js 创建配置文件

javascript - 通过循环的 JSON 响应

php - 如何将 mysql 查询转换为 laravel?