I have a small problem with a giftlist generated from SQL. My goal is to echo each row as a form with a textbox and a button, then when any button clicked, pass the textbox value, and an id number (hidden field value) to a function. Then this function would have get the values, and sends them with AJAX get method to a php, which would update a row with the giver's name in the SQL database. I cannot find the error in my code, so please help me in this regard.
我用SQL生成的礼品列表存在一个小问题。我的目标是将每一行作为带有文本框和按钮的表单进行回显,然后在单击任何按钮时,将文本框值和id号(隐藏字段值)传递给函数。然后这个函数将获取值,并使用AJAX get方法将它们发送到php,这将使用SQL数据库中的给予者名称更新行。我在代码中找不到错误,所以请在这方面帮助我。
EDIT: i need to figure out too, how to identify the button which was clicked.
编辑:我也需要弄清楚,如何识别被点击的按钮。
This would be my script:
这将是我的脚本:
<script type="text/javascript">
var aname = '';
var tid = 0;
$('.giftok').click(function()
{
if ($('.aname').val() === '')
{
alert('You have not provided your name.');
}
else
{
aname = $('.aname').val();
tid = $('.id').val();
$.ajax
({
url: "kosarba.php",
data: { ganame: aname, tid: gtid },
type: "GET",
context: document.body
}).done(function() {
alert("OK, it works.");
});
alert('Thank you!');
}
});
</script>
Here is my HTML+PHP:
这是我的HTML + PHP:
echo "<table id='giftlist' align='center' font-size='10pt'>";
while($sor=mysql_fetch_array($sordb))
{
echo "<tr>
<td width='420px'>$sor[gname]</td>
<td width='65px'>$sor[gprice] Ft</td>";
if (strlen($sor[aname]) !== 0)
{
echo "<td width='200px'>Sorry, someone already bought this one for us.</td>";
}
else
{
echo "<td width='335px'><form id='rendelget'>Your name: <input type='textbox' id='aname' name='aname' value='$aname'/><input type='hidden' class='id' name='id' value='$sor[id]'/> <button type='button' id='$sor[id]' class='giftok' value='Megveszem'>Megveszem</button></form> </td>";
}
echo "</tr>";
}
echo "</table>";
2 个解决方案
#1
1
You have mistaken a
variable
nametid = $('.id').val()
tid
should begtid
你误将变量名称tid = $('。id')。val()tid应该是gtid
I think that would be your script
我想那将是你的剧本
$(document).ready(function(){
var aname = '';
var tid = 0;
$('.giftok').click(function()
{
if($(this).closest('form').attr('name') == 'myId'){ //or id
if ($('.aname').val() === '')
{
alert('You have not provided your name.');
}
else
{
aname = $('.aname').val();
gtid = $('.id').val();
$.ajax
({
url: "kosarba.php",
data: { ganame: aname, tid: gtid },
type: "GET",
context: document.body
})
.error(function(){
alert('Ajax worked but error form server.');
})
.done(function() {
alert("OK, it works.");
});
alert('Thank you!');
}
}
});
})
//Update: If you identify the form holding the button gitve the form a name or id
//更新:如果您确定按住该按钮的表单,则为表单添加名称或ID
#2
1
Inside the ajax call, data: { ganame: aname, tid: gtid }
在ajax调用中,数据:{ganame:aname,tid:gtid}
'tid' is the post parameter, while gtid is the javascript variable. Mistakenly, you have used gtid instead of tid .
'tid'是post参数,而gtid是javascript变量。错误地,你使用gtid而不是tid。
use : data: { ganame: aname, tid: tid }
使用:数据:{ganame:aname,tid:tid}
#1
1
You have mistaken a
variable
nametid = $('.id').val()
tid
should begtid
你误将变量名称tid = $('。id')。val()tid应该是gtid
I think that would be your script
我想那将是你的剧本
$(document).ready(function(){
var aname = '';
var tid = 0;
$('.giftok').click(function()
{
if($(this).closest('form').attr('name') == 'myId'){ //or id
if ($('.aname').val() === '')
{
alert('You have not provided your name.');
}
else
{
aname = $('.aname').val();
gtid = $('.id').val();
$.ajax
({
url: "kosarba.php",
data: { ganame: aname, tid: gtid },
type: "GET",
context: document.body
})
.error(function(){
alert('Ajax worked but error form server.');
})
.done(function() {
alert("OK, it works.");
});
alert('Thank you!');
}
}
});
})
//Update: If you identify the form holding the button gitve the form a name or id
//更新:如果您确定按住该按钮的表单,则为表单添加名称或ID
#2
1
Inside the ajax call, data: { ganame: aname, tid: gtid }
在ajax调用中,数据:{ganame:aname,tid:gtid}
'tid' is the post parameter, while gtid is the javascript variable. Mistakenly, you have used gtid instead of tid .
'tid'是post参数,而gtid是javascript变量。错误地,你使用gtid而不是tid。
use : data: { ganame: aname, tid: tid }
使用:数据:{ganame:aname,tid:tid}