Php 尝试使用jquery发送id标识的html表单
我正在从我的数据库中回显一个内容列表,每个项目旁边都有一个星形图标。我试图这样做,当用户点击星号时,它会在我的数据库中找到该内容,并更改该字段中的“流行”值(一个“喜欢”按钮)。我一直在努力做到这一点,让每一颗星星都包在一个标签里,然后把每个表单的id做成'formWithId'x。。。。。。x是正在显示的内容的id 下面是代码:Php 尝试使用jquery发送id标识的html表单,php,jquery,html,Php,Jquery,Html,我正在从我的数据库中回显一个内容列表,每个项目旁边都有一个星形图标。我试图这样做,当用户点击星号时,它会在我的数据库中找到该内容,并更改该字段中的“流行”值(一个“喜欢”按钮)。我一直在努力做到这一点,让每一颗星星都包在一个标签里,然后把每个表单的id做成'formWithId'x。。。。。。x是正在显示的内容的id 下面是代码: echo "<form method = 'post' class ='formId".$row['id']."' id = 'likePostWithId"
echo "<form method = 'post' class ='formId".$row['id']."' id = 'likePostWithId".$row['id']."'>";
//all inputs are hidden and send the 'id' value of the content so the php file will know which field of the table to like in the mysql database
echo "<input type ='text' name = 'email' style = 'display: none;' value= '".$row['email']."' >
<input type ='text' name = 'id' style = 'display: none;' value= '".$row['id']."' >
//this is the star icon
<i class = 'ss-icon' id = '".$row['id']."'>⋆ </i>";
echo "</form>";
请参阅where$(“#likePostWithId”)
。。。我不知道如何让它选择一个具有正确id的(例如$(“#likePostWithId”+id)
)将解释为#likePostWithId6
或$likePostWithId7
,其中数字是mysql内容的id
不管怎样,我不知道你们是否清楚这一点。。。我真的不知道怎么问这个问题。。但基本上我需要在不重新加载页面的情况下向php表单发送一个id(mysql内容的id)
感谢您的帮助
谢谢您可以使用jQuery的Ajax实现这一点。只需给每个星形图标一个唯一的id(这个id应该和MySQL数据有一些关系,比如索引键)。使用jQuery的Ajax函数将这个参数(id)传递给一个php脚本,该脚本获取数据(每个MySQL条目都是唯一的,比如索引),从而更改它的流行字段条目。在成功功能中,您可以对星形图标进行一些动画或颜色更改,以通知数据已成功发布。试试看。试试这个
$(function(){
$(".ss-icon").click(function(){
var id= $(this).attr('id');
$("#likePostWithId" + id ).validate({ <--- here
submitHandler:function(data){
$("#likePostWithId"+id).submit();
}
)};
)};
$(函数(){
$(“.ss图标”)。单击(函数(){
var id=$(this.attr('id');
$(“#likePostWithId”+id)。验证({也许您可以使用$.post()
来解决您的问题。这样您就不必乱搞表单了:
$(document).ready(function(){
$(".ss-icon").click(function(){
$.post('yourPHPUrl', { contentID: $(this).attr('id') }, successFunction);
});
});
function successFunction(data) {
if (data.success) { //just a sample, you can give a JSON-Object back to the success function
$('#' + data.ID).css() ... // do something with the clicked star
}
}
您的代码:
$(function(){
$(".ss-icon").click(function(){
//this doesn't work...
$("#likePostWithId").validate({
submitHandler:function(data){
//do stuff
}
)};
}); // <-- this was missing
)};
玩一个演示:你有没有尝试过类似$(this).最近的('table').find('yourElementName').validate();没有,但我想我是通过ajax获得的…我只是不知道如何通过.ajax()发送id…从未使用过这么多,我如何使用.ajax()发送值php文件是如何接收它的?例如,我是否使用data:id发送,然后php文件将通过$_POST['id']接收它?我的《javascript&jQuery缺少的手册》一书中说,“与大多数jQuery函数不同,你不会将get()或POST()添加到jQuery selecto中。你永远不会做$('#content')。get('products.php'))。$.post()和$.get()的语法是$.get(url、数据、回调);希望对您有所帮助。。是的,在php中,您可以获得如下数据:$itemNum=$\u post['itemNum'];其中itemNum是表单元素之一的id,例如$(function(){$(.ss icon”)。单击(function(){var id=$(this.attr('id');$(“#likePostWithId”+id)。ajax({type:“POST”,url:“likePost.php”,success:function(){alert(“success”);}}};});
$(function(){
$(".ss-icon").click(function(){
//this doesn't work...
$("#likePostWithId").validate({
submitHandler:function(data){
//do stuff
}
)};
}); // <-- this was missing
)};
$(function(){
$("#likePostWithId").validate({ // initialize plugin
// other options & rules,
submitHandler: function(form){
// do ajax
return false; // blocks form redirection since you already did ajax
}
)};
)};