I'm working on lowering shopping cart abandonment rates for a client. I'd like to be able to post form data from individual input fields of a registration form to a php script, every time onblur fires.
The form fields would need to keep the value entered by the visitor and page shouldn't refresh so I think Ajax is the best solution. Any example code that does this would be appreciated.
Thank开发者_JAVA技巧s
Something like this would probably work for you:
<html>
<head>
<script type="text/javascript">
function sendData(obj) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
// Do stuff when script returns
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","script.php?" + obj.name + "=" + obj.value + "&t="+Math.random(),true);
xmlhttp.send();
}
</script>
</head>
<body>
Input 1 <input type="text" onblur="sendData(this)" name="input1" /><br />
Input 2 <input type="text" onblur="sendData(this)" name="input2" /><br />
Input 3 <input type="text" onblur="sendData(this)" name="input3" /><br />
</body>
</html>
The random number at the end of the request is to make sure the browser doesn't return a cached response instead of actually sending your data to the server.
精彩评论