drfqfuhej48511519 2014-12-29 01:58
浏览 97

如何在表单中动态创建字段?

I have this simple form

<form action="" method="post" class="smart-green">
    <h1>Contract Form 
        <span>Please fill all the texts in the fields.</span>
    </h1>
    <label>
        <span>Client:</span>
        <input type="text" name="client" placeholder="Your Client Name" />
    </label>

    <label>
        <span>Your Name (Salesman):</span>
        <input type="text" name="salesman" placeholder="Name" />
    </label>
    <label>
        <span>Construction Name:</span>
        <input type="text" name="construction" placeholder="Construction Name" />
    </label>
    <label>
        <span>City:</span>
        <input type="text" name="city" placeholder="City" />
    </label>

    <label>
        <span>Items:</span>
        <input type="text" name="id_item" placeholder="Item" />
    </label>    

     <label>
        <span>&nbsp;</span> 
        <input type="button" class="button" value="Send" /> 
    </label>    
</form>

It is a form for orders. I need to be able to enter a value of items I am going to enter (5 for example) and then I need the "Items" input to appear 5 times. I also need to be able to remove it, so user can correct himself. I don't know how to code in Javascript, so is this even possible? How? I need to access it later as an array in my controller. Thats why I have the name of the field as an array. Thank you in advance for your help.

EDIT: I have just found something similiar to what I need here: http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery I just dont want it to be a button, but a field with a input for a number and for it to not have a limit. I wish I knew Javascript :<

EDIT2: Maybe I wrote it unclear, but I CAN use Javascript, I just dont know how to properly programm in it, so if anyone knows how to achieve that in Javascript, I would be glad.

  • 写回答

3条回答 默认 最新

  • douguanyan9928 2014-12-29 02:27
    关注

    It sounds like you need to ask the user how many items to order, then when the user submits the page to you, save the form data into an array using $_SESSION. The next page that is shown would provide the item text elements for the user to complete.

    You'll basically want to:

    • Start session
    • If $_POST['id_item'] is set
      • Save all variables into $_SESSION
      • Generate item inputs based on id_item using a for loop
    • If items are provided
      • Retrieve previous form data from $_SESSION, marry with new item information
    • Otherwise
      • Display initial form

    (Personal note: Javascript would make this much nicer and easier)

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?