Solved

Display data for review before inserting

Posted on 2006-11-20
4
208 Views
Last Modified: 2011-09-20
I am very new to PHP and am getting lots of help from you experts.

I have a form in html on a website and when submitted, it inserts into a MySQL database. What I want is when the user fills out the form and clicks on submit, it displays the data for them to review. If they have no changes, they would click on another submit button and actually submit to the database. If they have changes, they would go back and make their changes.

I can do this in ASP, but have no clue in PHP. Can you help me?

0
Comment
Question by:katlees
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 17980170
Hi,

You could display the data in the prefered format for the user to review. Then you add a form consisting of hidden input fields and only a visible submit button. Possibly add a hidden input field <input type="hidden" name="reviewed" value="yes" />

Then have php look for $_POST['reviewed'] == 'yes' to run an insert query, or when not present display the data for reviewing:

<?php

if(isset($_POST['reviewed']) && $_POST['reviewed'] == 'yes') {
  # Insert record
  $sql = sprintf('INSERT INTO myTable (field1, field2, field3, dateField) values("%s","%s","%s", now())'
                       ,mysql_real_escape_string($_POST['field1'])
                       ,mysql_real_escape_string($_POST['field2'])
                       ,mysql_real_escape_string($_POST['field3'])
                      );
  mysql_query($sql) or print('<p>Your record could not be inserted.</p>');
} else {
  # Display for reviewing
  ?>
  <h1><?php echo htmlspecialchars($_POST['field1']);?></h1>
  <h2><?php echo htmlspecialchars($_POST['field2']);?></h2>
  <p><?php echo htmlspecialchars($_POST['field3']);?></p>
  <form method="post" action="test.php">
    <input type="hidden" name="reviewed" value="yes" />
    <input type="hidden" name="field1" value="<?php echo htmlspecialchars($_POST['field1']);?>" />
    <input type="hidden" name="field2" value="<?php echo htmlspecialchars($_POST['field2']);?>" />
    <input type="hidden" name="field3" value="<?php echo htmlspecialchars($_POST['field3']);?>" />
    <input type="submit" value="Save to database" />
  </form>
  <?php
}
?>

Alternatively to hidden input fields you can also use <textarea style="display:none;"><?php echo htmlspecialchars($_POST['field1']);?></textarea>

-r-
0
 

Author Comment

by:katlees
ID: 17980194
Thank you. I appreciate the help.
0
 

Author Comment

by:katlees
ID: 17990533
Roonaan... you there? i can't get this code to work when I change the fields to my fields.

This is my form page.

<form action="review.php" method="post">
  <p><font color="#333333" size="2" face="Arial, Helvetica, sans-serif">Date:
    <input type="text" name="currentdate" value="" style="border:0;">
    <br>
    </font>First Name:
    <input type="text" name="first">
    <br>
    Last Name:
    <input type="text" name="last">
    <br>
    Phone:
    <input type="text" name="phone">
    <br>
    Mobile:
    <input type="text" name="mobile">
    <br>
    Address:
    <input type="text" name="address">
    <br>
    City:
    <input type="text" name="city">
    <br>
    State :
    <input type="text" name="state">
    <br>
    Zip:
    <input type="text" name="zip">
    <br>
    E-mail:
    <input type="text" name="email">
    <br>
    Web:
    <input type="text" name="web">
    <br>
    <input type="Submit">
  </p>
  </form>
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17993986
Hi,

review.php could be something like this:

[review.php]
<?php

if(isset($_POST['reviewed']) && $_POST['reviewed'] == 'yes') {
  # Insert record
  $sql = sprintf('INSERT INTO myTable (first,last,phone.mobile,address,city,state,zip,email,web,date) values("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s", now())'
                       ,mysql_real_escape_string($_POST['first'])
                       ,mysql_real_escape_string($_POST['last'])
                       ,mysql_real_escape_string($_POST['phone'])
                       ,mysql_real_escape_string($_POST['mobile'])
                       ,mysql_real_escape_string($_POST['address'])
                       ,mysql_real_escape_string($_POST['city'])
                       ,mysql_real_escape_string($_POST['state'])
                       ,mysql_real_escape_string($_POST['zip'])
                       ,mysql_real_escape_string($_POST['email'])
                       ,mysql_real_escape_string($_POST['web'])
                      );

  mysql_query($sql) or print('<p>Your record could not be inserted: '.mysql_error().'</p>');
  echo '<p>Record inserted succesfuly</p>';
} else {
  # Display for reviewing
  ?>
  F: <?php echo htmlspecialchars($_POST['first']);?><br/>
  L: <?php echo htmlspecialchars($_POST['last']);?><br/>
  P: <?php echo htmlspecialchars($_POST['phone']);?><br/>
  M: <?php echo htmlspecialchars($_POST['mobile']);?><br/>
  A: <?php echo htmlspecialchars($_POST['address']);?><br/>
  C: <?php echo htmlspecialchars($_POST['city']);?><br/>
  S: <?php echo htmlspecialchars($_POST['state']);?><br/>
  Z: <?php echo htmlspecialchars($_POST['zip']);?><br/>
  E: <?php echo htmlspecialchars($_POST['email']);?><br/>
  W: <?php echo htmlspecialchars($_POST['web']);?><br/>
  <form method="post" action="review.php">
    <input type="hidden" name="reviewed" value="yes" />
    <input type="hidden" name="first" value="<?php echo htmlspecialchars($_POST['first']);?>" />
    <input type="hidden" name="last" value="<?php echo htmlspecialchars($_POST['last']);?>" />
    <input type="hidden" name="phone" value="<?php echo htmlspecialchars($_POST['phone']);?>" />
    <input type="hidden" name="mobile" value="<?php echo htmlspecialchars($_POST['mobile']);?>" />
    <input type="hidden" name="address" value="<?php echo htmlspecialchars($_POST['address']);?>" />
    <input type="hidden" name="city" value="<?php echo htmlspecialchars($_POST['city']);?>" />
    <input type="hidden" name="state" value="<?php echo htmlspecialchars($_POST['state']);?>" />
    <input type="hidden" name="zip" value="<?php echo htmlspecialchars($_POST['zip']);?>" />
    <input type="hidden" name="email" value="<?php echo htmlspecialchars($_POST['email']);?>" />
    <input type="hidden" name="web" value="<?php echo htmlspecialchars($_POST['web']);?>" />
    <input type="submit" value="Save to database" />
  </form>
  <?php
}
?>

-r-
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now