我有一個表單填寫,當你點擊提交按鈕時,調用一個js腳本,通過它調用一個php腳本來獲取結果並將它們顯示在用戶的表單中一個警告框。js需要的警報框才能正常工作
奇怪的是,如果我在我的js中有alert(「msg」)「命令(你可以在下面看到它)evertyhing工程很好,但如果我刪除它似乎沒有任何工作....任何想法爲什麼發生這種情況? 日Thnx
<form action="#" method="post">
<label for="userFirstName">Name </label>
<input name="userFirstName" id="userFirstName" type="text" placeholder="First Name" autofocus /><br>
<label for="userLastName">Surname </label>
<input name="userLastName" id="userLastName" type="text" placeholder="Surname" /><br>
<label for="phonenumber">Phone Number</label>
<input name="phonenumber" id="phonenumber" type="text" placeholder="Your Telephone Number(10 digits))" /><br>
<label for="email">Email Address</label>
<input name="email" id="email" type="text" placeholder="Your Email Address" /><br>
<button class="clearbutton" type="submit" value="Submit" onclick="checkInput(userFirstName.value,userLastName.value,phonenumber.value,email.value)">Add Partner</button>
JS腳本
//the first is the file and the second the value
function checkInput(name,surname,tel,email) {
//alert(name+" "+surname+" "+tel+" "+email);
xmlhttpURL=GetXmlHttpObjectURL();
if (xmlhttpURL==null) {
alert ("Your browser does not support AJAX!");
return;
}
var url = "AddPartnersDb.php";
url = url + "?n=" + name + "&l=" + surname + "&t=" + tel + "&m=" + email;
// Adds a parameter (q) to the URL with the content of the drop-down box
url = url + "&sid=" + Math.random();
// Adds a random number to prevent the server from using a cached file
xmlhttpURL.onload = stateChanged; // Each time the readyState property changes, the stateChanged() function will be executed
xmlhttpURL.open("GET",url,true); // Opens the XMLHTTP object with the given URL
xmlhttpURL.send(null);
alert("msg"); //<---------------------When i have this everything sees ok...
}
function stateChanged() {
if (xmlhttpURL.readyState==4) {
var res = xmlhttpURL.responseText;
//if(res.length>0)
alert(res);
}
}
function GetXmlHttpObjectURL() {
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
else { // code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
php腳本:
<?php
//database connection
$dbc = mysqli_connect('localhost','root','root','WTHw3') or die('Error connecting to MySQL server.');
//the values sent from the ajax script
$name = mysqli_real_escape_string($dbc,trim($_GET["n"]));
$surname = mysqli_real_escape_string($dbc,trim($_GET["l"]));
$email=mysqli_real_escape_string($dbc,trim($_GET["m"]));;
$tel=mysqli_real_escape_string($dbc,trim($_GET["t"]));
//a var to store the result(if any) and show the error to the admin
$result="";
if (!eregi ('[a-z||0-9]@[a-z||0-9].[a-z]', $email)) { //checks to make sure the email address is in a valid format
$result .= "Not Valid e-mail!\n";
}
if(strlen($tel) != 10){
$result .= "Telephone must be a numeric with 10 nums length!";
}elseif(!is_numeric($tel)) {
$result .= "Telephone must be a numeric with 10 nums length!!!";
}
//check for a validation error
if($result==""){
//query to be executed
//$query = "INSERT INTO Partners(partnerName,partnerSurname,partnerTel,partnerEmail) values ('$name','$surname','$tel','$email')";
//query execution
//$dbResult = mysqli_query($dbc,$query) or die('Error querying database.');
echo "wtf";
}else {
echo $result;
}
//close connection
mysqli_close($dbc);
?>
是否有一些原因,你不使用像jQuery的圖書館? –
完全同意@RobApodaca,如果它不只是一個小項目,你應該考慮使用JavaScript庫(jQuery非常棒!)。還可以看看您的電子郵件驗證碼:「[email protected]」和「me + [email protected]」也是有效的電子郵件地址。 – Roimer