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=
}
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'm facing a strange problem. I have a HTML form in which user can input the ID number and then submit the form to get the analysis. The problem that I'm facing is that when the user manually inputs the ID, the form doesn't submit, but if the ID is copy pasted in the Text box, and the click the submit button, it works and loads the analysis. Can someone please help.
the form looks like below:
<form action="" method = "GET" class="form-inline">
<div class="input-group" style="padding-top: 10px; padding-bottom: 10px;" >
<span class="input-group-addon">Campaign IDs</span>
<input type="text" name="campaigns" id="campaigns" style="width:220px;" value="<?=$campaigns?>" class="form-control" aria-describedby="basic-addon2">
</div>
<input type='submit' class='btn btn-success' id='idsub'>
</form>
and php is handled like this:
if(isset($_GET["campaigns"]) && $_GET["campaigns"] != null) {
$teststart = true;
$campaign_array = explode(',',$_GET["campaigns"]);
If I type the campaign Id and then submit the button:
the URL submitted remains:
http://localhost/Optimization/insight.php?campaigns=
However, if I copy and paste the campaign ID in the text box, the URL string becomes:
http://localhost/Optimization/insight.php?campaigns=70067553
Try this in your script ::
<?php
if(isset($_GET["campaigns"]))
{
$campaigns = $_GET["campaigns"];
echo "Input field exists";
}
?>
<form method="GET">
<input type="text" name="campaigns" value="<?php echo isset($campaigns)?$campaigns:''; ?>">
<input type='submit' name="Submit" value="Submit" class='btn btn-success' id='idsub'>
</form>
You need to echo to pass php variable to html
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.
I have a form that provides some input boxes and a link with text "Add more". Clicking on the link repeatedly adds the input boxes.
This is my code:
<div class="col-md-2" id="repeatDiv">
<label for="total" class="control-label sr_only"><?php echo lang('total_label'); ?></label>
<?php echo form_input('total',set_value('total'), 'id="total" class="form-control col-md- 4"'.'placeholder='.'"'.lang('placeholder_total').'"'."'");?>
<?php echo form_error('total'); ?>
</div>
<button class="col-md-offset-2 btn btn-primary" id="addmore"><?php echo lang('add_more_btn'); ? />
I want this code to be repeated when a button or the link with text "add more" is clicked.
$('#addmore').on('click', function() {
$('#repeatDiv').clone().insertAfter('#repeatDiv');
});
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);
}