Sticky form won't show POST data after showing MySQL data on page load!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dac
    New Member
    • Mar 2007
    • 4

    Sticky form won't show POST data after showing MySQL data on page load!

    I am quietly going insane on this project. I've never worked on a project like this one before. All my previous sticky forms were for data entry, not editing. I don't know how to display the form with data from the database, allow the user to update it, and then display the form again with POST data. I can get the data out of the database and get the user updates back into the database, but how do I get the filled-out form back to the user again! The form displays again but all the input fields are blank.

    Somehow I have to name the sticky fields with variable names that are identical for the data coming out of the database and for the POST data, and I can't figure out how to do that. Quiz scores come from mysql. Activities come from mysql and are updated by the user. `activities` and `quizzes` are MySQL tables.

    I cut this back to just the relevant parts of the code. I omitted insert & update queries because they're working but left fetch because there's probably something in the way I'm naming variables. I'm hazy on how all these foreach loops work. For instance, I wasn't sure how to grab them later as post variables. Because I think that if I could rename the post variables as simple variables, the first time around I could rename the fetch results as simple variables, and then put the simple variables in the form fields....It sounds so simple but nothing I've tried works! Or if I could put the posts into the $act array that was holding the fetch results....Any advice on the simplest way to straighten out this mess and get it working would be most appreciated!

    I'm limited to php 4.3 and mysql 4.0 right now but will soon be upgrading this app to php 5 and mysql 5, so it has to run on both versions of both programs.


    [PHP]<?php
    if (array_key_exis ts('_submit_che ck', $_POST)) //ON SUBMIT
    {
    include ('./includes/init_session.ph p');

    //Process Activities Post data
    $one_1_1 = $_POST['one_1_1'];

    // update and insert records

    }
    else //ON INITIAL PAGE LOAD
    {
    //prepare query
    $quiz_query = "
    SELECT quiz1, quiz2, quiz3, quiz4, quiz5, quiz6,
    quiz7, quiz8_1, quiz8_3, quiz9, quiz10
    FROM quiz_scores
    WHERE userid='$userid '
    ";

    //query quiz_scores table in database
    $quiz_results = mysql_query($qu iz_query);

    //fetch quiz scores from results
    $quiz_results_a rray = mysql_fetch_ass oc($quiz_result s);

    //initialize variables
    $quiz['key'] = 'value'; //initialize array
    $quiz_total = 0;
    $quiz_count = 0;
    foreach($quiz_r esults_array as $quiz_nbr => $score)
    {
    $quiz[$quiz_nbr] = $score;
    }

    $act_select_que ry = "SELECT * FROM activities
    WHERE userid = '$userid'";
    $act_select_res ults = mysql_query($ac t_select_query) ;
    $act_results_ar ray = mysql_fetch_ass oc($act_select_ results);
    $act['key'] = 'value'; //initialize array
    foreach($act_re sults_array as $field => $value)
    {
    $act[$field] = $value;
    }
    }
    ?>
    <form name="form1" method="post"
    action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <input name="one_1_1" type="checkbox" value="checked"
    <?php if ($act['one_1_1'] == "checked") echo 'checked="$chec ked"'; ?>
    />

    <?php if ($quiz[quiz1]) echo $quiz[quiz1]; ?>
    </form>
    [/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    You have stripped so much code that is is not possible to determine what and where it goes wrong.
    E.g. you don't show the complete form that is to be submitted which makes it hard to say what is passed in the POST array. Also you do not show the echoes of the POSTed values in these <input> fields.

    Btw: if you want to extract values from an associative array into variables with the same name as the keys of that array, have a look at the php EXTRACT command

    Ronald :cool:

    Comment

    • dac
      New Member
      • Mar 2007
      • 4

      #3
      I left out the rest of the form because there are over 100 activity inputs and 11 quizzes. All however have the same coding as these samples. The form pulls data from two other tables as well, so I put just the part that's not working.

      This sample coding pulls the values from the database for the first display of the form. That works--until the user makes updates and, with this coding, I can't get the post values to show unless When I put Post values here instead, then the original display of the form is blank because it's not displaying values from the database.

      Thanks for the reference to EXTRACT, ronverdonc. I've been trying to figure out a way to display values from database and post array in the same form on different viewings, and it sounds like EXTRACT_OVERWRI TE could replace the array from the database with values from the post array, so I can use the same coding in the form each time around. And it would be a lot cleaner and simpler!

      Thank you!
      dac

      Comment

      • dac
        New Member
        • Mar 2007
        • 4

        #4
        Extract did the trick! Thanks so much!

        Comment

        • dac
          New Member
          • Mar 2007
          • 4

          #5
          Even though my code isn't the most elegant, I thought I'd share it here in case anybody else is struggling with how to use a sticky form when the same fields are populated from the database the first time and by post data later. EXTRACT was used to turn MySQL field names into variables with their associated values. Cool! Thanks ronverdonk!

          Quiz scores came strictly from the database (once) and couldn't be changed by students so I used sessions to display them (securely) every time the form was displayed to save server time.

          Activities came from the database but were updated by the user so they had to map both ways--from the database the first time and by post data thereafter.

          Because there are eleven quiz scores and over 100 activities, I show just one variable/field in the quiz_scores table and one in the activities table.

          The form uses simple variables that match the field names in the database and the sessions variables.

          [PHP]<?php

          /* CHECK IF FORM IS SUBMITTED */
          if (array_key_exis ts('_submit_che ck', $_POST))
          {
          /*************** *************** *************
          * PROCESS ACTIVITIES
          *************** *************** ************/
          //Process Activities Post data
          $one_1_1 = $_POST['one_1_1'];

          // update activities table
          $act_update_que ry1 = "
          UPDATE activities
          SET one_1_1 = '$one_1_1'
          WHERE userid = '$userid'
          ";
          $act_update_res ults1 = mysql_query($ac t_update_query1 );
          mysql_free_resu lt($act_update_ results1);

          /*************** *************** *************
          * QUIZ SCORES COME JUST ONCE FROM DATABASE
          * MAKE SESSION DATA INTO SIMPLE VARIABLES
          *************** *************** ************/
          $avg_score = $_SESSION['avg_score'];
          $quiz01 = $_SESSION['quiz01'];
          }

          else //ON INITIAL DISPLAY OF FORM ON PAGE LOAD
          {
          /*************** *************** *************
          * FETCH QUIZ SCORES FROM DATABASE
          *************** *************** ************/
          // grab quiz scores from database
          $quiz_query = "
          SELECT
          quiz01, quiz02, quiz03, quiz04, quiz05,
          quiz06, quiz07, quiz08_1, quiz08_3,
          quiz09, quiz10 FROM quiz_scores
          WHERE userid='$userid '
          ";

          //query quiz_scores table in database
          $quiz_results = mysql_query($qu iz_query);

          //fetch quiz scores from results
          $quiz_results_a rray = mysql_fetch_ass oc($quiz_result s);

          //turn data into simple variables using field names
          extract($quiz_r esults_array);

          //initialize variables
          $quiz_total = 0;
          $quiz_count = 0;

          //total quiz scores and create session variables
          if ($quiz01) //one example to show syntax
          {
          $quiz_total = $quiz_total + $quiz01;
          $quiz_count = $quiz_count + 1;
          $_SESSION['quiz01'] = $quiz01;
          }

          //calculate average quiz score
          $avg_score = ($quiz_total / $quiz_count);
          $avg_score = round($avg_scor e,1);
          $_SESSION['avg_score'] = $avg_score;

          mysql_free_resu lt($quiz_result s);


          /*************** *************** *************
          * FETCH ACTIVITIES FROM DATABASE
          *************** *************** ************/
          $act_select_que ry = "
          SELECT * FROM activities WHERE userid = '$userid'";
          $act_select_res ults = mysql_query($ac t_select_query) ;

          //fetch activities row from results
          $act_results_ar ray = mysql_fetch_ass oc($act_select_ results);

          //turn data into simple variables using field names
          extract($act_re sults_array);

          mysql_free_resu lt($act_select_ results);
          }
          /*************** *************** *************
          * SHOW THE FORM NO MATTER WHETHER
          * DATA COMES FROM USER OR DATABASE
          * SIMPLE VARIABLES COME FROM DB OR SESSIONS
          *************** *************** ************/
          ?>
          <html>
          <head>
          <title>Progre ss Report</title>
          </head>
          <body>
          <p>Ave. Quiz Score: <?php echo $avg_score; ?></p>

          <form name="form1" method="post"
          action="<?php echo $_SERVER['PHP_SELF']; ?>">


          <input
          name="one_1_1"
          type="checkbox"
          id="one_1_1"
          value="checked"
          <?php if ($one_1_1 == "checked")
          echo 'checked="$chec ked"'; ?>
          />

          <p><?php if ($quiz01) echo $quiz01; ?></p>

          </form>
          </body>
          </html>
          [/PHP]

          Comment

          Working...