Solved

Display data for review before inserting

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Query return divisible by 3 3 27
How to delete values from an array using php 4 24
ajax to record click 3 16
Add a loading gif while php runs server side 15 37
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

830 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