2011-12-26 71 views
0

我無法將輸入值插入到數據庫中。serialize()然後發佈到數據庫?

我有這樣的事情

$("form#profileName").submit(function(){ 
    var updateName = $(this).serialize(); 
    $.post("update.php",{updateName: updateName},function(data){ 
     alert(data); 
    }); 
    return false; 
}); 

在我的PHP我有這樣的事情

if(isset($_POST['updateName'])){ 
    $fname = $_POST['updateName']['f_name']; 
    $mname = $_POST['updateName']['m_name']; 
    $lname = $_POST['updateName']['l_name']; 
    $altname = $_POST['updateName']['alt_nam']; 

    echo $fname." ".$mname." ".$lname." ".$altname; 

} 

其呼應因故 「FFFF」,

我會正確地做這個?

感謝

+1

使用類似fiddler 2的東西並捕獲http請求並查看正在發送的內容。應該有助於確定HTML表單或代碼中是否有錯誤。沒有形式和要求,很難確定問題出在哪裏。 – 2011-12-26 21:57:20

+0

或'console.log(updateName)'在'$ .post'之前' – babonk 2011-12-26 21:59:15

+0

我只需提醒(updateName),我就可以得到正確的值。 f_name = fsdaf&m_name =&l_name =&alt_nam =,如果我不添加任何值 – hellomello 2011-12-26 22:02:52

回答

2

這裏的一些文檔.serialize()
你基本上序列化它兩次。

var updateName = $(this).serialize(); //f_name=somename&m_name=some 
$.ajax({ 
    ... 
    data: {updateName: updateName} //{updateName: 'somename&m_name=some'} 
    //which is translated into updateName=f_name%3dsomename%26m_name%3dsome 
}); 

我要帶胡亂猜測,並說,這是輸出的原因「F F F F」是因爲$_POST['updateName']現在是一個字符串。 PHP中的每個單獨字符都可以像這樣訪問$string[n]。我猜想它將空白解釋爲0,並給出第一個字符,實際上是「f」。

下面是它看起來應該像:

$("form#profileName").submit(function(){ 
    var updateName = $(this).serialize(); 
    $.post("update.php",updateName,function(data){ 
     alert(data); 
    }); 
    return false; 
}); 

,在我看來,美麗,清潔的方式,

$("form#profileName").submit(function(){ 
    var updateName = $(this).serialize(); 
    $.ajax({ 
     url: "update.php", 
     type: "POST", 
     data: updateName, 
     success: function(msg){ 
      alert(data); 
     } 
    }); 
    return false; 
}); 

而且PHP $_POST變量現在有幾個值,它們是這樣訪問的:

$_POST['f_name'] 
$_POST['m_name'] 
... 

另請注意,在PHP腳本中,您可能必須在變量上使用urldecode()rawurldecode(),具體取決於您在JS(encodeURI()encodeURIComponent())中發送數據的方式。

在這種情況下,serialize()有它自己的內部encodeURI(),所以不需要它,但PHP可能需要解碼它。 如果你仍然有你固定2xSerialize後的問題,只是改變了PHP腳本來解碼編碼數據:

$fname = urldecode($_POST['f_name']); 
$mname = urldecode($_POST['m_name']); 
... 

旁註
如果你有一個ID ATTR上表格,爲什麼不使用:$("#profileName").submit(...)
如果你問我,會更有意義。

調試
取決於你使用的是什麼瀏覽器,你可以查閱一下XHR被髮送到/從服務器recieving。在Firefox中,你可以使用一個叫做Firebug插件(XHR面板是NET-下> XHR)
麥克還提到調試在Chrome:

開發工具,瀏覽器 - >網絡 - > XHR底部


最終解決

的解決方案現在應該是這個樣子:

的Javascript:

$("form#profileName").submit(function(){ 
    var updateName = $(this).serialize(); 
    $.ajax({ 
     url: "update.php", 
     type: "POST", 
     data: updateName, 
     success: function(msg){ 
      alert(data); 
     } 
    }); 
    return false; 
}); 

PHP腳本「update.php」:

if(isset($_POST)){ 
    $fname = $_POST['f_name']; //urldecode($_POST['f_name']); 
    $mname = $_POST['m_name']; //urldecode($_POST['m_name']); 
    $lname = $_POST['l_name']; //urldecode($_POST['l_name']); 
    $altname = $_POST['alt_nam']; //urldecode($_POST['alt_nam']); 

    echo "$fname $mname $lname $altname"; 
} 
0

要設置的數據在請求被髮送到基本上是這樣的:

{ updateName : "f_name=fsdaf&m_name=&l_name=&alt_nam=" } 

這將是一次連載,所以updateName的值是一個字符串時,它到達PHP端。要麼你想傳遞一個對象:

$.post("update.php", { updateName : { f_name : "fsdaf", ... } }, function(data) { 

或字符串:

$.post("update.php", "f_name=fsdaf&m_name=&l_name=&alt_nam=", function(data) { 

但不是兩者的混合。

-1

這是一個複製/粘貼代碼我目前使用做這同樣的事情。請注意,這是基於codeigniter框架。

<?php 

/** 
* get the post vars, explode the uri string 
* and then urldecode each section of the uri. 
* this will create a numerical array called $cat 
* that will have a list of the values from the form 
* you could use something similiar to explode/urldecode 
* into an associative array.. 
*/ 
$a = explode('&', $this->input->post('items')); 
$i = 0; 
while ($i < count($a)) { 
    $b = explode('=', $a[$i]); 
    $cat[] = urldecode($b[1]); 
    $i++; 
} 

在這裏你可以看到我序列化所屬分類,在這種情況下即時序列化的UL/LI,但你可以通過表單的CSS ID序列化也是如此。其他人告訴我即時通過一個序列化的對象,但它的工作原理,我還沒有看到性能打擊。

/** 
* JSON/AJAX Submit for Categories 
* 
* On submit of #submit JSON query site/process controller 
* returns json encoded arrays of points and their lat/lng, html and sidebarHtml 
* 
* @return {json_array} 
* 
* @author Mike DeVita 
* @category map_js 
*/ 
$('.category').click(function(){ 
    /** make sure the checkbox that was clicked is checked, dont want to submit nothing, now do we? */ 
    if ($(this).is(":checked")){ 
     var items = $('#categoryList').serialize(); 
     /** animate the sidebar, close it onclick, hides the sidebar to show the full map */ 
     $('#button').click(); 
     /** delete the overlays from the map, effectively starts fresh */ 
     deleteOverlays(); 
     /** just in case were loading a crap ton of points, throw up a loading notice */ 
     showLoading(); 
     /** set a timeout of 275ms, this is so the sidebar collapses first.. adds to the ui magic */ 
     setTimeout(function(){ 
      /** ajax post call to the controller */ 
      $.post("index/process/categorylist.html", { "items": items }, 
      function(data){ 
       /** @type {numerical} if the returned errorStatus is == 1 */ 
       if (data.messageType == 'error'){ 
        /** hide the loading notice */ 
        hideLoading(); 
        /** generate the error message, onClick dismiss the error message */ 
        $.jGrowl(data.message, { theme: data.messageType }); 

        /** generate the click event, to show the sidebar again */ 
        $('#button').click(); 
       /** no errorStatus, so continue with populating map */ 
       }else{ 
        /** @type {object} the returned point's information, set it to something more related */ 
        var points = data; 
        /** setup the points, infobubbles, and sidebar */ 
        setPoints(points); 
        /** display the overlays (sidebar, infobbubles, markers) */ 
        showOverlays(); 
        /** throw a class on every odd row of the sidebar */ 
        $('.item:odd').addClass('sidebarAltRow'); 
        /** everythings done, hide the loading notice */ 
        hideLoading(); 
       } 
      }, "json"); //END Ajax post call to controller 
     }, 275); //END TIMEOUT 
    }//END IF checkbox is checked 
}); //END SUBMIT CLICK FOR AJAX 

然後我用CI​​內置的消毒庫,驗證他們要提交的數據,然後插入到數據庫中。在我的情況下,以關聯數組的形式。

更新: 我忘了提,我使用谷歌Chrome開發者工具麻煩拍攝XHR提交/接收和查看怎麼回事就與形式,它通過它的生命週期。您可以通過在底部的chrome - > network - > XHR中使用開發人員工具來監視ajax提交/接收,然後提交表單。

+0

-1這看起來非常不必要,並且令人困惑。爲什麼在一行上的註釋中使用'/ ** /'標籤?這是如何回答的?你甚至沒有解決或解釋他的問題。 – ShadowScripter 2011-12-28 13:48:59

+0

這兩個代碼塊都顯示了我如何通過ajax和php中的urldecode提交。和/ ** /是從我的插件在textmate中,而不是我的選擇。但誰在乎呢? – gorelative 2011-12-28 13:54:37