Hello Everyone I am new to php and doing a project. My Problem is I am working on review page and fetching data from 3 tables using while loop.
The problem is I want to create comment reply system. I am using text area for comment in loop and showing the text area on button click but when I am click on button, each text area gets visible which I don't want. I think problem is due to while loop.
Please suggest me a proper idea.
Thank u in advance.
Php part:
<div class="feedback-list">
<!--img class="doc-img"src="img/doc-img.png" alt="tempimg" height="100" width="100"/>
<div class="feedback-header"-->
<?php
while($row1=mysql_fetch_array($result1))
{
$username1=$row1['username'];
$rtitle=$row1['reviewtitle'];
$rexperience=$row1['experience'];
echo '<div class="feedback"><img class="doc-img" src="img/doc-img.png" alt="temp img" height="100" width="100"/><div class="feedback-header">Rivew by '.$username1.'
<span class="stars" id="star1"><img src="img/stars.png"/></span>
</div>
<p> '.$rtitle.'</p><br/>
<p> '.$rexperience.'</p>
<form action="submitcomment.php" method="post" name="frms">
<!--button type="submit" onclick="showCommentBox()">Reply</button><br/-->
<input type="button" value="Reply" onclick="showCommentBox('.$row1['reviewid'].')"><br/>
<div class="hidden" id="comment">
<!--p>Comments and suggestions:<br><textarea name="comments" rows="3" cols="30" ></textarea><br><br>
<input type="submit" name="sub" value="Confirm"></p-->
</div>
</form>
<span class="read-more">Read More</span>
<span class="added-by">added on 25 March</span>
</div>';}?>
Script:
<script type="text/javascript">
function showCommentBox(x){
//alert(x);
var div=document.getElementById('comment');
div.className='visible';
document.getElementById("comment").innerHTML =
'<br/><textarea maxlength="5000" cols="30" rows="3" name="comments"></textarea>' +
'<input type="submit" name="sub" value="Confirm">';
}
</script>
ALL of your comment boxes have the same DOM ID:
<div class="hidden" id="comment">
^^^^^^^^^^^^
This is not permitted. An ID must be unique across the entire page. Because of this, getElementById() will only ever return ONE element which matches, which is generally the FIRST matching element it finds - there is no point in continuing to search for something of which only one can exist, right?
You probably want something more like
<div class="hidden" id="comment{$id}">
<button onclick="showComment($id);" >
function showComment(id) {
foo = document.getElementById('comment' + id);
}
Related
I have been working on a comment system in php and jQuery. I am using jQuery to slide a form whenever you press reply but instead 2 buttons work for only one form.
I am using php to get the data from a mysql database and display the content it displays it in div tags with a button that says reply when I click on the second button the first div come's down.
PHP CODE:
<?php if(!isset($_SESSION['fname']) && !isset($_SESSION['username']) && !isset($_SESSION['email'])) {
echo "<h1>You must be logged in to comment</h1>";
} else { ?>
<div id="comment">
<form action="createcomment.php?videoid=<?php echo $_GET['id']; ?>" method="post">
Comment: <br /><textarea name="comment" placeholder="Your comment..."></textarea><br />
<input type="submit" name="submit" value="Post Comment" class="btn btn-warning">
</form>
</div>
<?php } ?>
<div id="comments">
<?php $sql = "SELECT * FROM comments WHERE videoid = " . $_GET['id'];
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)) {
if(!empty($result)) {
?>
<div class="comment">
<h5><?php echo $row['name']; ?></h5>
<p><?php echo $row['comment']; ?></p>
<button class="btn btn-primary btn-small">Reply</button>
<div id="reply">
<form action="replycomment.php?videoid=<?php echo $_GET['id']; ?>&commentid=<?php echo $row['id']; ?>" method="post">
Reply Message: <br /><textarea name="comment" placeholder="Your comment..."></textarea><br />
<input type="submit" name="submit" value="Post Reply" class="btn btn-warning">
</form>
</div>
</div>
<?php }else {
echo "<h1>There are no comments.</h1>";
}
}?>
</div>
jQuery code:
<script>
$(document).ready(function(){
$("#reply").hide();
$("#comments > div > button").click(function(){
$("#reply").slideToggle();
});
});
</script>
The jQuery is supposed to hide all of the reply divs and when you click reply the div opens instead the first button works but the last button opens the first one i have no idea what is going on.
Thank you.
There can only be one of each ID in the entire DOM. You have an element with the id of "reply". Since the database is looping through all of the comments, there will be multiple elements with the id of "reply". This syntax is not correct.
Now, if you click on a button to toggle the element with the id of "reply", all of them will toggle. That's not what you want. Change the HTML to:
<button class="btn btn-primary btn-small">Reply</button>
<div class="reply">
<form action="replycomment.php?videoid=<?php echo $_GET['id']; ?>&commentid=<?php echo $row['id']; ?>" method="post">
Reply Message: <br /><textarea name="comment" placeholder="Your comment..."></textarea><br />
<input type="submit" name="submit" value="Post Reply" class="btn btn-warning">
</form>
</div>
Note that I have removed the id, and added the class. There can be multiple classes in a DOM. And so, the jQuery needs to hide all of the classes, so:
$(document).ready(function(){
$(".reply").hide();
// Hides all the elements with class="reply"
});
Now all we need is the button click action. If we just change the hashtag to the dot and make it a class, it still will toggle ALL of the elements with the class of reply. We need to just toggle the element that is right after the clicked button... right?
And so:
$(document).ready(function(){
$("#comments > div > button").click(function(){
$(this).nextAll('.reply:first').slideToggle();
// Gets all the next sibling elements under the same parent, and selects the first one with the class="reply"
});
});
I am trying to create a function that add up number to a given variable each time a button was click.
I have 4 buttons: farm, cave, house, casino
So what I am trying to achieve here is I need to send the random numbers generated by the buttons to a variable that will add up all of the SCORE on the "YOUR GOLD" section. So let's say I click the farm and the cave button so there will be 20 for the cave and 15 for farm for a total of 35 gold already.
Here's my form.php
<div class="wrapper">
<div id="gold">
<form action="process-game.php" method="post" >
YOUR GOLD: <input type="hidden" name="building" value="gold"/>
</form>
</div>
<div class="farm_form">
<h2>Farm</h2>
<form action="process-game.php" method="post" >
<input type="hidden" name="building" value="farm"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
<div class="farm_form">
<h2>Cave</h2>
<form action="process-game.php" method="post">
<input type="hidden" name="building" value="cave"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
<div class="farm_form">
<h2>House</h2>
<form action="process-game.php" method="post">
<input type="hidden" name="building" value="house"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
<div class="farm_form">
<h2>Casino</h2>
<form action="process-game.php" method="post">
<input type="hidden" name="building" value="casino"/>
<input type="submit" value="Find Gold!"/>
</form>
</div>
</div>
Here's my process.php:
<?php
session_start();
if(isset($_POST['building'])){
echo earn_gold();
}
function earn_gold(){
if($_POST['building'] == "farm"){
$gold = rand(10,20);
}else if($_POST['building'] == "cave"){
$gold = rand(5,10);
}else if($_POST['building'] == "house"){
$gold = rand(2,5);
}else if($_POST['building'] == "casino"){
$gold = rand(0,50);
}
return $gold;
}
?>
Any idea how to do this?
I know, you basically wanted a solution in PHP. Still, I could not resist showing you, how easy it would be doing the same in JavaScript/jQuery. Have a look at it or simply ignore it. It is up to you ... ;-)
// define gold amounts for each button (min,max):
var finds={farm:[10,20],cave:[5,10],house:[2,5],casino:[0,50]};
$(function(){
$(':submit').click(function(){ // for all submit buttons: bind the click event to a function ...
var place=$(this).closest('div[id]').attr('id'); // get the id of the buttin's parent div
var fnd=finds[place]; // get the min/max array for the current button
with ($('#gold span')) // locate and use the <span> inside the div with id=gold
text(parseFloat(text()) // get the current value of the span (convert to float)
+fnd[0]+Math.ceil(Math.random()*(fnd[1]-fnd[0]))); // add the gold ...
});
})
div {display:inline-block; width: 120px; border:1px solid grey}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="farm_form" id="gold">
<h2>Your Gold</h2><span>0</span>
</div><br>
<div class="farm_form" id="farm">
<h2>Farm</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="cave">
<h2>Cave</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="house">
<h2>House</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="casino">
<h2>Casino</h2><input type="submit" value="Find Gold!"/>
</div>
If you really need it, I guess you can do it with sessions.
if (isset($_POST['building'])) {
// getting random gold
$earn_gold = earn_gold();
// adding to session called "gold"
$_SESSION['gold'] = (isset($_SESSION['gold']) ? $_SESSION['gold'] + $earn_gold : $earn_gold);
// redirect back to process page
header('Location: process.php');
exit;
}
And then outputting it like so
<div id="gold">
<form method="post" >
YOUR GOLD: <input type="hidden" name="building" value="gold"/>
<?php
// if set, outputting sessions "gold" value
if (isset($_SESSION['gold'])) echo $_SESSION['gold'];
?>
</form>
</div>
When the user click on find gold then submit a form which will add the gold to the total result. For example if the user gets 3 gold then add the gold variable.
$totalGold = 0;
when user.click button then $totalgold +3;
Have one form element and use javascript to assign onclick events to each of the butons and may be use ajax to submit the form dynamically and display the results back on same page.
This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 8 years ago.
I have php comment system like Facebook where user can post comment and reply. when I make 2 reply in a comment one after one then I cannot make 3rd reply in same comment. Also here cannot work emoticons slide and upload toggle. But can reply others comments. I cannot understand this type of jQuery behaviour!!! I also can't understand, can php create this type of problem?
For emoticons display yet I create review window behind textarea And make my textarea transparent. So here I used jQuery bind(). All comment and reply submit by AJAX.
When i going to reply 3rd time Its not work in same comment. But after refresh its work for more 2 reply only.
Here is some related jQuery:
$('.maintbox').on('keyup',function() {
$(this).css('height','auto');
$(this).css('height',Math.max(this.scrollHeight)+'px');
});
$('.replycom').livequery("click",function() {
var VID = $(this).attr('id').replace('replycom','');
$("#parent_id").attr("value", VID);
$("#replycom"+VID).bind("keyup", function() {
$("#replycom"+VID).html($(this).val());
$(".review"+VID).html(smilyMe($("#replycom"+VID).val()));
});
return false;
});
$('.showhide_emobox').livequery("click",function(){
var EID = $(this).attr("id");
$("#emobox"+EID).slideToggle();
});
$(".embtno").livequery("click",function(event){
var emotiText = $(event.target).attr("alt");
var EID = $(this).attr('id').replace('','');
var prevMsg = $("#replycom"+EID).val();
$("#replycom"+EID).val(prevMsg + emotiText);
$(".review"+EID).html(smilyMe($("#replycom"+EID).val()));
$('#emobox'+EID).fadeToggle();
});
my form:
echo'<div class="replyform"><ul>
<form action="" method="post" class="repfrm'.$rows['id'].'" id="prepfrm">
<fieldset id="cmntfs">
<input type="hidden" name="username" id="author" value="'.$_SESSION['username'].'"/>
<input type="hidden" name="url" id="url" value="" />
<div class="maintbox">
<div class="chat">
<div class="review'.$rows['id'].'" id="review"></div>
<textarea name="replycom" id="replycom'.$rows['id'].'" class="replycom" placeholder="Type your comment ..."></textarea>
</div>
<div align="right"><img src="images/envlop.png" width="25" alt="" class="uploadReply" id="'.$rows['id'].'" style="padding:2px;cursor:pointer;" />
<div class="em">
<img src="images/smile.png" id="'.$rows['id'].'" class="showhide_emobox"/>
<div id="emobox'.$rows['id'].'" class="emobox">
<img src="smilies/smile.gif" alt=":)" class="embtno" id="'.$rows['id'].'" />
<img src="smilies/sad.gif" alt=":(" class="embtno" id="'.$rows['id'].'" />
<img src="smilies/biggrin.gif" alt=":-D" class="embtno" id="'.$rows['id'].'" />
</div>
</div>
</div>
<input type="hidden" name="parent_id" id="parent_id" value="'.$rows['id'].'" />
<input type="hidden" name="tutid" id="tutid" value="'.$tutid.'" />
</form>
// Image upload system here
</div>
<button type="submit" name="submit" value="" id="repl'.$rows['id'].'" class="replyfrm">Post Reply</button>
</fieldset>
</ul></div>';
You are using:
$("#replycom"+VID)
There can be only one id of the same name. Your first reply gets unique id, your second reply gets duplicate, and your code stops to work.
I have an issue using jquery to append an create more input tags, so i can collect multiple data and be treated as an array. But each time i process it with php, only one data is sent.Kindly help out.
<div class="half" id="bs_cat">
<form method="POST">
<div>
<div class="label">Create Category</div>
<div class="label" id="bs_cat1"><input type="text" class="form_element" name="bs_category[]" placeholder="Football, handball, basketball e.t.c" /></div><br />
</div>
</div>
<div class="label half center">
<input type="submit" class="button" value="Create Categorie(s)" />
<a class="button" href="#" onclick="$('#bs_cat').append('<div>'+$('#bs_cat1').html()+'Add Another Category
</div>
Here is the php i have processing it
$bs_category = $_POST['bs_category'];
$n = 0;
foreach($bs_category as $c){
if(!empty($c)){
$bs = $afrisoft->antiHacking($c);
$sQl = $afrisoft->dbcountchanges("INSERT INTO sport (bs_categories)
VALUES ('$bs')");
if($sQl > 0) $n++;
}
}
I'll appreciate any help i can get. Thanks
Try putting the name bs_category on the form itself rather than the input field
i have problem like this.
-text | button-
-text | button-
(and so on)
-text | button-
== TEXTAREA ==
eg. i have 20 sets of text and its button. when I press any button, I get the text associated with the button that i pressed, and append its text to textarea.
Here all pair of text and button using same id.
this is the code :
<div id="part" >
<form name='form1' id='form1'>
<div id="teks"> <?php echo $text; ?></div>
<button type="button" id="pos" name="pos" value="POS" onclick="addPOS();"/>POS</button>
</form>
</div>
<div id="part" >
<form name='form1' id='form1'>
<div id="teks"> <?php echo $text; ?></div>
<button type="button" id="pos" name="pos" value="POS" onclick="addPOS();"/>POS</button>
</form>
</div>
//and so on, up to 20 part text-button
<script type="text/javascript">
function addPOS()
{
document.getElementById("posbox").value+='<?php echo $text; ?>';
}
</script>
<div id="box" >
<textarea style="width:420px" name="posbox" id="posbox" rows="4" cols="70"></textarea>
</div>
I just want to add text (text beside button i press) to textarea. How to doing that?
any help will be greatly appreciated. thanks
you have to work with classes instead of ids. id must be unique . So get elements by class document.getElementsByClassName(''); and loop through array to change their values
Change everywhere id="" to class="" and do smth like this
var elements = document.getElementsByClassName('className');
for(var i=0; i<elements.length; i++) {
//your code elements[i].value=
}