Jump to content

Ajax Help - Could be a list full


TeddyKiller

Recommended Posts

Here is my function.. it's still a bit messy. Although it'll be tidied up a bit.

$(function() {      
    $(".postcomment").click(function() {
      var statusid = $("#statusid").val();
      var boxval = $("#comment".statusid).val();
      
      var dataString = 'content='+ boxval;
      if(statusid == '') {
         alert("Please Enter id");
      } else {
            if(boxval == '') {
               alert("Please Enter Some Text");
            } else {
               $("#flash"+statusid).slideDown("slow").html('<span class="loading">loading</span>');

               $.ajax({
                   type: "POST",
                   url: "update_comment.php",
                   data: dataString,
                   cache: false,
                   success: function(html){
                      $("div#commentdisplay"+statusid).prepend(html);
                     $("div#commentdisplay"+statusid).slideDown("slow");
                      document.getElementById('content').value='';
                     document.getElementById('content').focus();
                       $("#flash"+statusid).hide();
                  }
               });
         }
      } 
      return false;
    });   
});

It works like.. var boxval = $("#comment".statusid).val();

but not like.. var boxval = $("#comment"+statusid).val();

yet stuff like.. $("#flash"+statusid).slideDown("slow") doesn't work

Is it because of the 'var' ?

 

It looks as if it works.. probably has some claws in it. The divs are used over and over again (its in a php while) so I use a unique id for it.

 

Now the next problem is.. as it goes to update_comment.php, I don't know how to handle the $_POST?

 

$_POST['content'] and $_POST['statusid'] come up in the database as 0 for the id, and undefined for the content.

if(isset($_POST['content'])) {
   $content = $_POST['content'];
   $status = $_POST['statusid'];
   $time = time();
   $query = mysql_query("INSERT INTO `status_comment` (status_id, user_id, comment, date) 
                  VALUES ('$status', '$user->id', '$content', '$time')") or die(mysql_error());
}

 

Any help with this..?

It seems to work though, just having problems with the $_POST.

Link to comment
Share on other sites

One thing I noticed right away is this:

 

var boxval = $("#comment".statusid).val();

 

Should be:

 

var boxval = $("#comment"+statusid).val();

 

But as far as I can see there's no reason to store that in a variable.

 

First you should check to make sure the variables hold what you're expecting.

 

data: {statusid: $("#statusid").val(), content: $("#comment"+statusid).val()},

Link to comment
Share on other sites

One thing I noticed right away is this:

 

var boxval = $("#comment".statusid).val();

 

Should be:

 

var boxval = $("#comment"+statusid).val();

The second one issued the

alert("Please Enter Some Text");

The first one.. issued no alert, although it entered an empty value into the database...

 

Any help?

 

the data: you gave, works fine from what I can see (The statusid gets entered) I read about that in the other post. Which I read after I posted on here... so I have that understood now.

Link to comment
Share on other sites

I've echo'd the ID aswell.. and the ID works fine anyway.. so I'm confused..

Here is the form..

echo '<form action="" method="post">
   <table width="100%">
      <tr>
         <td><textarea rows="1" cols="40" name="comment'.$row['id'].'" id="comment'.$row['id'].'"></textarea></td>
      </tr>
                                 
      <tr>
         <td style="text-align:right;">
            <input type="hidden" value="'.$row['id'].'" name="statusid" id="statusid" />
            <input type="submit" name="postcomment" class="postcomment" value="Comment" />
         </td>
      </tr>
   </table>
</form>';

Link to comment
Share on other sites

You said that block of HTML is looped over and over? That means there will be multiple declarations of elements with the id/name of statusid containing different values.

Link to comment
Share on other sites

which is why there is ID's, it's unique to each status.

There is a while function for the status query. Which displays all status's.

Within the while function, is the echo in which is above.

 

$row is mysql_fetch_assoc of the status query. So each $row['id'] is unique for each status. There can only be one of the echo above for each status. So for example..

'comment9' can only be an ID ONCE....

 

is this clearer to you?

Link to comment
Share on other sites

I understand that, but what I'm talking about is this:

 

<input type="hidden" value="'.$row['id'].'" name="statusid" id="statusid" />

 

That element is also repeated without a unique id and it's what you're using to get the id for the comment, which won't work because there are multiple elements with that same id/name.

Link to comment
Share on other sites

I understand that, but what I'm talking about is this:

 

<input type="hidden" value="'.$row['id'].'" name="statusid" id="statusid" />

 

That element is also repeated without a unique id and it's what you're using to get the id for the comment, which won't work because there are multiple elements with that same id/name.

Oh.. yeah I see.. damn.. any ideas? I can't put $row['id'] onto it.. because I wouldn't be able to get the value of it as I wouldn't know the ID.

I can't use sessions or cookies..

Link to comment
Share on other sites

Something like this should work:

 

echo '<form action="" method="post">
   <table width="100%">
      <tr>
         <td><textarea rows="1" cols="40" name="comment'.$row['id'].'" id="comment'.$row['id'].'"></textarea></td>
      </tr>
                                 
      <tr>
         <td style="text-align:right;">
            <input type="submit" id="'.$row['id'].'" name="postcomment" class="postcomment" value="Comment" />
         </td>
      </tr>
   </table>
</form>';

 

$(function() {      
    $(".postcomment").click(function() {
  var commentid = $(this).attr('id');
  
      if(commentid == '') { // ??
         alert("Please Enter id");
      } else {
            if($('#comment' + commentid).val() == '') {
               alert("Please Enter Some Text");
            } else {
               $("#flash"+statusid).slideDown("slow").html('<span class="loading">loading</span>');

               $.ajax({
                   type: "POST",
                   url: "update_comment.php",
                   data: data: {statusid: commentid, content: $("#comment"+commentid).val()},
                   cache: false,
                   success: function(html){
                      $("div#commentdisplay"+statusid).prepend(html);
                     $("div#commentdisplay"+statusid).slideDown("slow");
                      document.getElementById('content').value='';
                     document.getElementById('content').focus();
                       $("#flash"+statusid).hide();
                  }
               });
         }
      } 
      return false;
    });   
});

 

I didn't alter the line with the slideDown(), or the stuff in the success callback, so you'll need to edit that accordingly. But that should give you an idea.

Link to comment
Share on other sites

... well there was an error as you duplicated the word "data:"

Although.. I still have the problem as before. It displays the please enter some text alert.

I changed the alert to this..

alert("comment" + commentid);

This gave "comment9" So it gets the ID from the submit button... but still is thinking the textarea is empty.

Even if I fill in all the textareas on the page, still says its empty.

 

I changed it to textbox, although this still never fixed it.

Like.. what the hell?

Link to comment
Share on other sites

Changing it to a class, doesn't bring up the alert (unless the textarea is ACTUALLY empty) although it just reloads the page..

 

EDIT: Oh yes!

There was a few 'statusid' that needed changing to commentid, even after the ones you mentioned in the success, still was one that needed changing. It works great now.

 

Although why didn't it like the ID, and instead it liked the CLASS for the textarea?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.