TeddyKiller Posted May 4, 2010 Share Posted May 4, 2010 I'm working on some ajax. So far only came into 1 problem. var statusid = $("#statusid").val(); var boxval = $("#comment").val(); #comment has the statusid on the end. How would I put it in? Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/ Share on other sites More sharing options...
TeddyKiller Posted May 4, 2010 Author Share Posted May 4, 2010 var statusid = $("#statusid").val(); var boxval = $("#comment"+statusid).val(); This does't work.. any other ideas? The code above.. still says the comment field is empty =/ Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053326 Share on other sites More sharing options...
Alex Posted May 4, 2010 Share Posted May 4, 2010 Not entirely sure what you're trying to do. Can you post an example of the element you're trying to match with that selector? Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053331 Share on other sites More sharing options...
TeddyKiller Posted May 4, 2010 Author Share Posted May 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053342 Share on other sites More sharing options...
Alex Posted May 4, 2010 Share Posted May 4, 2010 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()}, Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053344 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053349 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053356 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 What are you asking? Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053357 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 As said in your post, it should be.. var boxval = $("#comment"+statusid).val(); This brings up the alert saying to insert some text. Yet i've entered a value into the textarea.. The question is.. why? whats wrong. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053359 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 You must be using the selector incorrect, so it's not returning the proper element. Can you post your HTML and what statusid contains? Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053360 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 The html is in the Echo as posted in a previous post in here.. $row['id'] is the ID of the status. For example.. 9 statusid contains what $row['id'] contains. I've echod them both and it seems to have a value of expected. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053361 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053362 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053363 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053365 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053367 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 If it can't be done.. whats the other way.. perhaps the way facebook would do it.. because I'm trying to achieve something similar.. really puzzled Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053372 Share on other sites More sharing options...
Alex Posted May 5, 2010 Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053374 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 ... 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? Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053496 Share on other sites More sharing options...
TeddyKiller Posted May 5, 2010 Author Share Posted May 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200736-ajax-help-could-be-a-list-full/#findComment-1053501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.