Problem with Script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerim79

    Problem with Script

    Here it is:

    <?php

    if($_SERVER['REQUEST_METHOD ']=='POST'){

    $Number=$_POST['Number'];
    $Email=$_POST['Email'];
    $Number2=0;
    $error=0;

    $Variable1=$_PO ST['Variable1'];
    $Variable2=$_PO ST['Variable2'];
    $Variable3=$_PO ST['Variable3'];
    $Variable4=$_PO ST['SVariable4'];
    $Variable5=$_PO ST['Variable5'];
    $Variable6=$_PO ST['Variable6'];
    $Variable7=$_PO ST['IVariable7'];
    $Variable8=$_PO ST['Variable8'];
    $Check=$_POST['Check']; //We will use this to
    determine if we
    should do data checks

    if (empty($Variabl e1)){
    $msg1=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e2)){
    $msg2=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e3)){
    $msg3=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e4)){
    $msg4=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e5)){
    $msg5=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e6)){
    $msg6=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e7)){
    $msg7=" * You have not answered this
    question.";
    $error++;
    }

    $Variable9=$_PO ST['Variable9'];
    $Variable10=$_P OST['Variable10'];
    $Variable11=$_P OST['Variable11'];
    $Variable12=$_P OST['Variable12'];
    $Variable13=$_P OST['Variable13'];
    $Variable14=$_P OST['Variable14'];
    $Variable15=$_P OST['Variable15'];
    $Variable16=$_P OST['Variable16'];
    $Variable17=$_P OST['Variable17'];

    for ($i=0; $i<$Number; $i++){
    if (empty($Variabl e9[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e10[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e11[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e12[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e13[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e14[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e15[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e16[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    if (empty($Variabl e17[$i])){
    $msg[i]=" * You have not answered this
    question.";
    $error++;
    }
    } //closes FOR loop

    if ($error>0){
    echo "<form action=\"survey .php\" method=\"POST\" >
    <input type=\"hidden\" name=\"Email\" value=\"$Email
    \" />
    <input name=\"Number\" type=\"hidden\" value=\" $Number
    \" />
    <table width=\"700\" border=\"0\" cellpadding=\"0 \"
    align=\"center
    \">
    <tr><td colspan=\"25\"> <strong><center >PART 1 -
    SURVEY</center></
    strong<br/></td></tr>
    <tr><td align=\"right\" ></tr>
    <tr><td align=\"right\" ></td></tr>
    <tr><td colspan=\"25\"> Please answser the following
    questions:<br/
    ><br/></td </tr>
    <tr><td colspan=\"25\" bgcolor=\"#CCCC CC\">1. Question
    1?</
    td </tr>
    <tr>
    <td align=\"center\ "><input name=\"Variable 1\" type=
    \"radio\" value=
    \"Excellent\ " /Excellent</td>
    <td align=\"center\ "><input name=\"Variable 1\" type=
    \"radio\" value=
    \"Good\" /Good</td>
    <td align=\"center\ "><input name=\"Variable 1\" type=
    \"radio\" value=
    \"Average\" /Average</td>
    <td align=\"center\ "><input name=\"Variable 1\" type=
    \"radio\" value=
    \"Poor\" /Poor</td>
    <td colspan=\"21\"> $msg1</td></tr>
    <tr><td colspan=\"25\" bgcolor=\"#CCCC CC\">2.
    Question 2?</td></tr>
    <tr>
    <td align=\"center\ "><input name=\"Variable 2\" type=
    \"radio\" value=
    \"Excellent\ " /Excellent</td>
    <td align=\"center\ "><input name=\"Variable 2\" type=
    \"radio\" value=
    \"Good\" /Good</td>
    <td align=\"center\ "><input name=\"Variable 2\" type=
    \"radio\" value=
    \"Average\" /Average</td>
    <td align=\"center\ "><input name=\"Variable 2\" type=
    \"radio\" value=
    \"Poor\" /Poor</td>
    <td colspan=\"21\"> $msg2</td></tr>
    <tr><td colspan=\"25\" bgcolor=\"#CCCC CC\">3.
    Question 3?</td></tr>
    <tr>
    <td align=\"center\ "><input name=\"Variable 3\" type=
    \"radio\" value=
    \"Excellent\ " /Excellent</td>
    <td align=\"center\ "<input name=\"Variable 3\"type=
    \"radio\" value=
    \"Good\" /Good</td>
    <td align=\"center\ "><input name=\"Variable 3\" type=
    \"radio\" value=
    \"Average\" /Average</td>
    <td align=\"center\ "><input name=\"Variable 3\" type=
    \"radio\" value=
    \"Poor\" /Poor</td>
    <td colspan=\"21\"> $Variety</td>
    </tr>
    <tr><td colspan=\"25\" bgcolor=\"#CCCC CC\">4.
    Question 4?</td></
    tr>
    <tr>
    <td align=\"center\ "><input name=\"Variable 4\" type=
    \"radio\" value=
    \"Excellent\ " /Excellent</td>
    <td align=\"center\ "><input name=\"Variable 4\" type=
    \"radio\" value=
    \"Good\" /Good</td>
    <td align=\"center\ "<input name=\"Variable 4\" type=
    \"radio\" value=
    \"Average\" /Average</td>
    <td align=\"center\ "><input name=\"Variable 4\" type=
    \"radio\" value=
    \"Poor\" /Poor</td>
    <td colspan=\"21\"> $msg4</td>
    </tr>
    <tr><td colspan=\"25\" bgcolor=\"#CCCC CC\">5.
    Question 5?</td></tr>
    <tr>
    <td align=\"center\ "><input name=\"Variable 5\" type=
    \"radio\" value=
    \"Excellent\ " /Excellent</td>
    <td align=\"center\ "><input name=\"Variable 5\" type=
    \"radio\" value=
    \"Good\" /Good</td>
    <td align=\"center\ "><input name=\"Variable 5\" type=
    \"radio\" value=
    \"Average\" /Average</td>
    <td align=\"center\ "<input name=\"Variable 5\" type=
    \"radio\" value=
    \"Poor\" /Poor</td>
    <td colspan=\"21\"$ msg5</td>
    </tr>
    <tr>
    <td colspan=\"25\" bgcolor=\"#CCCC CC\">6. Question 6?
    </td>
    </tr>
    <tr>
    <td align=\"center\ "><input name=\"Variable 6\" type=
    \"radio\" value=
    \"Excellent\ " /Excellent</td>
    <td align=\"center\ "<input name=\"Variable 6\" type=
    \"radio\" value=
    \"Good\" /Good</td>
    <td align=\"center\ "><input name=\"Variable 6\" type=
    \"radio\" Value=
    \"Average\"/Average</td>
    <td align=\"center\ "><input name=\"Variable 6\" type=
    \"radio\" value=
    \"Poor\" /Poor</td>
    <td colspan=\"21\"> $msg6</td>
    </tr>
    <tr>
    <td colspan=\"25\" bgcolor=\"#CCCC CC\">7. Question 7?
    </td></tr>
    <tr>
    <td align=\"center\ "><input name=\"Variable 7\" type=
    \"radio\" value=
    \"Excellent\ " /Excellent</td>
    <td align=\"center\ "><input name=\"Variable 7\" type=
    \"radio\" value=
    \"Good\" /Good</td>
    <td align=\"center\ "><input name=\"Variable 7\" type=
    \"radio\" value=
    \"Average\" /Average</td>
    <td align=\"center\ "<input name=\"Variable 7\" type=
    \"radio\" value=
    \"Poor\" /Poor</td>
    <td colspan=\"21\"> $msg7</td>
    </tr>
    </table>
    <br/
    ADDITIONAL
    COMMENTS: <br/><div align=\"center\ "><textarea name=\"Variable 8\"
    cols=
    \"83\" rows=\"10\"></textarea></div><div align=\"center\ "><br/>
    <br/>
    <strong>
    <center>";

    if ($Number>0){
    echo "<table width=\"700\" border=\"0\"
    cellpadding=\"5 \">
    <tr><td colspan=
    \"10\"><strong> <center>PART 2 - SURVEY
    </center</strong></td></tr><tr><td colspan=\"10\"</td></tr>
    <tr><td colspan=\"10\"> <div align=
    \"center\">Plea se fill out
    each section below. </div></td></tr></table><br/></center></strong>";
    $SeminarNumber= 1;

    while ($Number2<$Numb er){
    echo "<table width=\"700\"
    border=\"0\" cellpadding=\"0 \" align=
    \"center\">
    <tr><td colspan=\"60\" align=
    \"center\"><str ong>Seminar
    Number
    $SeminarNumber</strong></td></tr>
    <tr><td align=\"right
    \">Variable9 : </td><td colspan=\"5\">< input
    type=\"text\" name=\"Variable 9[]$Number2\" value=\"\" width=\"250\"/
    ></td></td><td colspan=\"21\">
    $msg[$Number]</td></tr>
    <tr><td colspan=\"60\"</td></
    tr>
    <tr><td colspan=\"60\" bgcolor=
    \"#CCCCCC\"> 1. Question 10?</td></
    tr>
    <tr>
    <td align=\"center\ "><input
    name=\"Variable 10]$Number2\" type=
    \"radio\" value=\"Excelle nt\" />Excellent</td>
    <td align=\"center\ " ><input
    name=\"Variable 10[]$Number2\" type=
    \"radio\" value=\"Good\" />Good</td>
    <td align=\"center\ " ><input
    name=\"Variable 10[]$Number2\" type=
    \"radio\" value=\"Average \" />Average</td>
    <td align=\"center\ " ><input
    name=\"Variable 10[]$Number2\" type=
    \"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\"> $msg[$Number]</
    td>
    </tr>
    <tr><td colspan=\"60\"
    bgcolor=\"#CCCC CC\">2. Question 11?</td></
    tr>
    <tr>
    <td align=\"center\ "><input
    name=\"Variable 11[]$Number2\" type=
    \"radio\" value=\"Excelle nt\" />Excellent</td>
    <td align=\"center\ "><input
    name=\"Variable 11[]$Number2\" type=
    \"radio\" value=\"Good\" />Good</td>
    <td align=\"center\ "><input
    name=\"Variable 11[]$Number2\" type=
    \"radio\" value=\"Average \" />Average</td>
    <td align=\"center\ "><input
    name=\"Variable 11[]$Number2\" type=
    \"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\"$ msg[$Number]
    </td>
    </tr>
    <tr><td colspan=\"60\"
    bgcolor=\"#CCCC CC\">3. Question 12?</td></
    tr>
    <tr>
    <td align=\"center\ "><input
    name=\"Variable 12[]$Number2\" type=
    \"radio\" value=\"Excelle nt\" />Excellent</td>
    <td align=\"center\ "><input
    name=\"Variable 12[]$Number2\" type=
    \"radio\" value=\"Good\" />Good</td>
    <td align=\"center\ "><input
    name=\"Variable 12[]$Number2\" type=
    \"radio\" value=\"Average \" />Average</td>
    <td align=\"center\ "><input
    name=\"Variable 12[]$Number2\" type=
    \"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\"> $msg[$Number]</
    td></tr>
    <tr><td colspan=\"60\"
    bgcolor=\"#CCCC CC\">4. Question 13
    </td></tr>
    <tr>
    <td align=\"center\ "><input
    name=\"Variable 13[]$Number2\" type=
    \"radio\" value=\"Excelle nt\" />Excellent</td>
    <td align=\"center\ "><input
    name=\"Variable 13[]$Number2\" type=
    \"radio\" value=\"Good\" />Good</td>
    <td align=\"center\ "><input
    name=\"Variable 13[]$Number2\" type=
    \"radio\" value=\"Average \" />Average</td>
    <td align=\"center\ "><input
    name=\"Variable 13[]$Number2\" type=
    \"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\"> $msg[$Number]</
    td>
    </tr>
    <tr><td colspan=\"60\"
    bgcolor=\"#CCCC CC\"5.Question 14? </
    td></tr>
    <tr>
    <td align=\"center\ "><input
    name=\"Variable 14[]$Number2\" type=
    \"radio\" value=\"Excelle nt\" />Excellent</td>
    <td align=\"center\ "><input
    name=\"Variable 14[]$Number2\" type=
    \"radio\" value=\"Good\" />Good</td>
    <td align=\"center\ "><input
    name=\"Variable 14[]$Number2\" type=
    \"radio\" value=\"Average \" />Average</td>
    <td align=\"center\ "><input
    name=\"Variable 14[]$Number2\" type=
    \"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\"> $msg[$Number]</
    td></tr>
    <tr><td colspan=\"60\"
    bgcolor=\"#CCCC CC\">6.Questio n 15?. </td>
    </tr>
    <tr>
    <td align=\"center\ "><input
    name=\"Variable 15[]$Number2\" type=
    \"radio\" value=\"Excelle nt\" />Excellent</td>
    <td align=\"center\ "><input
    name=\"Variable 15[]$Number2\" type=
    \"radio\" value=\"Good\" />Good</td>
    <td align=\"center\ "><input
    name=\"Variable 15[]$Number2\" type=
    \"radio\" value=\"Average \"/>Average</td>
    <td align=\"center\ "><input
    name=\"Variable 15[]$Number2\" type=
    \"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\"> $msg[$Number]</
    td></tr>
    <tr><td colspan=\"60\"> <hr/></
    td></tr>
    </table><br/>";

    $Number2++;
    $SeminarNumber+ +;
    } //Closes While Loop

    echo "<table width=\"700\" border=\"0\" cellpadding=\"0 \" align=
    \"center\">
    <tr><td colspan=\"60\" bgcolor=\"#CCCC CC\">Question 16: </td>
    </tr>
    <tr><td colspan=\"60\" align=\"center\ "><div align=\"center
    \"><textarea name=\"Variable 16[]\" cols=\"83\" rows=\"5\"></textarea>
    </div></td></tr>
    <tr><td<input type=\"hidden\" name=\"Check\" value=\"1\"</
    td></tr>
    <tr><td bgcolor=\"#CCCC CC\">ADDITIONA L COMMENTS: </td></tr>
    <tr><td><div align=\"center\ "><textarea name=\"Variable 17[]\"
    cols=
    \"83\" rows=\"10\"></textarea></div><br/></td></tr>
    </table>
    <div align=\"center\ "><br/>If you are interested in receiving
    information or materials, please email me at <a href=
    \"mailto:e...@e mail.com\">e... @email.com</a>.</div>
    <div align=\"center\ "><br/><input name=\"Submit\" type=\"Submit
    \"
    value=\"Submit\ ">
    <input name=\"Reset\" type=
    \"Reset\" value=\"Reset\" ></div>
    </form>";
    } //closes the IF ($Number2>0) loop
    for if the user entered a
    number for seminars

    } //closes the IF ($error>0) loop
    else{
    include('submit .php');

    }
    } //closes the check POST validation IF

    else{
    echo"You can not access this page directly. To complete the
    survey,
    go to
    <br/><a href =\"http://www.website.com/index.php\">Sur vey
    Form</a>";
    }
    ?>

    I tried formatting it for easy viewing, don't know if I succeeded. A
    brief explanation is in order. The first page of the form asks a user
    for a number and their email address. The number and email address are
    passed to the second page of the form, which is the script listed
    above. First thing we do is declare local variables. Next, we check
    for any empty variables. If there are, $error>0 and we display the
    form. If $error<=0, we call the submit.php page which writes
    everything to the database.

    The FOR loop in the middle determines if a customer entered a number
    on the previous page. If there is a number, then it executes the next
    block of code. If not, we skip this section. This way, if the customer
    enters 0 or nothing for the number on the first page, then we don't
    clutter up the screen with those questions. The WHILE loop right below
    there, repeats certain questions for the number of times the user
    entered on the first page. So there is a set of questions that is
    asked once of all users, and a set of questions that is asked a number
    of times, depending on what number the customer entered. The form
    repeats the questions the correct number of times.

    I am trying to accomplish some form of data verification. For the
    first set of question, data verification works correctly, by not
    allowing you to move further until every question is answered.
    However, it automatically displays the error messages when the page
    loads. I can't use an inline if($_SERVER['REQUEST_METHOD ']=='POST')
    { echo $msg[i]; } method, since the form is always posted. Inside of
    the WHILE loop, I can only get it to verify $Variable9. It doesn't
    even check the other fields . Since the same question may be asked 10
    times, I need to specify which occurrence of the question was not
    answered. That is why I use the FOR loop.

    If you know of a way to make the current code work, or if you have a
    better idea of how to do this from top to bottom, please let me know.
    I would like to also be able to carry over the user's selections, so
    that they don't have to recheck each box, if they only left off one.

  • SUtrap

    #2
    Re: Problem with Script

    Hi,

    I've done something similar some time ago, but my structure was a bit
    different (I wanted to be very flexible):
    1. I used an array to have all questions saved, like:
    $question[x]="Question X"; Also all other values (your radio
    values) were saved as arrays!
    That way you dont have to look at the whole page to change some
    text! My page was in english, german and french, so I had three files
    each for one language.
    2. I've used functions to generate the questions, like
    getQuestion($nu mber), which returned the whole question + html string
    for the buttons.

    3. I've never used empty() call, only isset(); As far as I know it
    wont write any warning about unset variables. dont know if empty()
    does the same.
    4. Dont save $Variable1=$_PO ST['Variable1']; ! As long as you dont
    manipulate the data you dont need this!

    5. Most of your html code is the same only the values change. you
    could use such code:

    .....html stuff...
    <?php for($i=0;$i<siz eof($questions) ;$i++) { ?>
    ---- your question x, as function or embedded php ----
    <?php } ?>
    .....html stuff.....

    6. Maybe you can also separate the html stuff completely from PHP,
    with SMARTY or other template.

    I hope that gives you some hints ;)

    Comment

    • Jerim79

      #3
      Re: Problem with Script

      For the moment, the biggest question before me, is how to make it so
      that the user doesn't have to recheck each line if the form fails to
      verify. Here is an idea. What if upon submission, I were to exclude
      any questions from the form that were already answered correctly? Such
      that the first time through, it prints out all questions. Upon
      submission it verifies each variable. If all the variables contain
      data, then we move on to the next page. If a variable contains no
      data, then we repost the question to the user. Within the "IF
      ($error>0)" I could wrap each occurrence of a question with an IF
      statement to see if data for that variable has already been entered.
      If not, then it prints the question. If so, it skips on to the next
      IF.

      You say not to use $Variable1=$_PO ST['Variable1']. But after
      correcting the problem questions, how would I pass along the value for
      the correct questions as well? Usually I use hidden buttons on the
      form to send along values. Let's say I have three radio groups, A, B
      and C. User picks an answer for A and C but skipped B. When the form
      validates, and reprints question B, how would I include the value of A
      and C so that when the user clicks submit for the second time, the
      previous values are not over written? Does isset() maintain its status
      throughout all submissions? (As far as I know, PHP can only hold one
      form at a time. If you submit another form, then the values of that
      form overwrite the previous form.)

      Comment

      • ELINTPimp

        #4
        Re: Problem with Script

        On Apr 11, 3:38 pm, "Jerim79" <m...@hotmail.c omwrote:
        For the moment, the biggest question before me, is how to make it so
        that the user doesn't have to recheck each line if the form fails to
        verify. Here is an idea. What if upon submission, I were to exclude
        any questions from the form that were already answered correctly? Such
        that the first time through, it prints out all questions. Upon
        submission it verifies each variable. If all the variables contain
        data, then we move on to the next page. If a variable contains no
        data, then we repost the question to the user. Within the "IF
        ($error>0)" I could wrap each occurrence of a question with an IF
        statement to see if data for that variable has already been entered.
        If not, then it prints the question. If so, it skips on to the next
        IF.
        Does isset() maintain its status
        throughout all submissions? (As far as I know, PHP can only hold one
        form at a time. If you submit another form, then the values of that
        form overwrite the previous form.)
        You're correct about having to retain the state of those variables
        that were answered "correctly" . You can do this like you initially
        suggested, passing it back and forth from server to client each time
        using $_POST or $_GET using hidden values in the form or using a
        cookie on the client...but another way to do it, to save network
        resources and the like, is to save it on the server. If you're using
        a database, I suggest using transactions. You can "store" the
        variables in the transaction until the user get's them all "correct"
        and then commit the transaction. That would also give you the added
        benefit of using rollbacks if the transaction fails at a later part.
        Another way is to use sessions and to save those variables deemed
        "correct" as a session variable. That way, the only thing that you
        would need to send back and forth from the server to client would be
        the session ID.

        Comment

        Working...