Yalnız istemci tarafı doğrulamaya güvenmeyin ve kesinlikle PHP "kirli" olarak tüm verileri ele gerekir iken, doğrudan formunu yayınlamaktan tarayıcıyı önleyebilirsiniz JavaScipt'i kullanarak başka bir yolu yoktur. Aksine formun yöntem ve eylem ayarlamak yerine, basitçe, bir XmlHttpResponse nesnesi oluşturmak için onun onsubmit işlevini tanımlamak POST yöntemini ayarlamak ve (sizin form.serialize veri seti) ve uygun POST isteği gönderebilirsiniz. PHP komut dosyası GET veya TALEP parametrelerini kabul eğer Veya, (sizin doğrulama sonra) uygun veri ile PHP sayfasına yönlendirmek için URL sorgu ve sadece ayarlayabilirsiniz window.location oluşturabilirsiniz.
EDIT - Here is my illustration - Bu Prototip en kullanır Form.serialize function.
<form id="my_form" onSubmit="return checkUsername();">
Username: <input type="text" name="username" id="username" />
</form>
<script type="text/javascript">
var xhr; // global XMLHttpRequest object
var formElem = $('my_form'); // our form element
function checkUsername() {
var formData = formElem.serialize();
sendPOSTRequest('http://mydomain.com/mypath/myscript.php', formData);
}
function sendPOSTRequest(toURL, sendData) {
xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xhr) {
alert('Cannot create XHR');
return false;
}
xhr.onreadystatechange = handleResponse;
xhr.open('POST', toURL, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", sendData.length);
xhr.setRequestHeader("Connection", "close");
xhr.send(sendData);
}
function handleResponse() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var result = xhr.responseText;
// result is now whatever content was returned by the PHP script
// do whatever you want with the result here
// for example, you might have the PHP return 'true' or some such thing, and then
// change window.location, or perhaps if it returns 'false' you put up an alert('No!')
// use your imagination, go nuts
} else {
alert('The script returned an error.');
}
}
}
</script>
XMLHttpRequest nesnesi yaratmak ve işlemek için biraz daha sofistike yolu vardır. Ben bazı öneriler daha sonra bir güncelleştirme sonrası olabilir.