Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Form Submit help!

    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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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?

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    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]
    Code:
    <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]
    Code:
    <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 Code:
    <?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

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 ...
    PHP Code:

    // 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++;



    .
    Last edited by mlseim; 11-18-2010 at 02:34 PM.

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you

    Thank you mlseim I really appreciate it. I will give it a try, and let you know if I succeed
    Kind regards
    Effix


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •