...

View Full Version : PHP Form Submit help!



Effix
11-18-2010, 08:27 AM
I am trying to make a survey/poll app with jquery mobile. I have made my html/jquery form and I have several of these forms but with a varying number of questions in each form. I can create individual submit.php files to handle each form but that is not a solution if the user wants to create a survey/poll of his own. My question/problem is..
How do I create a single action="submit.php" file that can handle these forms?.
Ive tried to find similar solutions on google but no luck.. help of any kind will be greatly appreciated!
Just so you know im kinda new to this "web programming" so be gentle ;)
Kind Regards
Effix

mlseim
11-18-2010, 01:27 PM
Show us a link to one of these forms.

Also, what happens to the form data?
Is the form data put into a database of some kind? Like MySQL?

How much of the scripting do you already have?

Effix
11-18-2010, 02:04 PM
well I want the data from the form to be put in a mySQL database.
Just to make a simple example
Example #1 [on page 1 survey1.php]

<form name="survey1" method="post" action="formSubmit.php">
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
question 1: <input type="text" name="q1" />
question 2: <input type="text" name="q2" />
<input type="submit" value="Submit" />
</form>

Example #2 [on page 2 survey2.php]

<form name="survey2" method="post" action="formSubmit.php">
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
question 1: <input type="text" name="q1" />
question 2: <input type="text" name="q2" />
question 3: <input type="text" name="q3" />
<input type="submit" value="Submit" />
</form>

I now want the formSubmit.php too be able to handle both form. My problem is that i do not know how to make the formSubmit.php flexible/dynamic so that it can handle the change in the number of fields that need to be added to the database.

for the first form i guess it must look something like this


<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (firstName, lastName, q1, q2)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[q1]','$_POST[q2]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "survey added";

mysql_close($con)
?>

I intentionally left out the jquery stuff in the form since its only used for layout and effect purposes.
I might very well be way off on this since I only started messing about with html, jquery and php yesterday, so please bear with me if there are some stupid errors

mlseim
11-18-2010, 03:31 PM
You use an array for your form variables ...

Like this:
question 1: <input type="text" name="q[]" />
question 2: <input type="text" name="q[]" />
question 3: <input type="text" name="q[]" />

Notice "q[]"

Now, it doesn't matter how many you have.

When PHP processes it, like this:
$questions=$_POST['q'];

$questions is an array, not a single variable.

So, you should be creating a row in your MySQL table for each question,
because you won't know how many questions there are.

Instead of:
firstName, lastName, q1, q2, q3, q4 ... etc.

You will have something more like this:
ID,firstName, lastName, question, number
ID,firstName, lastName, question, number
ID,firstName, lastName, question, number

There should also be some sort of unique (auto-increment ID) for each question,
and a way to know which question number it is.

Here's a brief example ...



// get the array of questions from your form
$questions=$_POST['q'];

mysql_select_db("my_db", $con);

// loop through your array and add a row for each one.
$count=1;
foreach($questions as $question){
$sql="INSERT INTO Persons (firstName, lastName, question, number)
VALUES
('$_POST[firstname]','$_POST[lastname]','$question','$count')";
$count++;
}






.

Effix
11-18-2010, 03:42 PM
Thank you mlseim I really appreciate it. I will give it a try, and let you know if I succeed :D
Kind regards
Effix



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum