Reputation: 293
I have a form where I set textarea
field id by for loop
using php
<?php
foreach ($restaurant as $rest_data) {
?>
<textarea class="form-control" name="<?= 'comment_' . $rest_data->id ?>" id="<?= 'comment_' . $rest_data->id ?>" rows="3" placeholder="Leave a note for Restaurant (optional)" ></textarea>
<?php
}
?>
but I need this field value in another form's hidden field with their respective ids using jQuery or any other method like php session or anything
Upvotes: 1
Views: 236
Reputation: 32354
Create a hidden field for each textarea
$('textarea[name^="comment"]').each(function(i,v){
$('#main-form').append('<input type="hidden" id="comment_id" name="comment_id" value="'+$(v).attr('id')+'">');
});
Trigger this code when you click the button to open the #main-form
Upvotes: 0
Reputation: 603
I am assuming that you want to create duplicate hidden elements for textarea fields of one form to another form . I would suggest you to bind events on keypress of the textarea and populate the value of textarea into hidden fields. 1. Modify your code of generating textarea
<?php
foreach ($restaurant as $rest_data) {
?>
<textarea class="form-control" name="<?= 'comment_' . $rest_data->id ?>" id="<?= 'comment_' . $rest_data->id ?>" rows="3" placeholder="Leave a note for Restaurant (optional)" onkeypress="fillInDuplicate(this)" ></textarea>
<?php
}
?>
<?php
foreach ($restaurant as $rest_data) {
?>
<inpupt type="hidden" name="<?= 'comment_' . $rest_data->id.'-hidden' ?>" id="<?= 'comment_' . $rest_data->id.'_hidden' ?>" />
<?php
}
?>
function fillInDuplicate(element){
document.getElementById(element.id+'_hidden').value = element.value;
}
I hope this helped.
Upvotes: 1
Reputation: 341
You can do something like this you can set textarea with id and value using this
Then,<?php for($i = 0;$i <= 10;$i++){ ?> <input type="hidden" name="counterarray[]" value="<?php echo $i; ?>" /> <textarea name="data<?php echo $i; ?>" id="myid<?php echo $i; ?>"></textarea> <?php } ?>
While in the hidden part you can use counterarray to loop to same textarea ..Correct me if you need anything else.
Upvotes: 0
Reputation: 9
save textarea value in Session and then save this in hidden field and on submission get this value where u submit your form or get this value anywhere...
<input type="hidden" id="comment_id" name="comment_id" value="<?=$SESSION['comment_id']?>">
Upvotes: 0