Solved

Display data for review before inserting

Posted on 2006-11-20
4
209 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

937 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

4 Experts available now in Live!

Get 1:1 Help Now