Appending textarea data on pressing Enter key to a comment post - javascript

I am creating a commenting system whose structure is like below:
<div id="main_comment_DIV_id_no">
//Some user comment
</div>
<div "reply_DIV">
<textarea name="reply_comment" id="reply_comment" rows="1" style="width:100%">
</textarea>
</div>
I want to append the textarea data to the main comment (also updating the database) part on pressing the Enter key.
I can easily accomplish this if there is only one (or few) DIVs, but here I have n numbers of DIVs so I am searching for a solution to append the nth reply data to the nth main_comment_DIV only.
Any help will be highly useful?

Here is the logic. That might help you.
When you click on enter, call ajax function and pass reply_comment with it. In php side, add that comment into the database and fetch all messages from database. On ajax success,
$('#main_messages_container').html(null);
$('#main_messages_container').html(res);
This will give you all updated messages with the new one. You don't need to append any div container in this case.
DETAILED ANSWER:
$('#enter_key_button').on('click',function(){
$.ajax({
type: 'post',
url: 'process.php',
data: {textarea : textarea},
success: function (res) {
//alert(res);
$('#main_messages_container').html(null);
$('#main_messages_container').html(res);
}
});
});

You can try with the keyup event and jquery class selector. You should not use id as selector, because you said you have n number of main comment dive, so id of n number of divs should not be same.
<body>
<script type="text/javascript">
$(document).ready(function(){
$('[name="reply_comment"]').keyup(function(e){
if(e.which === 13){
var comment = $(e.currentTarget).val(),
commentDiv = $('.main_comment_DIV_id_no');
//you have the comment here, so you can call an ajax request to update it in the database.
$(e.currentTarget).val("");
//if the blank div where you want to add the comment is already present, then write
$(commentDiv[commentDiv.length-1]).text(comment);
// Else no need to add the blank div initially, just create a new div and add the comment there.
$(commentDiv[commentDiv.length-1]).after('<div class="main_comment_DIV_id_no">'+comment+'</div>');
}
})
})
</script>
<div class="main_comment_DIV_id_no">
Some user comment
</div>
<div class="main_comment_DIV_id_no">
Some user comment2
</div>
<div class="main_comment_DIV_id_no">
</div>
<div id="reply_DIV">
<textarea name="reply_comment" id="reply_comment" rows="1" style="width:100%" ></textarea>
</div>
</body>

Related

How to Call Ajax With Same Div Multiple Times

I am calling ajax with below code in my index.php file.
<script type="text/javascript">
$(document).ready(function(){
$("#counting").click(function(){
$.ajax({
type: 'POST',
url: 'https://example.com/update.php',
data: {url: '<?=$url?>'},
success: function(data) {
// alert(data);
//$("p").text(data);
}
});
});
});
</script>
It's working perfectly, but the issue is
I have div called counting multiple times on the page.
example
<div id="counting">
example
</div>
<div id="counting">
example
</div>
Issue - Ajax call only works with first div, not below divs.
How to make it work with all divs with id counting
Id attributes are supposed to be unique. You're only ever going to get the first div called because once the DOM sees that, it's met it's requirement and doesn't search further. I suggest you give you divs unique ids and then use a class that is the same for all of them.
<div id="div1" class="counting_class">
example
</div>
<div id="div2" class="counting_class">
example
</div>
Then your jQuery would look something like this:
$(".counting_class").click(function(){
var id = $(this).attr('id');
//then call ajax based on the individual ID
}

Inserting div value into database

I need to insert a div value into my database, as you can see down there in the code there is the called Total Amount, this is giving me the final price i will have to pay, what i want is to insert the total amount value(which depends on what you are buying) into the database, for example if i pay 90$ i want the column Payment in my table to save the price i paid. I already done some php and im gonna insert it here.
`<div class="box step6">
<h2 class="title">Step 5: Billing contact information</h2>
<h3>Amount:</h3> <div class="pay one"></div></br>
<h3>Early Bird Offer:</h3> <div class="pay two"></div></br>
<h3>Total Amount:</h3> <div class="pay three"></div>
<div class="pay-bt">
<div class="paypal"><h3>Paypal</h3></div>
<div class="card"><h3>Credit Card</h3></div>
<div class="bank"><h3>Bank Transfer</h3></div>
</div>`
If you want to keep the div content as html, you can use jquery JSON.stringify($(".step6").html()) to get the content and encode html chars in php to prevent XSS
Consider using ajax to send your data.
I would suggest using an ID for the div you want to target lets try #pay-three
$.ajax({
url: '/your-php-script.php',
type: 'POST',
data: {
total-amount: $('#pay-three').text(),
}
}).done(function(data){
alert('data sent');
});
In your script:
$total-amount=mysqli_real_escape_string($_GET['total-amount']);

jQuery Within a PHP While Loop

I am using jQuery to reveal an extra area of a page when a button is clicked.
The script is
$(document).ready(function() {
$("#prices").on('click', 'a.click', function() {
$(".hiddenstuff").slideToggle(1000),
$("a.click").toggleClass("faded");
});
});
Then the button is
Enquire or Book
and the newly revealed area is
<div class="hiddenstuff" style="display:none">
<!-- HTML form in here -->
</div>
The problem I have is that the button and "hiddenstuff" div are wrapped in a PHP while loop so they repeat anything between one and six times. When the user clicks on one of the buttons, all the hidden divs are revealed. I would like just the hidden div related to the clicked button to reveal.
I presume that I have to create a javascript variable that increments in the while loop and somehow build that into the script. But I just can't see how to get it working.
EDIT, in response to the comments
The while loop is actually a do-while loop. The code inside the loop is about 200 lines of PHP and HTML. That's why I didn't show it all in my question. In a shortened version, but not as shortened as before, it is
do {
<!-- HTML table in here -->
Enquire or Book
<!-- HTML table in here -->
<div class="hiddenstuff" style="display:none">
<!-- HTML form and table in here -->
</div>
<!-- More HTML in here -->
} while ($row_season = mysql_fetch_assoc($season));
EDIT 2
The final solution was exactly as in UPDATE2 in the reply below.
The easiest thing for you to do is to keep your onclick binding but change your hiddenstuff select. Rather than grabbing all the hiddenstuffs which you are doing now, you can search for the next one [the element directly after the specific button that was clicked].
$(this).next('div.hiddenstuff').slideToggle(1000);
UPDATE
i created a fiddle for you with what I would assume would be similar to the output from your php loop. one change from my early answer was rather than using next(), i put a div around each group as I would assume you would have and used .parent().find()
http://jsfiddle.net/wnewby/B25TE/
UPDATE 2: using IDs
Seeing your PHP loop and your nested tables and potentially complex html structure, I no longer thing jquery select by proximity is a good idea [be it by big parent() chains or sibling finds].
So I think this is a case for injecting your ids. I assume your table structure has an id that you can get from $row_season ( $row_season["id"] )
you can then place it in the anchor:
Enquire or Book
and the same for your hiddenstuff
<div class="hiddenstuff" data-rowid=" . $row_season['id'] . " style="display:none">
and then your js can find it easily
$(document).ready(function() {
$("#prices").on('click', 'a.click', function() {
var rowid = $(this).attr("data-rowid");
$(".hiddenstuff[data-rowid='" + rowid + "']").slideToggle(1000),
$(this).toggleClass("faded");
});
});
updated fiddle
If your structure is something like this:
<div class="container">
Enquire or Book
<div class="hiddenstuff" style="display:none">
<!-- HTML form in here -->
</div>
</div>
You can do your js like this:
$(document).ready(function() {
$("#prices").on('click', 'a.click', function() {
$(this).siblings(".hiddenstuff").slideToggle(1000),
$(this).toggleClass("faded");
});
});
which is similar to William Newby answer, but a close look at your while loop, I'd think you could do this:
$(document).ready(function() {
$("#prices").on('click', 'a.click', function() {
var index = $(this).index();
$(".hiddenstuff")[index].slideToggle(1000),
$(this).toggleClass("faded");
});
});
There are several ways of do it, I hope I was useful.

How to replace an element by textarea using javascript

What I am making is basically is a user profile that has a text box for description added by the user about himself/herself. Although I have a separate page where the user can edit the profile, but I want to provide a feature that when a user hovers over his description box then he sees an edit button.
If the user clicks that button, then he/she can edit the description field there itself, and the backend will be updated using Ajax. The ajax call and the backend processing is simple, but how can I replace the html with a textarea and submit button and again put back the original html using javascript.
Here is what my html look like
<div id="personalText" >
edit
<p id="descText">{{profile.desc}}</p>
</div>
When somebody clicks editButton I want to replace the html inside personalText with a textarea containing the original text in descText, and an update button. And when the user edits the text and click update, I will update the backend model and again put the <a> tag and the <p> tag.
Can anybody help. I seem to know how to do parts of it, but can't figure out how I will replace the original structure on update click.
Instead of creating/destroying DOM elements an option would be to have the edit <textarea/> hidden.
<div id="personalText" >
<div class="display">
edit
<p id="descText">{{profile.desc}}</p>
</div>
<div class="edit" style="display:none;">
<input type="submit" value="Update"/>
<textarea>{{profile.desc}}</textarea>
</div>
</div>
Then your jQuery can simply toggle the elements and handle your ajax post.
$("input[type='submit']").on("click", function() {
$.ajax({
url:"/your/url/"
}).success(function(){
$(".display, .edit").toggle();
$("#descText").html($("textarea").val());
});
return false;
});
Example on jsfiddle
$('#editButton').click(function(){
var text = $('descText').text();
var textArea = $('<textarea></textarea>');
textArea.value = text;
$('#personalText').replaceWith(textArea);
});

Data only to click the div

Good morning, everyone.
I have a doubt. I have a list of div. textarea with text inside it and inside. text for a button event. Want to get only the data from the textarea div that the person clicking the button.
I would be very grateful for the help. thanks
Example code.
<div class="text">
<textarea id="texts" class="texts" rows="5" cols="45" name="textarea"> </ textarea>
<div class="bt" id="1234556">
<div class="button"> Send </div>
</div>
</div>
$('.button').click(function() {
var text = $('#texts').val();
// do something with text
alert(text);
});
Working demo at http://jsfiddle.net/PPcxm/
As long as you have the same structure (and presuming you have more than one example on the page as the button is a class and textarea is an id, the following would work:
$(".button").click(function()
{
var text = $(this).parent().prev().val();
});
Since you have a list of div.text your best bet is to query based upon the structure of your DOM.
$(".button").click(function() {
var $textArea = $(this).parents(".text").find(".texts");
//Do something with $textArea.Val();
});
What we simply do is call .parents() on the current div.button which will allow us to get the div.text element. From there you can simply find your textarea.texts element and get the corresponding value.
Code example on jsfiddle.

Categories