duanlin1933 2011-03-05 03:26
浏览 29
已采纳

在没有javascript的情况下将项目的多个部分添加到表单中?

Thanks everybody. (For some reason I couldn't up vote answers, so everyone wins today :] thanks again!)


Complete php and javascript novice here, apologize up front for anything half-witted.

I have a portion of a user's profile in which I'd like the user to be able to add additional items of the same thing, with slightly different conditions.

For example, let's say it's favorite books. A fieldset contains some checkboxes for genre and an input text box for the title of a book. After the user checks a genre and fills in a title, they can add the book to their set of favorite books and then have the option to add another. When done adding books, they move on to the next fieldset, complete the form and submit.

How is this done? And more-importantly, is it possible without javascript?

Without JS, I understand this probably entails a lot of reloading of the page to add the items, regardless I'm more confused about how the $_POST data is handled, both before and after the submit.

Sorry for such an open ended question, really just looking for someone to point me in the right direction, as searching for this topic proved to be a bit difficult.

Thanks.

Some Clarification I'm trying to develop an application that is as independent of javascript as possible. In that sense, I don't know if it's possible to add the new items with PHP alone. My sense is the fieldset in question could have it's own submit button, the action of which POSTS to the page itself (no DB interaction, etc), and variables like $book_genre1, $book_title1 are populated in the page. Then, the "official" submit is sent later, which actually adds the POST data, which contains the books array, to the DB. But I don't know if that is a safe procedure or good logic to begin with.

  • 写回答

4条回答 默认 最新

  • dpxyfa4718 2011-03-05 04:03
    关注

    In reply to the above answer, if that is exactly what you need since I seem to have a different idea.

    You simply store each addition in an array stored inside a session variable, and in each page load, parse the data into readable html.

    $_SESSION['form'][] = serialized_form_data; 
    

    On each load,

    foreach ($_SESSION['form'] as $form) {
        unserialize_data_and_create_html(); 
    }
    
    add_new_form_element();
    

    I'm assuming you want to show the user already filled forms so he can deal with them as he wishes.

    This is a better implementation than what I thought of earlier. I wanted to implement a db version.

    Sorry for the delay. I can't comment since I'm mobile (js issues) so I decided to edit instead.

    You can simply use the $_SESSION['form'] for your inserts.

    A simple foreach will work as well. However, remember to sanitize each value properly before inserting it. That's the key.

    If you use prepared statements with binding, you have the advantage of clean input as well as better database performance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 问题遇到的现象和发生背景 360导航页面千次ip是20元,但是我们是刷量的 超过100ip就不算量了,假量超过100就不算了 这是什么逻辑呢 有没有人能懂的 1000元红包感谢费
  • ¥30 计算机硬件实验报告寻代
  • ¥15 51单片机写代码,要求是图片上的要求,请大家积极参与,设计一个时钟,时间从12:00开始计时,液晶屏第一行显示time,第二行显示时间
  • ¥15 用C语言判断命题逻辑关系
  • ¥15 原子操作+O3编译,程序挂住
  • ¥15 使用STM32F103C6微控制器设计两个从0到F计数的一位数计数器(数字),同时,有一个控制按钮,可以选择哪个计数器工作:需要两个七段显示器和一个按钮。
  • ¥15 在yolo1到yolo11网络模型中,具体有哪些模型可以用作图像分类?
  • ¥15 AD9910输出波形向上偏移,波谷不为0V
  • ¥15 淘宝自动下单XPath自动点击插件无法点击特定<span>元素,如何解决?
  • ¥15 曙光1620-g30服务器安装硬盘后 看不到硬盘