Jquery / PHP动态表单 - 创建和提交

时间:2014-11-24 16:55:29

标签: php jquery html

这是一个2部分问题。

我有一个包含多个问题的表单,每个问题包含多个选项。

我的表格:

<?php
    session_set_cookie_params(0);
    session_start();
    include_once('database/db_connection.php');

    $username = $_SESSION['user_id'];
    $userid = getUserId($username);
?>

<!DOCTYPE html>
<html>
    <head>
        <title><?php echo $username?> Create Poll</title>
        <meta charset="UTF-8">
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script type="text/javascript" src="scripts/poll_create.js"></script>
        <link rel="stylesheet" href="style/pollCreateStyle.css">
    </head>
    <body>
        <div id="createPollContainer">
        <p>
            <label for="poll_title">Title</label>
            <input type="text" placeholder="Poll Title" name="poll_title" required><br>
        </p>
        <p>
            <label for="poll_image">Image URL</label>
            <input type="text" placeholder="Image URL" name="poll_image" required><br>
        </p>
        <p>
            <label for="poll_des">Description</label>
            <input type="text" placeholder="Description" name="poll_des" required><br>
        </p>
        <div id="question_container">
            <fieldset id="new_question"><legend>Question</legend>
            <p>
                <label for="question_title">Title</label>
                <input type="text" placeholder="Question Title" name="question_title" required><br>
            </p>
            <div id="choice_container">
                <p id="new_choice">
                    <label for="choice">Choice</label>
                    <input type="text" placeholder="Choice Description" name="choice" required><br>
                <p class="add_choice"><a href="#"><span>Add Choice</span></a></p>
            <div>
            </fieldset>
            <p id="add_question"><a href="#"><span>Add Question</span></a></p>
        </div>
        <input type="submit" value="Submit" name="submit_val">
        </div>
    </body>
</html>

以及添加问题和选择的脚本

var questionLine;
var choiceLine;

$(document).ready(loadDocument);

function loadDocument() {   
    saveLine();

    $("#add_question").click(addQuestion);
    $(".add_choice").click(addChoice);
}

function addQuestion(){
    $(this).before(questionLine.clone());
}

function addChoice(){
    $(this).before(choiceLine.clone());
}

function saveLine(){
    questionLine = $("#new_question").clone();
    choiceLine = $("#new_choice").clone();
}

问题1:我可以添加任意数量的问题,但只能在第一个问题中添加选项。我该如何解决这个问题?

问题2:收集和提交所有内容的最佳方式是什么?我知道如何用固定表格来做,但我找不到帮助我的例子或其他答案。如果有帮助,我的数据库结构是:

CREATE TABLE users(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username VARCHAR,
    password VARCHAR,
    name VARCHAR,
    email VARCHAR
);

create TABLE polls(
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    title VARCHAR NOT NULL,
    imageurl VARCHAR NOT NULL,
    description VARCHAR, 
    privateStatus INTEGER NOT NULL CHECK (privateStatus = 0 or privateStatus=1),
    owner INTEGER REFERENCES users (id)
);

create TABLE questions(
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    title VARCHAR NOT NULL,
    timesAnswered INTEGER NOT NULL DEFAULT 0,
    poll INTEGER REFERENCES polls (id)
);

create TABLE choices(
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    title VARCHAR NOT NULL,
    timesAnswered INTEGER NOT NULL DEFAULT 0,
    question INTEGER REFERENCES questions (id)
);

0 个答案:

没有答案
相关问题