[英]using jquery post to send values to php file
I'm trying to write a script in javascript/jquery that will send values to a php file that will then update the database. 我正在尝试在javascript / jquery中编写一个脚本,该脚本会将值发送到php文件,该文件随后将更新数据库。 The problem is that the values aren't being read in by the PHP file, and I have no idea why.
问题是PHP文件没有读取这些值,我也不知道为什么。 I hard-coded in values and that worked fine.
我对值进行了硬编码,并且效果很好。 Any ideas?
有任何想法吗?
Here's the javascript: 这是JavaScript:
var hours = document.getElementById("hours");
var i = 1;
while(i < numberofMembers) {
var memberID = document.getElementById("member"+i);
if(memberID && memberID.checked) {
var memberID = document.getElementById("member"+i).value;
$.ajax({
type : 'post',
datatype: 'json',
url : 'subtract.php',
data : {hours : hours.value, memberID : memberID.value},
success: function(response) {
if(response == 'success') {
alert('Hours subtracted!');
} else {
alert('Error!');
}
}
});
}
i++;
}
}
subtract.php: 减去.php:
if(!empty($_POST['hours']) AND !empty($_POST['memberID'])) {
$hoursToSubtract = (int)$_POST['hours'];
$studentIDString = (int)$_POST['memberID'];
}
$query = mysql_query("SELECT * FROM `user_trials` WHERE `studentid` = '$studentIDString' LIMIT 1");
Edit: Updated code following @Daedal's code. 编辑:按照@Daedal的代码更新了代码。 I'm still not able to get the data in the PHP, tried running FirePHP but all I got was "profile still running" and then nothing.
我仍然无法在PHP中获取数据,尝试运行FirePHP,但是我所得到的只是“配置文件仍在运行”,然后什么也没有。
This might help you: 这可能对您有帮助:
function subtractHours(numberofMembers) {
var hours = document.getElementById('hours');
var i = 1;
while(i < numberofMembers) {
// Put the element in var
var memberID = document.getElementById(i);
// Check if exists and if it's checked
if(memberID && memberID.checked) {
// Use hours.value and memberID.value in your $.POST data
// {hours : hours.value, memberID : memberID.value}
console.log(hours.value + ' - ' + memberID.value);
// $ajax is kinda longer version of $.post api.jquery.com/jQuery.ajax/
$.ajax({
type : 'post',
dataType : 'json', // http://en.wikipedia.org/wiki/JSON
url : 'subtract.php',
data : { hours : hours.value, memberID : memberID.value},
success: function(response) {
if( response.type == 'success' ) {
alert('Bravo! ' + response.result);
} else {
alert('Error!');
};
}
});
}
i++;
}
}
and PHP part: 和PHP部分:
$result = array();
// Assuming we are dealing with numbers
if ( ! empty( $_POST['hours'] ) AND ! empty( $_POST['memberID'] ) ) {
$result['type'] = "success";
$result['result'] = (int) $_POST['hours'] . ' and ' . (int) $_POST['memberID'];
} else {
$result['type'] = "error";
}
// http://php.net/manual/en/function.json-encode.php
$result = json_encode( $result );
echo $result;
die();
Also you probably don't want to CSS #ID start with a number or to consist only from numbers. 另外,您可能不希望CSS #ID以数字开头或仅由数字组成。 CSS Tricks explained it well http://css-tricks.com/ids-cannot-start-with-a-number/
CSS技巧对它的解释很好http://css-tricks.com/ids-cannot-start-with-a-number/
You can simple fix that by putting some string in front: 您可以通过将一些字符串放在前面来简单地解决该问题:
var memberID = document.getElementById('some_string_' + i);
This is not ideal solution but it might help you to solve this error. 这不是理想的解决方案,但可以帮助您解决此错误。
Cheers! 干杯!
UPDATE: 更新:
First thing that came to my mind is that #ID with a number but as it seems JS don't care about that (at least not in a way CSS does) but it is a good practice not to use all numbers. 我想到的第一件事是带数字的#ID,但似乎JS并不在乎(至少不是CSS那样),但是最好不要使用所有数字。 So whole error was because
document.getElementById()
only accepts string. 所以整个错误是因为
document.getElementById()
仅接受字符串。
Reference: https://developer.mozilla.org/en-US/docs/DOM/document.getElementById id is a case-sensitive string representing the unique ID of the element being sought . 参考: https : //developer.mozilla.org/zh-CN/docs/DOM/document.getElementById id是区分大小写的字符串,表示要查找的元素的唯一ID 。
Few of the members already mentioned converting var i
to string and that was the key to your solution. 很少有成员提到过将
var i
转换为字符串,这是解决方案的关键。 So var memberID = document.getElementById(i);
所以
var memberID = document.getElementById(i);
converts reference to a string. 将引用转换为字符串。 Similar thing could be accomplished I think in your original code if you defined wright bellow the loop
while(i < numberofMembers) {
var i to string i = i.toString();
我想在您的原始代码中也可以完成类似的事情,如果您在循环中定义了wright
while(i < numberofMembers) {
var i to string i = i.toString();
but I think our present solution is better. 但我认为我们目前的解决方案更好。
Try removing the '' fx: 尝试删除''fx:
$.post (
"subtract.php",
{hours : hours, memberID : memberID}
try this 尝试这个
$.ajax({type: "POST",
url:"subtract.php",
data: '&hours='+hours+'&memberID='+memberID,
success: function(data){
}
});
Also you could try something like this to check 你也可以尝试这样的检查
$(":checkbox").change(function(){
var thisId = $(this).attr('id');
console.log('Id - '+thisId);
});
$studentID = $_GET['memberID'];
$hoursToSubtract = $_GET['hours'];
Try this: 尝试这个:
$.post("subtract.php", { hours: hours, memberID : memberID })
.done(function(data) {
document.body.style.cursor = "auto";
});
Try n use this... 尝试使用此...
$.post("subtract.php", { hours: hours, memberID : memberID })
.done(function(data) {
$(body).css({ 'cursor' : 'auto' });
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.