Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display data for review before inserting

Posted on 2006-11-20
4
Medium Priority
?
218 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

876 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