473,544 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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]
Mar 2 '07 #1
4 2985
ronverdonk
4,258 Recognized Expert Specialist
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 New Member
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
Mar 3 '07 #3
dac
4 New Member
Extract did the trick! Thanks so much!
Mar 4 '07 #4
dac
4 New Member
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]
Mar 4 '07 #5

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

Similar topics

10
19315
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 able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes...
16
2865
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 form order to do that ? My point is for the client to be able to re-read the modified data.
25
10179
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 data in each record, which includes the ID of the father and the mother (who also have records in the table). One record per form. I have a Tab...
4
2430
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. The first class handles the form generation - (this was done using GUI form designer).
4
5312
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 that can be ignored). When I press the button the first time, the click handler hides the textbox. Pressing the button a second time unhides the...
6
1899
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 allow the user to type in a value. Also, I would like the 2nd option only editable if the button for that option is selected. All I can seem to...
9
18006
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 will almost always lead to the user opening a certain child window that's fairly resource intensive to load. Is it possible to load this form in a...
7
2909
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 it shows error page not found on this server i googled and found some people use union and select to hack sites (mysql injection) i guess the...
0
7625
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7389
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5928
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5306
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3427
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1848
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.