2012-11-19 67 views
0

我用ajax更新數據庫有點麻煩,但是我不能讓它工作。 我不知道我做錯了什麼。用ajax更新數據庫不起作用

這裏我的PHP文件(sendata.php)

<?php 
     if (isset($_POST['register']) && $userid != 0) 
     {$register = $_POST['register'] ; 

      $sql2 =$db->setQuery("INSERT INTO .... "); 
        $db->query() ; 
      } 

    ?> 

,在這裏我javascript代碼

$(document).ready(function() { 
    $('#register').click(function() { 

     $.ajax({ 

      url: "sendata.php", 
      data: { 
       age: $('#age').val() 
      }, 
      type: 'POST', 

      success: function(msg) { 
       alert("Data has been saved succefully"); 
       $('#mydata').html("<b> age: </b>" + age); 
      } 
     }); 

     return false; 
    }); 
}); 

什麼heppening對我來說,當寄存器按鈕被點擊我只得到警報後,該數據保存成功,但是當我去數據庫並看到時,沒有任何記錄。 即時做錯了什麼?

編輯:

這是我的按鈕

 <input type="submit" id="register" name="register" value="Save my Data" /> 
+0

您是否可以在任何地方定義可變重量?你是否檢查數據庫連接在你的php函數中是否正確?你的完整查詢是什麼? –

+0

是的,它定義了重量 –

回答

1

sendata.php檢查,看看是否「註冊」設置:if (isset($_POST['register']) ...)所以你必須在你的要求設置變量「註冊」(我固定代碼 - 見粗體):

$(document).ready(function() { 
    $('#register').click(function() { 

     $.ajax({ 

      url: "sendata.php", 
      data: { 
       age: $('#age').val(), 
       register: "register" 
      }, 
      type: 'POST', 

      success: function(msg) { 
       alert(msg); 
       $('#mydata').html("<b> age: </b>" + age); 
      } 
     }); 

     return false; 
    }); 
}); 

sendata.php

if (isset($_POST['register']) && $userid != 0) 
{ 
    $register = $_POST['register'] ; 
    $sql2 =$db->setQuery("INSERT INTO .... "); 
    $db->query() ; 
    echo "SUCCESS"; 
    exit(0); 
} 
echo "FAILURE"; 
+0

這是正確的 –

+0

「register = something」?什麼東西?這個寄存器是提交按鈕,所以什麼可以是什麼東西? –

+0

它說失敗的成功消息。但我做了'註冊:「註冊」',我不知道你應該在那裏做什麼 –

0

的Javascript:

    jQuery(document).ready(function($) 
        { 
        //prevent registering the event more than once 

        //$('#register').off('click').on('click',(function(e) { 

(1)我想先從:

    $('form').off('submit').on('submit',(function(e) { 

或..

     $('input[type="submit"]').off('click').on('click',(function(e) { 

然後..

       $.ajax({ 
               //url to touch 
              url: "sendata.php", 
              data: { 
               age: $('#age').val() 
              }, 
              type: 'POST', 
              //on fail 
                 fail: function(data){ 
                 //regular javascript alert on error 
                    alert("Oppps! Data error"); 
                 }, 
              //on success 
              success: function(data) { 
               //regular javascript alert on success 
               alert("Data has been saved succefully"); 

               //assuming this is a div with an id of mydata 
               $('#mydata').html("<b> Age is: </b>" + data); 
              } 
             }); 

           //prevents default action as we are not submiting the form but rather making use of AJAX to touch the php file 
           e.preventDefault(); 

           }); 

        }); 

sendata。 p hp

       <? 
       $msg= ''; 

        if ( isset($_POST['age']) ) 
        { 
         $age = filter_input(INPUT_POST, "age", FILTER_SANITIZE_STRING); 
       //echo $age; 

       //around this check if the insertion is correct and that you can talk to the db 
       //db handling, insertion, etc.. 
            // $sql2 =$db->setQuery("INSERT INTO .... "); 
            // $db->query(); 

         //$msg .= "Epic Win! $age"; 
         $msg .= "$age"; 
        } 
        else $msg .= 'Error, no age provided :('; 
         //we are returning the data so we need to echo it as an output here. 
        echo $msg; 

       ?>