2013-02-27 63 views
1

我試圖通過ajax更新學生的配置文件腳本運行良好我獲得了所有在dataString中傳遞的值,但它沒有更新字段的值,當savebasic.php通過ajax調用。在通過ajax發佈值時更新表字段的錯誤

的Jscript:

<script type="text/javascript" src="http://ajax.googleapis.com/ 
ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
$(".savestudent").click(function() { 

    var _firstname=$("#firstname").html(); 
    var _lastname=$("#lastname").html(); 
    var _gender=$("#gender").html(); 
    var _location=$("#location").html(); 
    var _aboutme=$("#about").html(); 
    var _dob=$("#dob").html(); 

    var dataString= 'fname='+ _firstname + '&lname='+ _lastname + '&gender='+ _gender + '&location='+ _location + '&about='+ _aboutme + '&dob='+ _dob ; 
    alert(dataString); 

    $.ajax 
    ({ 

    type: "POST", 
    url: "savebasic.php", 
    data: dataString, 
    cache: false, 
    success: function(html) 
    { 

     alert('success'); 
    }, 
    error: function(html) 
    { 


    } 
    }); 
    }); 
}); 

</script> 

savebasic.php:

<?php 

include_once('controller/profile.controller.php'); 
$profileObject=new ProfileController(); 

     $fname=$_POST['fname']; 
     $lname=$_POST['lname']; 
     $gender='M'; 
     $loc=$_POST['location']; 
     $about=$_POST['about']; 
     $birth=$_POST['dob']; 

$upt=$profileObject->updateUserprofile('59',$fname,$lname,$birth,$gender,$loc,$about); 

?> 

,如果我直接傳遞savebasic.php它的工作原理值。但是通過ajax時它什麼都不做。

回答

1

您正在以錯誤的方式在ajax中使用數據字段。你應該這樣做:

data:{'fname=': _firstname, 'lname=':_lastname,'gender=':_gender,'location=': _location} 
0

您的數據串的設置類似於一個GET - 正常的語法是:

data: {'fname': _firstname, 'lname':_lastname .... } 
0
Change your code to assign data in following way 


<script type="text/javascript" src="http://ajax.googleapis.com/ 
ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
$(".savestudent").click(function() { 

    var _firstname=$("#firstname").html(); 
    var _lastname=$("#lastname").html(); 
    var _gender=$("#gender").html(); 
    var _location=$("#location").html(); 
    var _aboutme=$("#about").html(); 
    var _dob=$("#dob").html(); 

    var data= {  fname:_firstname, 
        lname:_lastname, 
        gender=: _gender , 
        location:_location, 
        about: _aboutme, 
        dob: _dob}; 
    alert(data); 

    $.ajax 
    ({ 

    type: "POST", 
    url: "savebasic.php", 
    data: data, 
    cache: false, 
    success: function(html) 
    { 

     alert('success'); 
    }, 
    error: function(html) 
    { 


    } 
    }); 
    }); 
}); 

</script> 

而且,不要讓從元素的值正確方法.. 如果名字,姓氏和其他字段在文本框中,那麼你應該訪問這樣的。

var _firstname=$("#firstname").val(); 

或者,如果他們是在跨度或div標籤,然後訪問那些像這樣

var _firstname=$("#firstname").text(); 
+0

不,他們在標籤和即時獲得所有的價值,雖然 – 2013-02-27 10:38:26

+0

好吧,這很好 – 2013-02-27 10:42:24

0

您的數據部分是錯誤的。你也忘了提及dataType 我已編輯你的代碼。

檢查出來:

<script type="text/javascript" src="http://ajax.googleapis.com/ 
ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
$(".savestudent").click(function() { 

    var _firstname=$("#firstname").html(); 
    var _lastname=$("#lastname").html(); 
    var _gender=$("#gender").html(); 
    var _location=$("#location").html(); 
    var _aboutme=$("#about").html(); 
    var _dob=$("#dob").html(); 

    // var dataString= 'fname='+ _firstname + '&lname='+ _lastname + '&gender='+ _gender + //'&location='+ _location + '&about='+ _aboutme + '&dob='+ _dob ; 
    // alert(dataString); 

    $.ajax 
    ({ 

    type: "POST", 
    contentType: 'text/html', 
dataType: 'text', 
    url: "savebasic.php", 
    data: { 
     fname: _firstname, lname :_lastname, gender :_gender, location : _location 
     ,about : _aboutme , dob : _dob 
    }, 
    cache: false, 
    success: function(html) 
    { 

     alert('success'); 
    }, 
    error: function(html) 
    { 


    } 
    }); 
    }); 
}); 

</script> 

這會給你100%正確的結果。