要使用AJAX将空的表单数据发送给PHP,你可以按照以下步骤进行操作:
document.getElementById('myForm').addEventListener('submit', function(e) {
e.preventDefault(); // 阻止表单提交的默认行为
var formData = new FormData(this); // 创建一个FormData对象,用于存储表单数据
// 发送AJAX请求
var xhr = new XMLHttpRequest();
xhr.open('POST', 'your_php_file.php', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// 请求成功
console.log(xhr.responseText);
}
};
xhr.send(formData);
});
'success', 'message' => 'Form data received'];
echo json_encode($response);
?>
这是一个基本的示例,当表单提交时,它会将表单数据发送到PHP文件进行处理,并在控制台中显示响应。你可以根据自己的需求进行修改和扩展。