Arsee
Arsee

Reputation: 293

php move data from one form to another but with multiple input ids

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

Answers (4)

madalinivascu
madalinivascu

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

Vijay Rathore
Vijay Rathore

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
}
?>
  1. Create hidden fields in Another form

<?php foreach ($restaurant as $rest_data) { ?> <inpupt type="hidden" name="<?= 'comment_' . $rest_data->id.'-hidden' ?>" id="<?= 'comment_' . $rest_data->id.'_hidden' ?>" /> <?php } ?>

  1. Write a simple javascript , which will duplicate the data from textarea to hidden input field .

function fillInDuplicate(element){ document.getElementById(element.id+'_hidden').value = element.value; }

I hope this helped.

Upvotes: 1

Gurunatha Dogi
Gurunatha Dogi

Reputation: 341

You can do something like this you can set textarea with id and value using this

<?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 } ?>

Then,

While in the hidden part you can use counterarray to loop to same textarea ..Correct me if you need anything else.

Upvotes: 0

Umer Best
Umer Best

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

Related Questions