以下是我的注册表格的验证功能。问题是如果用户输入了所有正确的数据,如何用SQL语句触发我的php函数将其插入到我的数据库中?
<script type="text/javascript">
function validate(){
var school_name = document.formValidation.school_name.value;
var mailing_address = document.formValidation.mailing_address.value;
var city = document.formValidation.city.value;
var state = document.formValidation.state.value;
var postcode = document.getElementById('postcode');
var courier_address = document.formValidation.courier_address.value;
var courier_city = document.formValidation.courier_city.value;
var courier_state = document.formValidation.courier_state.value;
var courier_postcode = document.getElementById('courier_postcode');
var phonenumber = document.getElementById('phonenumber');
var faxnumber = document.getElementById('faxnumber');
var email = document.getElementById('email');
var website = document.formValidation.website.value;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(school_name == "" || mailing_address == "" || city == "" || state == "" || postcode == "" || courier_address == "" || courier_city == "" || courier_state == "" || courier_postcode == "" || phonenumber == "" || faxnumber == "" || email == "" || website == ""){
alert("Please enter fields marked with an asterisk");
return false;
}
if (!/^[0-9]+$/.test(postcode.value)) {
alert("Please enter numbers only for postcode");
postcode.value = postcode.value.substring(0,postcode.value.length-1);
return false;
}
if (!/^[0-9]+$/.test(courier_postcode.value)) {
alert("Please enter numbers only for courier postcode");
courier_postcode.value = courier_postcode.value.substring(0,courier_postcode.value.length-1);
return false;
}
if (!filter.test(email.value)) {
alert("Please enter a valid email address");
email.focus;
return false;
}
else {
alert("Registration Success!");
<?php
?>
return true;
}
}
答案 0 :(得分:1)
Javascript在用户的浏览器上运行。 PHP在您的服务器上运行。您必须将该数据发送到服务器,在那里验证,然后插入数据库。
答案 1 :(得分:0)
接下来,您需要POST或GET操作将变量发送到服务器端的PHP脚本(此JavaScript在客户端执行)。 JQuery为它提供了一些非常好用的函数:.post()和.get()。
http://api.jquery.com/jQuery.post/ 和 http://api.jquery.com/jQuery.get/