我想將數據從html表單發佈到php頁面。通過Ajax將表單數據發佈到php?
下面的形式:
<form onSubmit="return validEmail()" style="text-align:center; clear:both">
<input type="text" id="ms_firstName" name="ms_firstName" required placeholder="First Name" style="float:left;" >
<input type="text" id="ms_lastName" name="ms_lastName" required style="float:left; margin-left:20px;" placeholder="Last Name">
<input type="email" id="ms_email" name="ms_email" required placeholder="Corporate Email address" pattern="^.*(\[email protected]|\[email protected]).*$" oninvalid="this.setCustomValidity('Please enter your corporate email')" style="float:left; margin-top: 10px;">
<input type="password" id="ms_password" name="ms_password" required style="clear:right; margin-top: 10px; margin-left: 20px;" placeholder="Password" pattern=".{6,}">
<input type="submit" name="submit" style="alignment-adjust:central; margin-top:30px; clear:right;" ><br>
</form>
這裏是AJAX:
$.ajax({
url: "/ms_form_handler.php",
method:"POST",
data: "{'ms_firstName':'" + ms_firstName+ "', 'ms_lastName':'" + ms_lastName+ "', 'ms_email':'" + ms_email+ "', 'ms_password':'" + ms_password+ "'}",
success: function(){
alert('form was submitted succesfully');
}
});
以下是PHP拿起字段名稱:
if($_POST && isset($_POST['submit'], $_POST['ms_firstName'], $_POST['ms_lastName'], $_POST['ms_email'], $_POST['ms_password']`)){}
任何幫助將是讚賞。
如果你有形式爲什麼需要AJAX ,你的頁面不能重新加載?要麼 ? –
爲什麼您要手動編碼數據字段?爲什麼不直接通過ajax提交表單,如所有示例中所示? – arkascha
['serialize()'](https://api.jquery.com/serialize/)節省了大量的手工編碼。 –