2013-08-23 69 views
0

因此,因爲我的經驗與jquery爲零我不知道如何運行我的mysql腳本時提交表單沒有重新加載頁面? 總之 形式單選按鈕提交 - >形式sended - >查詢,而無需重新加載Jquery提交單選按鈕沒有重裝和

我的腳本:

<form id="myForm" method="get"> 
    <div class="row"> 
     <div class="span4 info1"> 
      <section class="widget"> 
       <img src="../bootstrap/img/thumb/0.jpg" width="auto" height="auto"> 
       <?php if ($selectedBg == '0') { 
        echo '<h2>Current one</h2>'; 
       } ?> 

       <input type="radio" name="options[]" value="0" onclick="document.getElementById('myForm').submit();"/> 
       Choose Background 
      </section> 
     </div> 
    </div> 

    <?php 
     $checked = $_GET['options']; 
     for ($i = 0; $i < count($checked); $i++) { 
      $sql = 'UPDATE blog_users SET background = '.$checked[$i].' WHERE username=?'; 
      $bg = $db->prepare($sql); 
      $bg->execute(array($session->username)); 
     } 
    ?>  
</form> 

所以我的問題是如何提交我的形式,而不重新加載頁面+我跑查詢?

+0

您需要使用AJAX這一點。這可能對你有所幫助:http://stackoverflow.com/questions/15814369/getting-radio-button-value-and-sending-through-ajax-to-php –

回答

1

您將需要創建一個接受表單數據的單獨頁面,並使用jQuery的ajax函數異步提交數據。

下面是一些僞代碼,以顯示你將如何做到這一點:

$('form').on('click', 'input:radio', function() { 
    $.get('NEW_PAGE_URL' + $(this).serialize(), function(data) { 
     // this function is the success function. 
     // your page should return some kind of information to let this function 
     // know that the submission was successful on the server side as well. 
     // This was you can manipulate the DOM to let the user know the submission 
     // was successful. 
    }); 
}); 
+0

我會研究它謝謝你! –

0

您所描述的是一種稱爲AJAX的技術。這是一種不特定於jQuery,php或mysql的技術。這就是說,JQuery有一個共同的任務來幫助它。

你可能要檢查這個帖子出來:https://stackoverflow.com/a/5004276/1397590

或者,如果你正在尋找更多的教程的,那麼這裏取峯:http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

無論哪種方式,有很多資源在那裏開始學習AJAX。