Jump to content

PHP Dynamic Form Setup ( For Survey Purpose )


FooKelvin

Recommended Posts

Hi All,

 

i would like to create a web page that allow user to feedback. So, i need a page where allow admin to setup a survey form. The more flexible, the best. What i mean is, admin able to create a form. 

A form may contain of check box, Main question, Sub Question and also open text feed back.

 

For now, i able to complete only one module, which is allow admin add only Main Question, i have stuck with the Sub Questions.

 

Here is my sample codes.

 

 

HTML Page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Test Market</title>
        <script src="http://jtable.org/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
        <script src="http://jtable.org/Scripts/jquery-ui-1.10.0.min.js" type="text/javascript"></script>
    </head>
    <script type="text/javascript">
        $(document).ready(function () {
            var max_fields = 10; //maximum input boxes allowed
            var wrapper = $(".input_fields_wrap"); //Fields wrapper
            var add_button = $(".add_field_button"); //Add button ID

            var x = 1; //initlal text box count
            $(add_button).click(function (e) { //on add input button click
                e.preventDefault();
                if (x = x) { //max input box allowed
                    x++; //text box increment
                    $(wrapper).append('<div><input type="text" name="question[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
                }
            });

            $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
                e.preventDefault();
                $(this).parent('div').remove();
                x--;
            })
        });
    </script>
    <style>
        #b-wrapper{
            width: 297px;
            margin: 0 auto;
        }
    </style>
    <body>
        <div id="b-wrapper">
            <form action="save-form.php?mod=add" method="post">
                <table>
                    <tr>
                        <td><lable>Form Name</lable></td>
                        <td><input name="form_name"></input></td>
                    </tr>
                    <tr>
                        <td><lable>Form Description</lable></td>
                        <td><input name="form_desc"></input></td>
                    </tr>
                </table>
                <div class="input_fields_wrap">
                    <div>Question</div>
                    <div><input type="text" name="question[]"></input></div>
                </div>
                <button class="add_field_button">Add More Fields</button>
                <input type="submit"></input>
            </form>
        </div>
    </body>
</html>

SUBMIT FORM PAGE:

<?php

$serverName = "server_one"; //serverName\instanceName
$connectionInfo = array("Database" => "RSA", "UID" => "username", "PWD" => "password");
$conn = sqlsrv_connect($serverName, $connectionInfo);

$formName = $_POST['form_name'];
$formDesc = $_POST['form_desc'];
$formQuestion = $_POST['question'];
$id = $_POST["id"];
foreach( $formQuestion as $key => $text_field ) {
  //print "$formQuetion";  echo '<br>';


//MySqli Insert Query
$result = "INSERT INTO form(form_name,form_description,question) VALUES('$formName','$formDesc','$text_field')";
$stmt = sqlsrv_query($conn, $result);
}

?>

This only able to dynamically add main question, how if there is a sub question? radio button? selection? check box?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.