如何使用JavaScript创建更多的表单域,然后将值传递给PHP [英] How to create more form fields by using javascript and then passing values to PHP

查看:307
本文介绍了如何使用JavaScript创建更多的表单域,然后将值传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望让用户点击上传更多文件按钮创建一个额外的文件上传表单域,还可以点击另一个名为添加更多姓名的按钮来创建一个额外的输入type ='text'表单字段。

Hi I'd like to make it so that the user can click a "upload more files" button to create an additional file upload form field and they can also click another button called "add more names" to create an additional "input type='text'" form field.

对于可以使用各自按钮创建的文件上传或文本字段应该有一个限制。例如,应该有最多5个文件上传字段和最多5个文本字段。

There should be a limit on how many file upload or text fields that can be created using their respective buttons. For example, there should be a max number of 5 file upload fields and a max number of 5 text fields.

用户点击提交按钮后,脚本将记录文件上传和文本格式字段的总数,并将这些数字传递给PHP。例如,如果有5个文件上传和5个文本表单字段,变量$ numfiles将记录文件上传字段的数量,变量$ numtext将记录文本字段数。

After the user clicks the "submit" button, the script will record the total number of file upload and text form fields and pass those numbers to PHP. For example, if there are 5 file upload and 5 text form fields, the variable $numfiles will record the number of file upload fields and the variable $numtext will record the number of text fields.

一个示例代码很棒,谢谢。

A sample code would be great, thanks.

推荐答案

可能是这样的:

var totalFields = <?=$total_fields?>;
var currentFields = 0;

var addMode = document.getElementById('addMore');
var form = docuement.getElementsByTagName('form')[0];

addMore.onclick = function() {
   if (currentFields >= totalFields) {
      return false;
   }
   var newField = document.createElement('input');
   newField.setAttribute('type', 'file');
   newField.setAttribute('name', 'file'+currentFields);
   form.appendChild(newField);
   currentFields++
}

然后

<?
   foreach ($_FILES as $file) {
      // i guess you know what you should do
   }
?>

没有测试...只是例如

not tested ... just for example

这篇关于如何使用JavaScript创建更多的表单域,然后将值传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆