我有一个包含几个关于用户的不同信息的表单:
<form action="" method="post">
<input type="text" id="input-name" placeholder="Name" name="nameD">
<input type="text" id="input-surname" placeholder="Surname" name="surname">
<input type="text" id="input-name" placeholder="City" name="city">
<input type="text" id="input-name" placeholder="Address" name="address">
<input type="text" id="input-name" placeholder="Zip Code" name="zipcode">
<input type="text" id="input-name" placeholder="State" name="stateR">
<input type="text" id="input-name" placeholder="VAT" name="vat">
<input type="text" id="input-name" placeholder="Fiscal Code" name="fiscalcode">
<input type="email" id="input-email" placeholder="Email address" name="email">
<input type="text" id="input-name" placeholder="Bank Account" name="bankaccount">
<input type="text" id="input-name" placeholder="Bank Name" name="bankname">
<input type="text" id="input-name" placeholder="Phone" name="phone">
<input type="submit" class="btn btn-primary" onclick="doCreate_User()" value="Create">通过单击create按钮,我调用Javascript函数"doCreate_User()“。
function doCreate_User(){
//Actually empty
}现在,我有一个PHP函数:
function create_user($name,$surname,...ect){
//here using INsert into TableX() Values();
} 用于在mysql表中插入从表单输入的变量。
它可能是一个合适的Javascript函数,可以将所有数据从表单(在HTML中,给定onclick触发的事件)传递给create_user函数。
提前感谢
发布于 2016-11-29 10:57:40
请按照以下步骤操作:
函数create_user() { var userData = $('userForm').serialize();var请求={“方法”:"POST","url“:"user.php",”数据“:userData,};$.ajax(请求) .success(handleSuccess);}函数handleSuccess(响应){console.log(”成功“);}
发布于 2016-11-29 09:52:41
我想你应该呆在页面上,而不是仅仅使用javascript触发的表单提交?我会用表单作为post数据向PHP发送AJAX请求。为此,我更喜欢使用Mootools的Jquery,这使它更容易一些。
发布于 2016-11-29 09:52:50
不能直接从javascript命令PHP,因为javascript在浏览器上运行,PHP在服务器上运行。您需要在某个地方提交表单,例如一个php文件,然后解析来自$_POST的输入(最好使用filter_input()函数,参见php.net中的手册 )。
https://stackoverflow.com/questions/40862628
复制相似问题