[英]Error in updating table fields when posting values through ajax
I m trying to update profile of student through ajax the script is runing good i get all the values passed in dataString but it is not updating the values of fields when the savebasic.php is called through ajax. 我试图通过ajax更新学生资料,脚本运行良好,我获得了所有通过dataString传递的值,但是当通过ajax调用savebasic.php时,它没有更新字段的值。
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: 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);
?>
if i directly pass values on savebasic.php it works . 如果我直接在savebasic.php上传递值,它将起作用。 but while passing through ajax it does nothing. 但是在通过ajax时它什么也没做。
You are using data field in ajax in a wrong way. 您在ajax中以错误的方式使用数据字段。 You should do: 你应该做:
data:{'fname=': _firstname, 'lname=':_lastname,'gender=':_gender,'location=': _location}
您的数据字符串像GET一样设置-常规语法为:
data: {'fname': _firstname, 'lname':_lastname .... }
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>
Also, it is not correct way to get the value from the element.. If the firstname, lastname and other fields are in text boxes, then you should access those like this. 同样,从元素中获取值也是不正确的方法。如果名字,姓氏和其他字段在文本框中,则应访问此类字段。
var _firstname=$("#firstname").val();
Or if they are in span or div tag, then access those like this 或者,如果它们在span或div标签中,则可以访问类似的内容
var _firstname=$("#firstname").text();
your data part is wrong . 您的数据部分错误。 also you forgot to mention the dataType
I have edited your code . 您也忘记提及我编辑过代码的dataType
。
check it out : 看看这个 :
<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>
this will give you 100% correct result . 这将为您提供100%正确的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.