2015-10-26 31 views
-1

我想創建一個表單提交將執行兩個操作的頁面。在index.php中,第一個動作是使用ajax發送要保存在數據庫中的數據。第二個動作是將頁面更改爲index1.php。我成功創建的代碼將數據保存到數據庫,但不會更改爲index1.php。我的代碼有什麼問題?表單提交兩個動作

的index.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 

<script> 
$(document).ready(function(){ 
$("#submit").click(function(){ 
var radio1 = $("input[name=group1]:checked").val(); 
var radio2 = $("input[name=group2]:checked").val(); 
var radio3 = $("input[name=group3]:checked").val(); 
var radio4 = $("input[name=group4]:checked").val(); 
// Returns successful data submission message when the entered information is stored in database. 
var dataString = '&submit1='+ radio1 + '&submit2='+ radio2 + '&submit3='+ radio3 + '&submit4='+ radio4; 

if(radio1==''||radio2==''||radio3==''||radio4=='') 
{ 
alert("Please Fill All Fields"); 
} 
else 
{ 
// AJAX Code To Submit Form. 
$.ajax({ 
type: "POST", 
url: "ajaxsubmit.php", 
data: dataString, 
cache: false, 
success: function(result){ 
alert(result); 
} 
}); 
} 
return false; 
}); 
}); 
</script> 

<form action="index1.php" method="post"> 
       <hr> 
         <label>Alignment: </label> 
         <input type="radio" name="group1" value="5"> 5 
         <input type="radio" name="group1" value="4"> 4 
         <input type="radio" name="group1" value="3"> 3 
         <input type="radio" name="group1" value="2"> 2 
         <input type="radio" name="group1" value="1"> 1 
         <hr> 
         <label>Blend: </label> 
         <input type="radio" name="group2" value="5"> 5 
         <input type="radio" name="group2" value="4"> 4 
         <input type="radio" name="group2" value="3"> 3 
         <input type="radio" name="group2" value="2"> 2 
         <input type="radio" name="group2" value="1"> 1 
         <hr> 
         <label>Warp: </label> 
         <input type="radio" name="group3" value="5"> 5 
         <input type="radio" name="group3" value="4"> 4 
         <input type="radio" name="group3" value="3"> 3 
         <input type="radio" name="group3" value="2"> 2 
         <input type="radio" name="group3" value="1"> 1 
         <hr> 
         <label>Overall: </label> 
         <input type="radio" name="group4" value="5"> 5 
         <input type="radio" name="group4" value="4"> 4 
         <input type="radio" name="group4" value="3"> 3 
         <input type="radio" name="group4" value="2"> 2 
         <input type="radio" name="group4" value="1"> 1 
       <hr> 
       <input type="submit" id="submit" name="submit" value="Submit" class="button"> 
</form> 
+0

使用'window.location.href = 「index1.php」;'在阿賈克斯成功 – Suraj

+0

做ajax成功重定向 – ElefantPhace

+0

看起來有答案,如果這個答案解決了你的問題,你可以通過點擊答案旁邊的綠色複選標記來將其標記爲已接受?它有助於社區知道這個問題已經解決。謝謝! – Jujunol

回答

1

你需要AJAX成功後改變位置:

$.ajax({ 
    type: "POST", 
    url: "ajaxsubmit.php", 
    data: dataString, 
    cache: false, 
    success: function(result){ 
     alert(result); 
     window.location = '/index1.php'; 
    } 
});