php - Javascript function not functioning -


ok have little problem something. have javascript/dom script submits user comment php page via ajax, , there works excellent.

but need implement on page, little differently. , can't seam make work . appreciate if me point errors.

html part:

<form name="comment-form-<?php echo $comment['comment_id'];?>" id="comment-form-<?php echo $comment['comment_id'];?>" method="post">     <div id="comments-approval">         <h1><?php echo $article['naslov'];?></h1>         <h2><?php echo $comment['comment_text'];?></h2>         <h3>[<?php echo date('d\. m\. y\.    h:i', $comment['comment_time']);?>]   ---   [ <?php echo $comment['comment_name'];?> ]</h3>     </div>     <input type="hidden" name="article_id" id="article_id" value="<?php echo $comment['comment_article_id'];?>" />      <input type="submit" value="&#x2717;" onclick="remove_comment('comment-form-<?php echo $comment['comment_id'];?>'); return false;" /> <!-- iks -->     <input type="submit" value="&#10004;" onclick="add_comment('comment-form-<?php echo $comment['comment_id'];?>'); return false;" /> <!-- otkaceno --> </form> 

after php foreach there n-number of forms created, , every form has it's own unique id

after moderator clicks on button , calls function ether add or remove send through form id in buttons located.

the javascript part:

function add_comment(formid){      var target = string('"#'+formid+'"');      $(target).submit(function(){           $.ajax({            type: "post",           url: "approvecomment.php",           data: $(this).serialize(),           datatype: 'text',                success: function(msg){               switch(msg) {                        //message                  }               }           });           return false;           });    } 

i know maybe it's dumb mistake don't have clue doing wrong.

edit:

javascript part:

function add_comment(formid){      var target = '#'+formid;      alert(target);      $(target).submit(function(){          alert($(this).serialize());           $.ajax({            type: "post",           url: "approvecomment.php",           data: $(this).serialize(),           datatype: 'text',                success: function(msg){               switch(msg) {                        //message                  }               }           });           return false;       });    } 

ok, first alert posts #comment-form-1

and second nothing.

and form id comment-form-1 exists in document.

if you're using jquery - why not bind submit buttons jquery's .click() handler instead of hard coding in html itself?

(note i'm assuming ajax function removing comments "removecomment.php")

updated html:

<form name="comment-form-<?php echo $comment['comment_id'];?>" id="comment-form-<?php echo $comment['comment_id'];?>" method="post"> <div id="comments-approval">     <h1><?php echo $article['naslov'];?></h1>     <h2><?php echo $comment['comment_text'];?></h2>     <h3>[<?php echo date('d\. m\. y\.    h:i', $comment['comment_time']);?>]   ---   [ <?php echo $comment['comment_name'];?> ]</h3> </div> <input type="hidden" name="article_id" id="article_id" value="<?php echo $comment['comment_article_id'];?>" />  <input type="submit" class="comment-remove" value="&#x2717;" /> <!-- iks --> <input type="submit" class="comment-add" value="&#10004;" /> <!-- otkaceno --> 

updated js:

//comment add click handler $("input.comment-add").click(function() {               $.ajax({           type: "post",           url: "approvecomment.php",           data: $(this).parent().serialize(),           datatype: 'text',           success: function(msg){               switch(msg) {                   //message              }           }       });       return false; });  //comment remove click handler $("input.comment-remove").click(function() {                $.ajax({           type: "post",           url: "removecomment.php",           data: $(this).parent().serialize(),           datatype: 'text',           success: function(msg){               switch(msg) {                   //message              }           }       });       return false; });      

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -