PDA

View Full Version : Taking multiple html fields & placing in database



lankanmon
09-02-2011, 08:55 AM
Hi guys,
I am creating a poll like thing ware one can create a questioner including a question "question_1" and then multiple answers "question_1_1, question_1_2, question_1_3, question_1_4, question_1_5". assume that the "question_1_..." are the names of the input fields.

So what i want to know is what the best method (using php) is for taking that content that was submitted and placing it in a database table, so all of it can be in a single row and in a single field (within the same box).

I want to know if its possible to use arrays and if do how should i tackle this. i want to use arrays or something similar because there could be multiple questions and more or less answered (so can not be fixed).

I want to do it to some type of standard, so i know this method for future use.

I have been trying to figure our a way for a couple of days and sifted through many webpages and forums but cant find anything useful.

Any and all help will be appreciated.

sunfighter
09-02-2011, 08:39 PM
See this article:
http://php.about.com/od/finishedphp1...e_poll_php.htm

And for script that doesn't require a database:
http://www.dbscripts.net/poll/

They may help.

lankanmon
09-03-2011, 06:42 AM
Those links are going to help me with some other problems i had, so thanks... but it wont help me with this problem because i can't figure out how to make the php file gather all the dynamic content (submissions) into variable which i can then put into a single string and store into a database.

The problems is... i hv multiple questions and each question can have multiple answers which the user gets to decide since it is expandable using javascript.

now i don't know how to get the content submitted since i don't know how to assign a variable to each since they may or may not exist on submission.

I thought arrays may be the best choice but i don't know how to make the arrays variable name change while in a loop.

is someone that knows can help me out... i would greatly appreciate it.

In short: i am trying to make something like forum polls ware they allow the user to create the questions and the answers, and i want to know how to handle it.

sunfighter
09-03-2011, 05:36 PM
i don't know how to get the content submitted since i don't know how to assign a variable to each since they may or may not exist on submission.

You need to study FORMS. They are basic to programming. I just get the feeling that your pasting together other peoples code trying to get you site working. You should take some time to study html and php.

http://www.w3schools.com/html/default.asp

I'm not being an ***, I'm trying to help.:thumbsup:

lankanmon
09-03-2011, 08:51 PM
You need to study FORMS. They are basic to programming. I just get the feeling that your pasting together other peoples code trying to get you site working. You should take some time to study html and php.

http://www.w3schools.com/html/default.asp

I'm not being an ***, I'm trying to help.:thumbsup:

Thank you for that, but i know how to use forms... i have much experience with them. but this is the first time using dynamic forms... which i hv working fine.. and submitting properly. All my code is my own and i don't ever copy and pate others code... that's just not cool.

I just want to get an idea on how to handle dynamic forms with php which i have not yet encounters and that's why i was asking you for some assistance... i am not asking for code... just something for maybe links to references or other way to handle the code.

BTW: this is kindah what i have so far:

assuming that the html form is expendable (see comments):

<form name="create_form" action="" method="POST">
<p>Poll Title</p>
<input type="text" name="m_title" id="m_title" class="text" />
<p>Description (optional)</p>
<input type="text" name="m_desc" id="m_desc" class="text" />
<p>Question 1: <input type="text" name="question_1" id="question_1" class="text" /></p>
<p>Accepted Answers:</p>
<input type="text" name="answers_1_1" id="answers_1_1" class="text" />
<input type="text" name="answers_1_2" id="answers_1_2" class="text" />
<!-- more answers can be added here using JS -->
<input type="button" style="margin-top:10px;" value="Add Possible Answer" onClick="addPossAnswer('answers_1','answers_1_');" />
<!-- more questions can be added here using JS -->
<input type="button" style="margin-top:10px;" value="Add Question" onClick="addQuestion('questions','question_');" />
<input name="create" type="hidden" value="true">
<input type="submit" value="Create">
</form>


and here is what i hv for the handler (the echo s are there so i can see if its running the way i assime it will




if (isset($_POST['create'])) {
if ($_POST['create'] == 'true') {
$title = $_POST['m_title'];
$desc = $_POST['m_desc'];
$question = 1;
$answer = 1;
$f_safe = 1;
$run = 1;
$answer_active = 1;
$question_active = 1;
$entire_active = 1;
echo "Title: ". $title ."</br>";
echo "Description: ". $desc ."</br>";
do {
do {
echo "Question: ".$question ."</br>";
do {
$answer_st[$answer] = $_POST['answers_'.$question."_".$answer];
echo $answer_st[$answer] ."</br>";
$answer++;
if (!isset($_POST['answers_'.$question."_".$answer])) {
$answer_active = 0;
$answer = 1;
}
} while ($answer_active != 0);
$question++;
if (!isset($_POST['question_'.$question])) {
$question_active = 0;
}
} while ($question_active != 0);
if ($question_active != 0) {
$entire_active = 0;
}
} while ($entire_active != 0);
}
}


also when it gets to the second question it gets the first answer and then skips the rest... and idea why?