gpt4 book ai didi

javascript - 如何在不离开页面的情况下将表单插入mysql(javascript+html)

转载 作者:行者123 更新时间:2023-12-03 06:59:33 25 4
gpt4 key购买 nike

我正在尝试将新用户插入 mysql。我尝试过使用 jQuery,但它似乎不起作用。我尝试使用纯javascript,但结果是一样的。我点击按钮后没有任何反应。怎么了?

var regBtn = document.getElementById("regBtn");
regBtn.addEventListener("click", submitForm, false);

function submitForm() {

var acR = document.getElementById("ac2");
var pw1 = document.getElementById("pw1");
var shop = document.getElementById("shop");

var http = new XMLHttpRequest();
http.open("POST", "http://xyz.php", true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var params = "ac=" + acR + "&pw1="+pw1 "&shop="+ shop;
http.send(params);
http.onload = function() {
alert(http.responseText);
};
}

最佳答案

你的 JS 代码中有相当多的问题,我已经在这里整理它并在本地运行它到一个名为 xyz.php 的页面,这样 AJAX 调用就可以工作但您需要发布 PHP 代码才能获得有关数据库查询的任何帮助

    var regBtn = document.getElementById("regBtn");
regBtn.addEventListener("click", submitForm, false);

function submitForm() {

var acR = document.getElementById("ac2");
var pw1 = document.getElementById("pw1");

var http = new XMLHttpRequest();
// removed the http:// protocol, assuming you're going for a local AJAX call
http.open("POST", "xyz.php", true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// get values of the form fields, don't submit the full element
// also added the plus (+) character before the final pw1
var params = "ac=" + acR.value + "&pw1=" + pw1.value;
http.send(params);
http.onload = function() {
alert(http.responseText);
}
}

我附上了一张屏幕截图,显示 Chrome Dev Tools愉快地记录成功的 AJAX 请求

chrome dev tools successfully logging ajax calls

关于javascript - 如何在不离开页面的情况下将表单插入mysql(javascript+html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37121243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com