我有這樣的代碼,它從表單獲取信息並將其發送到form.php,並將其處理。 form.php迴應變量(Fullname:$ fullname E-mail:$ email Link:$ link Instructions:$ instr),它們應該被輸入到#success。我的代碼在firefox和chrome中表現了兩種不同的方式。jQuery Ajax不返回來自外部的變量PHP
在Firefox中,我發送到form.php,它顯示正確的輸出,但顯然我不應該發送到那裏,我應該留在我的主頁上,並在#success中看到輸出。基本上,阿賈克斯不起作用。
在Chrome瀏覽器中,ajax可以工作,但只能將Fullname:Email:Link:Instructions:轉換爲#success。實質上,jQuery不通過POST傳遞變量。
mainpage.php:
<script type="text/javascript">
$(document).ready(function() {
var data = $(form).serialize();
$('#form').submit(function(event) {
$.ajax({
url: '../wp-content/themes/MC/form.php',
type: 'POST',
data: data,
success: function(result) {
$('#success').html(result);
}
});
event.preventDefault();
});
})
</script>
<div id="exchange_inside">
<div id="formdiv">
<br>
<form method="post" id="form">
Name/Nom:<input type="text" name="fullname" /><br />
E-mail/Courriel:<input type="text" name="email" /><br /> Your Daily URL/Votre URL Quotidien:<input type="text" name="link" /><br />
Voting Instructions/Instructions de Vote:<textarea name="instr" style="width: 450px; height: 100px;"/></textarea><br />
<input type="submit" value="Submit" />
</form>
</div>
</div>
<div id="success">
</div>
form.php的:
<?php
if (isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
}else{
echo "No name";
}
if (isset($_POST['email'])){
$email = $_POST['email'];
}else{
echo "No email";
}
if (isset($_POST['link'])){
$link = $_POST['link'];
}else{
echo "No link";
}
if (isset($_POST['instr'])){
$instr = $_POST['instr'];
}else{
echo "No instructions";
}
echo "Name: " .$fullname. " E-mail: " .$email. " Link: " .$link. " Instructions: " .$instr;
?>
問題解決了! – Sweepster