2016-11-30 67 views
0

目前我有一個多選複選框的形式。當提交它發佈到一個PHP腳本具有類似於一個數組: & ID%5B%5D = 3 & ID%5B%5D = 7提交表格與重新加載

HTML:

<form id="frm-example" action="vmwaressl_admin_process.php" method="POST"> 

PHP腳本通過循環數組併爲選定的每一行執行SQL更新。該腳本然後使用header()函數重定向回頁面。

PHP:

<?php 
foreach ($_POST['id'] as $key => $idValue) { 
    $host_id_sql = "query"; 
    $stmt  = sqlsrv_query($conn, $host_id_sql); 
    if ($stmt === false) { 
     die(print_r(sqlsrv_errors(), true)); 
    } 
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    $host_id = $row['HOST_ID']; 

    } 
    $host_update_sql = "update query"; 
    $stmt   = sqlsrv_query($conn, $host_update_sql); 
    if ($stmt === false) { 
     die(print_r(sqlsrv_errors(), true)); 
    } 
    $host_id_sql = null; 
    $stmt  = null; 
} 

header('Location: home_page.php'); 
?> 

我的問題是關於頁面刷新。有沒有辦法提交表單,但不刷新頁面?我在想阿賈克斯,但我不知道如何實現它。

感謝。

回答

1

你的表單標籤更改爲只: -

<form id="frm-example"> 

,並在script標籤補充一點: -

$('#frm-example').on('submit', function (e) { 
     if (!e.isDefaultPrevented()) { 
      var url = "vmwaressl_admin_process.php"; 

      $.ajax({ 
       type: "POST", 
       url: url, 
       data: $(this).serialize(), 
       success: function (data) 
       { 
        //Whatever You want to do on success 
       } 
      }); 
      return false; 
     } 
    }) 
+0

謝謝!!它工作完美。 – solar411

+1

不客氣 –

相關問題