473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

4
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_exists('_submit_check', $_POST)) //ON SUBMIT
{
include ('./includes/init_session.php');

//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($quiz_query);

//fetch quiz scores from results
$quiz_results_array = mysql_fetch_assoc($quiz_results);

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

$act_select_query = "SELECT * FROM activities
WHERE userid = '$userid'";
$act_select_results = mysql_query($act_select_query);
$act_results_array = mysql_fetch_assoc($act_select_results);
$act['key'] = 'value'; //initialize array
foreach($act_results_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="$checked"'; ?>
/>

<?php if ($quiz[quiz1]) echo $quiz[quiz1]; ?>
</form>
[/PHP]
Mar 2 '07 #1
4 2976
ronverdonk
4,258 Expert 4TB
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:
Mar 2 '07 #2
dac
4
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_OVERWRITE 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
Mar 3 '07 #3
dac
4
Extract did the trick! Thanks so much!
Mar 4 '07 #4
dac
4
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_exists('_submit_check', $_POST))
{
/*******************************************
* PROCESS ACTIVITIES
******************************************/
//Process Activities Post data
$one_1_1 = $_POST['one_1_1'];

// update activities table
$act_update_query1 = "
UPDATE activities
SET one_1_1 = '$one_1_1'
WHERE userid = '$userid'
";
$act_update_results1 = mysql_query($act_update_query1);
mysql_free_result($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($quiz_query);

//fetch quiz scores from results
$quiz_results_array = mysql_fetch_assoc($quiz_results);

//turn data into simple variables using field names
extract($quiz_results_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_score,1);
$_SESSION['avg_score'] = $avg_score;

mysql_free_result($quiz_results);


/*******************************************
* FETCH ACTIVITIES FROM DATABASE
******************************************/
$act_select_query = "
SELECT * FROM activities WHERE userid = '$userid'";
$act_select_results = mysql_query($act_select_query);

//fetch activities row from results
$act_results_array = mysql_fetch_assoc($act_select_results);

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

mysql_free_result($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>Progress 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="$checked"'; ?>
/>

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

</form>
</body>
</html>
[/PHP]
Mar 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
16
by: Philippe C. Martin | last post by:
Hi, I am trying to change the data in a form field from python. The following code does not crash but has no effect as if "form" is just a copy of the original html form. Must I recreate the...
25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
4
by: Tony W | last post by:
Hi, I am trying to write a simple application to retrieve data from the Windows registry and insert it into textboxs on a windows form. So far I have one namespace containing two classess. ...
4
by: Lee Chapman | last post by:
Hi, Can anyone tell me why in the code below, the call to ClearChildViewState() has no effect? To paraphrase the code: I'm using view state. I have a textbox and a submit button (and a label...
8
by: PAPutzback | last post by:
How do I keep the form up.
6
by: drec | last post by:
I am just learning Javascript and I would like to create a basic form that gives me two options. This will be using either checkbox or radio input type, however I would like the second option to...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
7
by: php_mysql_beginer911 | last post by:
Hi .. hope someone will help i am trying to figure it out why i cannot post string "union select" every time i try to post data which content union and select .. the page doesn't get posted and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.