1
我有帶輸入字段的html表單。表單中的數據將通過php中的ajax函數發送出去,並且php以html形式返回結果。 雖然並不是所有的字段都被php執行並返回結果。 如果所有的字段都填寫不到的結果從PHP。未填寫必填字段,執行php
任何人都可以幫助我解決問題嗎?
在此先感謝
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<div id="inputform">
<form>
Name:<br>
<input type="text" id="firstname" required><br>
Surname:<br>
<input type="text" id="lastname" required ><br>
<input type="submit" value="submit" onclick='ajax()'>
<div id="ajaxDiv"></div>
</form>
</div>
的JavaScript
function ajax() {
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4) {
var answer = document.getElementById('ajaxDiv');
answer.innerHTML = ajaxRequest.responseText;
}
};
var firstname = document.getElementById('firstname').value;
var lastname = document.getElementById('lastname').value;
var queryString = "?firstname=" + firstname + "&lastname=" + lastname;
ajaxRequest.open("GET", "test.php" + queryString, true);
ajaxRequest.send(null);}
PHP
<?php
$firstname=$_GET['firstname'];
$lastname=$_GET['lastname'];
echo $firstname;
?>
感謝快速回答。 沒有
你想如何發送數據到服務器?異步或?如果是異步的,那麼你應該使用ajax並移除form標籤,否則你需要移除ajax並使用form標籤'method =「get」'。 – Vusal