尝试使用jquery发送由id标识的html表单

时间:2013-02-20 06:25:49

标签: php jquery html

我正在回复我的数据库中的内容列表,并在每个项目旁边都有一个星形图标。我尝试这样做,以便当用户点击星标时,它会在我的数据库中找到该内容并更改该字段中的“热门”值(“喜欢”按钮)。我一直试图通过让每个星形包装在一个标签中,然后使每个表单的形式'formWithId'x ...... x成为正在显示的内容的id来做到这一点。

这是代码:

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']."'>&#x22C6; </i>";
echo "</form>"; 

所以,现在我需要的是能够通过使用jquery validate发送表单....但我遇到的问题是我一直使用jquery验证(使用提交处理程序)来提交特定的表单id是预定义的,不会根据从数据库发送的内容的ID进行更改。

这是我试图

的例子
$(document).ready(function(){
    $(".ss-icon").click(function(){
       var id= $(this).attr('id');
       $("#likePostWithId"+id).submit();
    });
});

$(function(){
    $(".ss-icon").click(function(){
    //this doesn't work...
    $("#likePostWithId").validate({
         submitHandler:function(data){
            //do stuff
          }
     )};
)};

查看$("#likePostWithId")的位置...我不知道如何选择具有正确ID的示例(示例$("#likePostWithId"+id))将其解释为#likePostWithId6$likePostWithId7其中数字是mysql内容的id

无论如何我不知道你是否清楚任何人...我真的不知道如何问这个问题..但基本上我需要发送一个id(来自mysql的内容的id)到没有重新加载页面的PHP表单

感谢任何帮助。

感谢

4 个答案:

答案 0 :(得分:1)

您可以使用jQuery的Ajax实现此目的。简单地给每个星形图标一个唯一的id(这个id应该与MySQL数据有一些关系,比如索引键)。使用jQuery的Ajax函数将此参数(id)传递给php脚本,该脚本获取数据(每个MySQL条目都是唯一的,如索引),这会改变它的流行度字段条目。在成功功能上,您可以对星形图标进行一些动画或颜色更改,以通知数据已成功发布。试一试。

答案 1 :(得分:0)

试试这个

 $(function(){
   $(".ss-icon").click(function(){
      var id= $(this).attr('id');
      $("#likePostWithId" + id ).validate({ <--- here
      submitHandler:function(data){
         $("#likePostWithId"+id).submit();
      }
   )};
 )};

然而,这提交了表单...你可以使用ajax提交表格并重新加载

答案 2 :(得分:0)

也许您可以使用$.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
    }
}

答案 3 :(得分:0)

您的代码:

$(function(){
    $(".ss-icon").click(function(){
        //this doesn't work...
        $("#likePostWithId").validate({
             submitHandler:function(data){
                 //do stuff
             }
        )};
    }); // <-- this was missing
)};

您错过了一组括号,但您仍然不应该像那样实现它。您只是在每次点击时重新初始化插件。 (该插件已内置click事件处理程序,并自动捕获。)

.validate()必须在DOM中调用一次,准备好初始化插件。 As per documentationsubmitHandler回调函数“在验证后通过Ajax提交表单的正确位置。”

$(function(){

    $("#likePostWithId").validate({ // initialize plugin
         // other options & rules,
         submitHandler: function(form){
             // do ajax
             return false; // blocks form redirection since you already did ajax
         }
    )};

)};

使用演示进行游戏:http://jsfiddle.net/XnfDc/