Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display data for review before inserting

Posted on 2006-11-20
4
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 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