简体   繁体   中英

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

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.

在会话中保存 textarea 值,然后将其保存在隐藏字段中,并在提交时获取此值,您可以在其中提交表单或在任何地方获取此值...

<input type="hidden" id="comment_id" name="comment_id" value="<?=$SESSION['comment_id']?>">

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM