Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 07-06-2002, 03:43 PM   #1 (permalink)
SteveSoler
Registered User
 
SteveSoler's Avatar
 
Join Date: Jul 2002
Posts: 3
SteveSoler is on a distinguished road
Question 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.
SteveSoler is offline   Reply With Quote
Old 07-06-2002, 05:03 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 07-07-2002 at 01:34 AM.
redhead is offline   Reply With Quote
Old 07-06-2002, 05:25 PM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
redhead is on a distinguished road
Thread is now moved to "Web development" where it is more suited.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-06-2002, 05:47 PM   #4 (permalink)
SteveSoler
Registered User
 
SteveSoler's Avatar
 
Join Date: Jul 2002
Posts: 3
SteveSoler is on a distinguished road
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!
SteveSoler is offline   Reply With Quote
Old 07-07-2002, 01:33 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-07-2002, 09:54 AM   #6 (permalink)
SteveSoler
Registered User
 
SteveSoler's Avatar
 
Join Date: Jul 2002
Posts: 3
SteveSoler is on a distinguished road
Talking

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:
SteveSoler is offline   Reply With Quote
Old 08-18-2008, 01:20 PM   #7 (permalink)
itsme85
Recruit
 
Join Date: Aug 2008
Posts: 1
itsme85 is on a distinguished road
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.
itsme85 is offline   Reply With Quote
Old 08-19-2008, 08:26 AM   #8 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
redhead is on a distinguished road
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");
  }
?>
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple PHP MySQL code: confused. easilyi Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 10-24-2004 07:53 PM
cant connect to mysql databases using php eran PHP 11 08-07-2004 08:02 AM
MYSQL - insert jimmyoctane PHP 7 09-15-2003 01:15 PM
Help with setting up mySQL and PHP Ilya020 PHP 11 03-19-2003 05:10 AM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 04:04 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting