Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

PHP MySQL Insert form submission

Guest
Aug 09, 2006 Aug 09, 2006
I'm a teacher, and I'm giving Internet-based assignments to my class. Ideally, I'd like for them to type their findings into a webpage, press "submit," and have the page's contents go into a database so I can review their answers before class. The page would have a couple of paragraphs describing the first problem, then a textarea where students can enter an answer, then a couple more paragraphs describing the second problem, then another textarea for the answer, and so on for about a half dozen problems.

I've set up a database table to capture the answers with three fields: studentID, questionID, and questionAnswer (there are two other tables for students and questions). My initial thought was to ask for the studentID at the top of the page, then have a separate form for each answer, with a "submit" button at the bottom of the page, together with buttons for them to print the page and email themselves a copy of it. But I've learned that it's possible to submit only one form at a time, so I'm not sure this will work. I've thought of perhaps creating a separate "submit" button for each form and making them invisible, then having a button at the bottom that somehow "presses" each submit button in turn. But I don't know how to implement that, nor do I know if that's the best way to do it.

So here are my questions:
(1) Am I on the right track, or is there conceptually a better way to make this work?
(2) How do I get the studentID from the top of the page into the database?
(3) How do I get the questionID (which identifies the question the student is answering) into the database?

This is my first foray into anything like this, and I'm trying to teach myself with David Powers' book PHP for Dreamweaver 8. So far I can view the database records and insert a single record into the database with the Insert Record Wizard. I know I have a ways to go, and I would be grateful for any help and also for any code or pseudo-code.

Thank you.

Joe Herl
TOPICS
Server side applications
439
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 10, 2006 Aug 10, 2006
Joe Herl wrote:
> (1) Am I on the right track, or is there conceptually a better way to make
> this work?

There are three ways that you could do it. One is to put each question
on a separate page, and get the students to answer the questions in a
given order. After inserting the answer to question one, you send them
to the page for question two, and so on.

Another way is to put everything in one form and use arrays to gather
the answers. To create an array, you use square brackets after the name
attribute like this:

name="question[]"

When the form is submitted, you would then need to hand-code loops to
insert each question and answer individually. It's quite easy to do, but
you can't automate it with Dreamweaver.

The third way to do it is less than ideal, but might be worth
considering if you know that you will *always* want only three
questions. Create columns for q1_id, q2_id, q3_id, question1, question2,
and question3. You can then insert everything in a single operation.

> (2) How do I get the studentID from the top of the page into the database?

It needs to be part of the form. As you have discovered, only one form
can be submitted at a time.

> (3) How do I get the questionID (which identifies the question the student is
> answering) into the database?

Again, it's got to be part of the form. I presume that you will pull the
questions from the database. Retrieve the question ID at the same time,
and store it as a hidden field (see page 280 of my book).

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 10, 2006 Aug 10, 2006
Thanks so much, David, for your response. I was surprised and pleased to see my book's author answer my question. I would be lost without your book.

I had a misconception that led to my original question. I had thought you could have only brief items like labels between elements of a form. I didn't think you could have entire paragraphs, so I was trying to create several forms on a page with paragraphs between each one. But then I realized you can't submit several forms at once, so I was stumped. This evening I figured out that entire paragraphs could in fact fit between form elements, so now I'm planning to have one form occupying the entire page and containing the following elements: text field for student's name, paragraphs of text, textarea for answer1, paragraphs of text, textarea for answer2, [etc.], submit button. I think this is what you suggested with your second idea. If it doesn't make sense, please let me know.

I'm not, by the way, getting the questions from the database. They are contained in the paragraphs between each textarea field, so it's not quite so complex as you were thinking.

I think I can figure out the next steps. I also have Luke Welling and Laura Thomson's book *PHP and MySQL Web Development*, so that should help me with any hand coding. I may post again to the forum if I have more questions. Thanks again for the help.

Joe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 11, 2006 Aug 11, 2006
LATEST
Joe Herl wrote:
> Thanks so much, David, for your response. I was surprised and pleased to see
> my book's author answer my question. I would be lost without your book.

Glad to have been of help, Joe.

> I think I can figure out the next steps. I also have Luke Welling and Laura
> Thomson's book *PHP and MySQL Web Development*, so that should help me with any
> hand coding.

It's a long time since I have read that book, but I remember that it was
very good (at least the first edition was).

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines