|
 |
|
 |
07-06-2002, 03:43 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Jul 2002
Posts: 3
|
HTML form preview then INSERT using PHP & MySQL
Hello everyone! This is regarding HTML forms, PHP & MySQL.
My Question:
How do you create an html form that goes to a preview screen and then allows the user to move back to the form if they made any errors and corrections are needed or click the send button to submit the form. The form's contents are then inserted into a database table.
|
|
|
07-06-2002, 05:03 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
Quite simple:
Code:
<?php
if(!isset($preview) || (isset($preview) && $preview == 1))
{
if(isset($preview))
{
echo "your current issue is:<br>
some setup of how the $whatever_fieldnames should be shown";
}
else
$preview =0;
echo "
<form action='$PHP_SELF' method=post>
<input type=hidden value='$preview+1' name='preview'>
<input type='text' name='some_field' value='$some_field'>
<!-- and so on with the rest of the fields used -->
<input type=submit></form>
";
}
else
{
/* $preview must be > 1
thus its a second submission.
*/
$DB=mysql_connect("localhost", "user", "passwd");
mysql_select_db("some_db");
mysql_query("INSERT INTO whatever VALUES (some_field='$some_field' ...)", $DB);
header("Location: /what/ever/page.php");
}
?>
Only one page for everything, only drawback for this one, is, theres only one preview, uppon seccond submit you'll have the entry parsed onto your sql database. But then again you're forcing the user to have that one preview.
Or did I missunderstood your question? In this, the user will uppon submit, be shown a preview, he can chose to change something in it, or leave as it is, and just press submit again, which will store the info in your database. It needs some error checking, but for a fast one, it covers the aspects.
Last edited by redhead; 07-07-2002 at 01:34 AM.
|
|
|
07-06-2002, 05:25 PM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
Thread is now moved to "Web development" where it is more suited.
|
|
|
07-06-2002, 05:47 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Jul 2002
Posts: 3
|
redhead,
Forgive me but i'm just learning PHP, MySQL, SQL. This is all new to me. I do know HTML though  (big deal).
The actions I want the user to take are as follows:
1) Fill out form.
2) Press Preview Button (the only button on that page).
3) Look over preview of text entered. (this should be on a new page and appear as text, not text in form fields.)
4a) If user finds mistake in text of Preview, click Edit button (or link or Browser back button) to go back to form and make changes. Then user repeats steps 1 (editing not re-entering data) through 4.
4b) If user does not find mistakes in text of Preview, clicks submit button that INSERTS form data into MySQL table.
5) User gets a new page thats says "thank you. form submitted."
Does the script/s you are working on function like this?
If not does anyone know how I can get this to work?
I already know how to INSERT form data into a table and ECHO the data onto a page after the data was inserted, but I can't figure out how to add the Preview in the middle before inserting the data.
Thanks!
|
|
|
07-07-2002, 01:33 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
Quote:
Originally posted by SteveSoler
The actions I want the user to take are as follows:
1) Fill out form.
2) Press Preview Button (the only button on that page).
3) Look over preview of text entered. (this should be on a new page and appear as text, not text in form fields.)
4a) If user finds mistake in text of Preview, click Edit button (or link or Browser back button) to go back to form and make changes. Then user repeats steps 1 (editing not re-entering data) through 4.
4b) If user does not find mistakes in text of Preview, clicks submit button that INSERTS form data into MySQL table.
5) User gets a new page thats says "thank you. form submitted."
Does the script/s you are working on function like this?
|
The page code I displayed will work in a similar way, the first page the user will have displayed, (At this time no info has been entered into the form fields) is a page, where theres only the form itself, with a single 'submit' button. (you could make a small check, that would display it as 'preview' and on submit change it to 'submit')
Uppon clicking the submit button, after fillining the form, the user will see a page, where the top part will be a sample of how the info in the form will be displayed. (The echo "your current issue... part will display that)
The second half of the page, will be the form once again, this time with the info the user submitted allready entered into the fields.
Uppon editing (or what ever the user feel is needed) he presses submit once again, and this time the values of the input fields, will be stored into the database, with no chance of previewing.
That was just the way my tired head thought this up. It's easy because you use the same segment of code for the form, no matter if its the first time viewed, with no data entered into any field, or if it's the second time, with the preview displayed.
In your requirements, you'll need two submit buttons, each sending different infos, where the first time viewed, only one will be displayed.
But to follow your requirements, here's this days shot at it (mind that this next code segment hasn't been tested in any way, its directly written into the reply form, just like yesterdays code, it comes with no warentie)
Code:
<?php
if(!isset($view) || isset($edit) || $view == 1)
{
/* this condition should cover the following:
1) First view here, no values in fields yet
2) Preview time, $view should be 1 now.
3) Edit time, $view is 2, but who cares lets edit
*/
if(isset($preview))
{
/* we only want this part displayed if it
truely is a 'preview' press that directed
us here
*/
echo "your current issue is:<br>
some setup of how the $whatever_fieldnames should be shown";
$type = "hidden";
}
else
{
/* only solution to end here, at this point,
is if 'edit' has been pressed, or its a
first view, so it's safest to set $view to 0
*/
$type = "text";
$view = 0;
}
echo "
<form action='$PHP_SELF' method=post>
<input type='hidden' value='$view+1' name='view'>
<input type='$type' name='some_field' value='$some_field'>
<!-- and so on with the rest of the fields used -->
";
if($view == 0)
{
/* first time here, or we are editing it.
only preview available
*/
echo "<input type='submit' name='preview' value='preview'>";
}
else
{
/* second time, alot of options */
echo "<input type='submit' name='edit' value='edit'>
<input type='submit' name='submit' value='submit'>";
}
echo "
</form>";
}
else
{
/* The 'submit' must've been pushed
insted of edit or preview, so we want it
into the database for good
*/
$DB=mysql_connect("localhost", "user", "passwd");
mysql_select_db("some_db");
mysql_query("INSERT INTO whatever VALUES (some_field='$some_field' ...)", $DB);
header("Location: /what/ever/page.php");
}
?>
As you can se, the $type can only be set to 'text', so if you have more than that type of input field, create a $type_field for each of those.
The page functionality is now as this:
1) first time here, only a form, with input fields and a single preview button
2) preview is pressed, and you see a preview of how its gonna be, a 'edit' and a 'submit' button displayed.
3a) pressing 'edit' results in step 1, with the fields holding the values allready in use.
3b) pressing 'submit' results in submitting data to database, and redirecting to /what/ever/page.php.
Was that how it was intended ?
I'm beginning to lose objectivity in this small input field.. But I think every possible outcome has been covered.
|
|
|
07-07-2002, 09:54 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Jul 2002
Posts: 3
|
redhead,
That sounds like a plan. I'll give it a try. If it does not work, I'll play around with it till it does or if I'm desperate enough, I'll submit the code for review.
Thanks for all your help! :rock:
|
|
|
08-18-2008, 01:20 PM
|
#7 (permalink)
|
|
Recruit
Join Date: Aug 2008
Posts: 1
|
redhead,
I tried your code above. But it's gonna stay on the page with the preview button. When I push the preview button, nothing happend. I have changed the field_name, table_name, database connection and location page only. Could you please help me?
Thanks in advance.
|
|
|
08-19-2008, 08:26 AM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
It's been a while since I made this, and since then PHP-4/5.whatever has come out, where most likely register_globals is turned off by default, the code portion here requires it to be turned on, altho if you were to change it to something like this, it should work.
PHP Code:
<?php
isset($_POST['view']) ? $view = $_POST['view'] : $view = 0;
$type = "text";
if(!isset($_POST['view']) || isset($_POST['edit']) || $_POST['view'] == 1)
{
/* this condition should cover the following:
1) First view here, no values in fields yet
2) Preview time, $view should be 1 now.
3) Edit time, $view is 2, but who cares lets edit
*/
if(isset($_POST['preview']))
{
/* we only want this part displayed if it
truely is a 'preview' press that directed
us here
*/
echo "your current issue is:<br>
some setup of how the $_POST['some_field'] should be shown";
$type = "hidden";
}
else
{
/* only solution to end here, at this point,
is if 'edit' has been pressed, or its a
first view, so it's safest to set $view to 0
*/
$view = 0;
}
echo "
<form action='$PHP_SELF' method='post'>
<input type='hidden' value='$view+1' name='view'>
<input type='$type' name='some_field' value=\"$_POST['some_field']\">
<!-- and so on with the rest of the fields used -->
";
if($view == 0)
{
/* first time here, or we are editing it.
only preview available
*/
echo "<input type='submit' name='preview' value='preview'>";
}
else
{
/* second time, alot of options */
echo "<input type='submit' name='edit' value='edit'>
<input type='submit' name='submit' value='submit'>";
}
echo "
</form>";
}
else
{
/* The 'submit' must've been pushed
insted of edit or preview, so we want it
into the database for good
*/
$DB=mysql_connect("localhost", "user", "passwd");
mysql_select_db("some_db");
mysql_query("INSERT INTO whatever VALUES (some_field=\"$_POST['some_field']\" ...)", $DB);
header("Location: /what/ever/page.php");
}
?>
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:04 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|