采用ajax异步方式,通过js获取form中所有input、select等组件的值,将这些值组成Json格式,通过异步的方式与服务器端进行交互,
一般将表单数据传送给服务器端,服务器端处理数据并返回结果信息等
<html>
<head>
</head>
<body>
<form id="register_form">
<input class='formVal' type="text" name="first_name" placeholder="FirstName">
<input class='formVal' type="text" name="last_name" placeholder="LastName">
<input type="submit" value="submit_now" onclick="myFunction(); return false;">
</form>
<script>
function myFunction()
{
var elements = document.getElementsByClassName("formVal");
var formData =new FormData();
for(var i=0; i<elements.length; i++)
{
formData.append(elements[i].name,elements[i].value);
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
alert(xmlHttp.responseText);
}
}
xmlHttp.open("post", "server.php");
xmlHttp.send(formData);
}
</script>
</body>
</html>
php
<?php
$firstName = $_POST["first_name"];
$lastName = $_POST["last_name"];
echo $firstName." ".$lastName;
?>