Keep saved data in form after revisiting the page - php

I've created a form with three input fields. I managed to find out how to keep the data after saving the form, but when I leave the page and come back to this form, the fields are empty again.
Here is my code:
<?php
if(isset($_POST['save_home_details'])) {
$home_title = escape_string($_POST['home_title']);
$home_desc = escape_string($_POST['home_desc']);
$home_keywords = escape_string($_POST['home_keywords']);
$query = "INSERT INTO settings(home_title, home_desc, home_keywords) ";
$query .= "VALUES('{$home_title}', '{$home_desc}', '{$home_keywords}') ";
$home_details_query = mysqli_query($conn, $query);
confirm($home_details_query);
echo "<div class='alert alert-success'>Settings saved successfully!</div>";
}
?>
<form action="" method="post">
<div class="form-group">
<label for="home_title">Home title</label>
<input type="text" class="form-control" name="home_title" value="<?php if(isset($_POST['home_title'])) { echo htmlentities ($_POST['home_title']); }?>">
</div>
<div class="form-group">
<label for="home_title">Home description</label>
<input type="text" class="form-control" name="home_desc" value="<?php if(isset($_POST['home_desc'])) { echo htmlentities ($_POST['home_desc']); }?>">
</div>
<div class="form-group">
<label for="home_title">Home keywords</label>
<input type="text" class="form-control" name="home_keywords" value="<?php if(isset($_POST['home_keywords'])) { echo htmlentities ($_POST['home_keywords']); }?>">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="save_home_details" value="Save settings">
</div>
</form>
Basically what I want here is:
to be able to add value to these fields
save the values
keep the values in the fields whenever I come back to this page
if I click the save settings button, update the values and not add a new record to the database.
I know I haven't added the update query yet, but can I do that on the same page where I insert the values into the database?
Thanks

If you need to store temporarily you can use $_SESSION['anyData'] = $anyData.
When you do not need you can unset($_SESSION['anyData'])

Related

Why I can't update the record in codeigniter?

I am getting this error,
A PHP Error was encountered
Severity: Notice
Message: Trying to get property 'post_title' of non-object.
I cannot get the form inputs to print on the page and cannot update. Please help!
So, I have tried to update each record and only artist users are able to do that. But I cannot figure out how to show the individual post on the form page. The records saves correctly and deletes but cannot update them.
Controller
function editpost($post_id)//Edit post page
{
if (!$this->session->Role =='member,artist')
{
redirect(base_url() . 'login');
}
$data['success'] = 0;
if( !empty($this->input->post('post_title')) ) {
$data['post_title'] = $this->input->post('post_title');
$data['post'] = $this->input->post('post');
$data['active'] = 1;
/* >> file attach */
View
<form action="<?= base_url()?>starter/editpost/" method="post" class="justify-
content-center" enctype='multipart/form-data'>
<div class="form-group">
<label class="sr-only">Title</label>
<input type="text" class="form-control" placeholder="Title"
name="post_title" value="<?php echo $post->post_title; ?>">
</div>
<div class="form-group">
<label class="sr-only">Description</label>
<textarea class="form-control" placeholder="Describe it" name="post" ><?
php echo $post->post; ?></textarea>
</div>
<div class="row py-4">
<div class="col-lg-6 mx-auto">
<!-- Upload image input-->
<!-- File Button -->
<div class="col-md-5">
<input name="main_img[]" type="file" accept="image/x-png,image/gif,image/jpeg">
</div>
<br>
<br>
<p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="update"
value="Publish" /></p>
</form>
use direct the variable because you are passing just values which you are posting from form.
//like :
//for post_title
<input type="text" class="form-control" placeholder="Title"
name="post_title" value="<?php echo $post_title; ?>">
// for description
textarea class="form-control" placeholder="Describe it" name="post" ><?
php echo $post; ?></textarea>
You don't need to pass the post data in controller you can get post data anywhere
<input type="text" class="form-control" placeholder="Title" name="post_title" value="<?php echo ($this->input->post('post_title')!='')?$this->input->post('post_title'):''; ?>">

stop duplicate entry after reload browser

I have a form and trying to insert data in table with class. Data is inserting in proper way.
But when click on browser reload after insert query, then it inserts duplicate entry in table.
I tried it with redirect function. But not working.
My form is:
<?php
if(isset($_POST['insertcourse']))
{
$userprofileobj->insert_course($_POST['c_name'],$_POST['c_description']);
}
?>
<form action="" method="POST">
<div class="control-group">
<label class="control-label">Course Name :</label>
<div class="controls">
<input type="text" class="span11" placeholder="First name" name="c_name" />
</div>
</div>
<div class="controls">
<label class="control-label">Course Description :</label>
<textarea class="textarea_editor span12" rows="6" placeholder="Enter text ..." name="c_description"></textarea>
</div>
<div class="form-actions">
<input type="submit" value="SAVE" name="insertcourse" class="btn btn-success">
</div>
</form>
My classes.php is:
<?php
class operations{
public $user_name;
public $error;
public $con;
public function __construct()
{
$this->con = new mysqli("localhost","root","","admin_with_oops");
}
public function insert_course($c_name,$c_description)
{
$date1 = date("Y-m-d");
$result = $this->con->query("insert into courses (c_name,c_description,date1,status1) VALUES ('$c_name', '$c_description', '$date1','1')");
if($result)
{
$alertmsgs = "Inserted Successfully";
$this->alertmsg($alertmsgs);
}
}
public function alertmsg($alertmsgs)
{
echo"<div class='alert alert-error'>
<button class='close' data-dismiss='alert'>×</button>
<strong></strong> $alertmsgs </div>
</div>";
}
}
You could generate a guid and put it in a hidden input in your form. Database that ID on the initial submit and don't allow it to be inserted a second time.
<input type="hidden" value="<?php com_create_guid() ?>">
It's a little more foolproof than post-redirect-get and will be a less jarring user experience, especially over high latency networks.

Thank you message after Complete a Form

I have a Bootstrap form and after complete the form I want remain in the same page and display a thank you message just below the submit button.
Here my HTML code
<div id="form">
<div class="row">
<div class="col-md-12"><h3>RESTA IN CONTATTO</h3>
<form id="form_members" role="form" data-toggle="validator" novalidate action="form-data.php" method="POST">
<div class="form-group">
<label for="firstname" class="control-label">Nome</label>
<input type="text" class="form-control" name="firstname" id="name" placeholder="Inserisci il Nome" required>
</div>
<div class="form-group">
<label for="lastname" class="control-label">Cognome</label>
<input type="text" class="form-control" name="lastname" id="lastname" placeholder="Inserisci il Cognome" required>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter the Email" data-error="Inserire email valida" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="terms" required data-error="Devi essere d'accordo con i termini di condizione d'uso">Privacy
</label>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="submit" id="submit" onclick="this.form.clear()" value="submitmessage">Registrati</button>
</div>
</form>
<div id="submitmessage"></div>
and Here my php Code
<?php
$link = mysqli_connect("","","") or die("failed to connect to server !!");
mysqli_select_db($link,"");
if(isset($_POST['submit']))
{
$errorMessage = "";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
// Validation will be added here
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}
else{
//Inserting record in table using INSERT query
$insqDbtb="INSERT INTO `test`.`members`
(`firstname`, `lastname`, `email`) VALUES ('$firstname', '$lastname', '$email')";
mysqli_query($link,$insqDbtb) or die(mysqli_error($link));
}
}
?>
First check if your insert actually happen by assigning result to a variable, ie:
$res=mysqli_query($link,$insqDbtb);
Now, below your form you can add the following php code and you can use this variable in an if statement to echo different things:
if($_POST && $res)
{/*this code is executed if form is submitted and insert worked*/
echo ' <div class="alert alert-success" role="alert">
Grazie per esserti registrato!
</div>';
}
elseif ($_POST && !$res)
{ /*you can write a different message if insert did not happen*/
echo '<div class="alert alert-danger" role="alert">
<strong>Oh no!</strong> Provaci ancora
</div>';
}
Note that this might only work if you use php to process the form and scripts are on the same page so you would also need to modify your form like this:
<form action="register.php" method="post" accept-charset="utf-8">
(where register.php stands in for the name of your file)
and the submit button like this:
<input type="submit" name="submit" value="Submit" id="submit_button" class="btn btn-primary" />
Then at the top of your page, after you have set the parameters for the connection you put the following statement:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
and you can put all the form validation here. What happens is the first time you go to the file it will be through a GET so nothing happens, after you submit the form you access the same file but with a POST so the submission is validated.
To submit the form and stay on the same page you have two possibilites:
A) As other people have suggested you use Ajax
B) The other possibility which has the same visible result for the user is that when you submit your form you go back to the same page, so the user will see the same page but different things will be displayed depending on weather he has already submitted the form or not
To achieve this second solution you can do the following:
1)In your head you establish the connection (the php you have is improvable but as a first attempt should do the job)
2) In your page you put your form with the following modification:
<form action="FILNAME.php" method="post" accept-charset="utf-8">
and
<input type="submit" name="submit" value="Submit" id="submit" class="btn btn-default" />
THis way when a user clicks the button the form is submitted to the same page.
3) Now you need to check if your page is beening accessed for the first time (ie thoruh a GET) or after the form has been submitted, so where you want your message to appear you put the following if statement:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
3) Within this bracket you put all your php to validate the form and after validation your insert command
$insqDbtb="INSERT INTO `test`.`members`(`firstname`, `lastname`, `email`) VALUES ('$firstname', '$lastname', '$email')";
$res = mysqli_query($link, $insqDbtb);
4) If the query created a new row you can write the thank you message:
if (mysqli_affected_rows($link) === 1) {echo '<div class="alert alert-success"><h3>Thanks!</h3><p>Thank you for registering! </p></div>';
If not you can write an alert instead
I learned this method from a book by Larry Ullman, you can find the scripts with examples for free
here

PHP button in a for each loop

I am displaying user details on a form using a for each loop. In the for each loop i am adding a button below the user details to delete the information for each user.
<?php
foreach( $Names as $Name )
{
$dbQuery = $db->prepare("select * from user WHERE Name = ?");
$dbQuery->execute(array($Name));
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC))
{
$ID = $dbRow['ID'];
$Email = $dbRow ['Email'];
}
?>
<div class="form-group">
<label for="hotel_id" class=" col-sm-4 control-label">ID:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="ID" name="ID[]" placeholder="Enter City Name" value="<?php echo $ID?>">
</div>
</div>
<div class="form-group">
<label for="Name" class="col-sm-4 control-label">Name:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Name" name="Name[]" value="<?php echo $Name?>">
</div>
</div>
<div class="form-group">
<label for="Email" class="col-sm-4 control-label">Email:</label>
<div class="col-sm-8 input">
<input type="text" class="form-control" id="Email" name="Email[]" value="<?php echo $Email ?>">
</div>
</div>
<div class ="form-group">
<label for="removeUser" class="col-sm-4 control-label"></label>
<div class="col-sm-8">
<input id="removeUser" name="removeUser" type="submit" value="Remove this user" class="btn btn-danger" onclick="return confirm('Are you sure you want to completely remove this user?');">
</div>
</div>
<?php
}
?>
I know how to delete the users but the problem i am having is that i cant seem to get the id for the specific user when the button is clicked. So far i have only been able to retrieve the last id displayed or all of the id's displayed.
Any help would be appreciated. Thanks.
First, you'll need to move the closing loop brace to the end of the html of each row. Then I recomend you to do a separated URL for deleting a record, and changing the submit button to a anchor tag (by CSS rules you can make it to look like a button), so the anchor will be:
Delete
So in the backend of delete.php you'll have something like:
if(isset($_GET['id'])){
$dbQuery = $db->prepare(sprintf("DELETE FROM user WHERE ID = %s", $_GET['id']));
$dbQuery->execute(array($Name));
}
You'll need to improve this delete query so it will be safe, because people might send you SQL code trough $_GET['id']
Your while loop is wrong, because last one is fetched and then html is rendered. Try add that html under while loop
Please add ID in hidden filed in a form. So you can get id each form submit like normal form field value. Add ID to hidden field value.

inserting data into mysql database using php

I have a php order form named (order.php) and when the user clicks the (submit button "Next Step") it takes him to another page called (confirm-order.php)
The (confirm-order.php) shows the information that the user submitted from the (order.php) using the $_POST[] and by assigning each one of these to a variable.
Data showing on the (confirm-order.php) plain text like for example :
$itemName = $_POST['itemName'];
<?php echo $itemName; ?>
at the end of page there is a form contains only one element as (submit button)
How can i insert the $itemName data into mysql database only (after the submit button is clicked and the form actions take me to the confirmation page)?
I know how to insert data into mysql, but it didn't work with the isset() function
Do i have to write the isset function inside the form first? and below it the mysql database code?
order.php page:
<form class="form-horizontal well" action="confirm-order.php" method="POST">
<fieldset>
<legend>Personal Shopper Order Form</legend>
<div class="control-group">
<label class="control-label" for="select01">Choose a plan</label>
<div class="controls">
<select id="select01" name="plan">
<option>Lite Plan $0 per order</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="itemName">Item Name</label>
<div class="controls">
<input type="text" class="input-xlarge" id="itemName" name="itemName">
<p class="help-block">Item name exapmle: iPad3 White 32GB wifi & 3G.</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="itemID">Item ID</label>
<div class="controls">
<input type="text" class="input-xlarge" id="itemID" name="itemID">
<p class="help-block">example: Ebay Item ID, Amazon Item ID.</p>
</div><br>
<div class="control-group">
<label class="control-label" for="itemURL">Item URL</label>
<div class="controls">
<input type="text" class="input-xxlarge" id="itemURL" name="itemURL">
<p class="help-block">Direct web link to the item.</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="textarea">Item Details</label>
<div class="controls">
<textarea class="input-xlarge" id="textarea" name="itemDetails" rows="6"></textarea>
<p class="help-block">Item details (name, color, specifications etc...)</p>
</div>
</div>
<li id="li_3" data-pricefield="money_simple" data-pricevalue="0">
<div class="input-prepend input-append">
<label class="control-label" for="element_3_1">Item Price</label>
<div class="controls">
<span class="add-on">$</span>
<input id="element_3_1" data-price-value="10.00" name="element_3" type="text" class="element text large">
<p class="help-block">Item exact price on the US online store.</p>
</div>
</div>
</li>
<li id="li_7" data-pricefield="money_simple" data-pricevalue="0">
<div class="input-prepend input-append">
<label class="control-label" for="element_7_1">Local Shipping Cost</label>
<div class="controls">
<span class="add-on">$</span>
<input id="element_7_1" data-price-value="10.00" name="element_7" type="text" class="element text large">
</div>
<p class="help-block">Local shipping fee from the US Store to Sky2ship (if applicable).</p>
</div>
</li>
<li id="li_8" data-pricefield="radio" data-pricevalue="0">
<div class="control-group">
<div class="controls">
<p class="help-block">Order Processing Service Fee.</p>
<label class="radio">($0) Standard 2-3 days
<input id="element_8_1" data-pricedef="00.00" name="element_8" class="element radio" type="radio" value="$0 Standard 2-3 Day">
</label>
<label class="radio">($10) Express 1 day
<input id="element_8_2" data-pricedef="10.00" name="element_8" class="element radio" type="radio" value="$10 Express Same Day">
</label>
</div>
</div>
</li>
<legend>Personal Information & Shipping Address</legend>
<div class="control-group">
<label class="control-label" for="input04">Full Name</label>
<div class="controls">
<input type="text" class="input-medium" id="fullName" name="fullName">
<p class="help-block">First & last name.</p>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="radio">Male
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
</label>
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">Female
</label>
</div>
</div>
<div class="input-prepend">
<label class="control-label" for="prependedInput">Email Address</label>
<div class="controls">
<span class="add-on">#</span>
<input type="text" class="span2" id="prependedInput" name="Email">
<p class="help-block">Your email address.</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="input06">Address</label>
<div class="controls">
<input type="text" class="input-xxlarge" id="input06" name="streetAddress" placeholder="Street Address">
<p class="help-block">Your shipping address.</p>
</div>
</div>
<div class="control-group">
<div class="controls controls-row">
<input type="text" class="span2" id="City" name="City" placeholder="City">
<input type="text" class="span3" id="State" name="State" placeholder="State / Province">
</div>
</div>
<div class="control-group">
<div class="controls controls-row">
<input type="text" class="span2" id="PostalCode" name="PostalCode" placeholder="Postal Code">
<input type="text" class="span3" id="Phone" name="Phone" placeholder="Phone Number">
</div>
</div>
<div class="control-group">
<label class="control-label" for="select01">Country</label>
<div class="controls">
<select id="select02" name="Country">
<option>IRAQ</option>
<option>JORDON</option>
</select>
</div>
</div>
<li class="total_payment" align="right" data-basetotal="0">
<span>
<h3 class="alert-success">$<var>0</var></h3>
<h5>Total</h5>
</span>
</li>
<div class="control-group">
<label class="control-label" for="optionsCheckbox">Read & Agree</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" id="optionsCheckbox" value="option1">
I agree to the site's Terms of Service & Privacy Policy.
</label>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Confirm Order</button>
<button type="reset" class="btn">Cancel Order</button>
</div>
</fieldset>
</form>
confirm-order.php page:
<?php
$itemName = $_POST['itemName'];
$plan = $_POST['plan'];
$itemID = $_POST['itemID'];
$itemPrice = $_POST['element_3'];
$processService = $_POST['element_8'];
$itemDetails = $_POST['itemDetails'];
$streetAddress = $_POST['streetAddress'];
$City = $_POST['City'];
$State = $_POST['State'];
$PostalCode = $_POST['PostalCode'];
$Phone = $_POST['Phone'];
$Country = $_POST['Country'];
$fullName = $_POST['fullName'];
$Email = $_POST['Email'];
$itemURL = $_POST['itemURL'];
$itemLocalShipCost = $_POST['element_7'];
?>
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "000000";
$db_name = "dbname";
if (isset($_POST['submit'])) {
$db_connect = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql ="INSERT INTO lite_order (lite_plan, lite_item_name)
VALUES
('$plan','$item')";
if (!mysqli_query($db_connect,$sql))
{
die('Error: ' . mysqli_error($db_connect));
}
echo "1 record added";
}
?>
<address>
<strong>Shipping Address.</strong><br>
<?php echo $streetAddress; ?><br>
<?php echo $City; ?>, <?php echo $State; ?>, <?php echo $PostalCode; ?><br><?php echo $Country; ?><br>
<abbr title="Phone">P:</abbr><?php echo $Phone; ?>
</address>
<address>
<strong><?php echo $fullName; ?></strong><br>
<?php echo $Email; ?>
</address>
<table class="table">
<thead>
<tr>
<th>Plan</th>
<th>Item Name</th>
<th>Item ID</th>
<th>Local Shipping Cost</th>
<th>Item Price</th>
<th>Order Processing Fee</th>
</tr>
</thead>
<tbody>
<tr class="success">
<td><?php echo $plan; ?></td>
<td><?php echo $itemName; ?></td>
<td><?php echo $itemID; ?></td>
<td><?php echo "$" . $itemLocalShipCost; ?></td>
<td><?php echo "$" . $itemPrice; ?></td>
<td><?php echo $processService; ?></td
></tr>
</tbody>
</table>
<strong>Item URL</strong><p class="alert alert-info"><?php echo $itemURL; ?></p>
<pre class="pre-scrollable"><?php echo $itemDetails; ?></pre>
<p>Your Total <h3 class="question"><?php echo "$" . $orderTotal; ?></h3></p>
<div class="form-actions"><form action="pending-order.php" method="post" name="confirmed-order">
<button type="submit" name="submit" class="btn btn-primary">Submit Order</button>
<button type="button" class="btn">Previous</button></form>
</div>
</div>
</div>
</div>
</div>
</div>
Whereto insert the mysql database code to insert all the variables into database after the submit button is clicked? where to place the isset() function? i tried it, it didn't insert any data into my table.
EDIT: a simple example
do <form>, validation and inserting in one file, say form.php:
<? // check if FORM has been posted
$posted = isset($_POST['submit']);
if ($posted) { // form has been posted...
// validate input
if (!isset($_POST['item']) || strlen(trim($_POST['item'])) == 0)
$error['item'] = "please insert an item-name!";
if (!isset($_POST['price']) || !is_numeric($_POST['price']))
$error['price'] = "please enter a valid price!";
// ready for input?
if (!isset($error)) { // no $error --> go insert!
// I'll do the db-operation with PDO and a prepared statement.
// this is cool, easy and safe. LEARN IT!
$sql = "INSERT INTO table (item,price) VALUES (:item,:price)";
$insert = $db->prepare($sql);
$insert->execute(array(
':item' => $_POST['item'],
':price' => $_POST['price']
));
} // $error
} // submit
?>
Now, in the <body> of the same page...
<? // check whether to display confirmation or form...
if ($posted && !isset($error)) {
// form was sent AND no error --> confirm
?>
<h1>Confirmed!</h1>
<p>Your data has been sent, thank you very much!</p>
go to somepage
<?
} else {
// form not sent or errors --> display form
?>
<h1>Please enter data</h1>
<? // display error-message, if there's one:
if (isset($error)) {
$output = "";
foreach ($error as $field => $msg)
$output .= (strlen($output) > 0?', ':'') . "[$field]: $msg";
echo "<p>There were errors: $output</p>";
} // $error
?>
<form method="post">
<!-- if the form has been sent, bring back the field's value from $_POST -->
<p>item-name: <input type="text" name="item"
value="<?=($posted?$_POST['item']:'')?>" /></p>
<p>price: <input type="text" name="price"
value="<?=($posted?$_POST['price']:'')?>" /></p>
<p><input type="submit" name="submit" value="submit" /></p>
</form>
<?
} // submit & $error
?>
See the use of a ternary-operator for setting the value-attribute of the <input>-elements:
(<condition>?<what to do if true>:<what to do if false>)
There are two specific things I can contribute.
First, isset tests for null... which is different than empty. If you have a form field that is submitted empty, then set a local variable to that posted value, then test it with isset; isset will return true because the value exists which is different than the variable not having been registered in the page load at all.
Second... ANYTHING can post to your form (think evil autonomous Korean hacker bots). Also, there are many ways a form can get submitted without having activated the submit button itself so there is no guarantee you will even see a submit key in your $_POST vars. What you need to define in your processing script is a "default action". What I mean by that is a very basic and SAFE behavior (like redirecting to a 'something is wrong' page) that kicks off by default such that the only way around it is to submit a correct form with all anticipated values correctly set.
If you do this, you can ignore the value of the submit button itself and instead focus on the contents of the POST. Did I receive everything I expected to receive? Was it all in the correct format? Was the user authenticated correctly? Only after all these questions have been tested to your satisfaction would you switch from the default behavior to a form processing behavior in which the posted data can be inserted into your database.
Example using your 3 page structure:
reference: filter vars
Page 1:
<form action=./page2 method=POST>
<input type=text value=1234 name=numericValue />
<input type=text value="dummytext" name=stringValue />
<input type=submit value=submit name=submit />
</form>
Page 2:
<?php
$args = array('numericValue' => FILTER_VALIDATE_INT
,'stringValue' => FILTER_SANITIZE_STRING);
$clean_data = filter_input_array(INPUT_POST,$args);
if (is_array($clean_data))
{
$_SESSION["saved_clean_data"] = $clean_data;
}
else
{
Header(<something wrong page>);
die();
}
?>
<form action=./page3 method=POST>
<input type=submit name=submit value=No />
<input type=submit name=submit value=Yes />
</form>
Page 3:
<?php
if ($_POST["submit"] === "Yes")
{
$cleanNum = $_SESSION["saved_clean_data"]["numericValue"];
$cleanStr = $_SESSION["saved_clean_data"]["stringValue"];
// DB insert Query, use advice from michi about PDO
// parameterize your queries to help prevent sql injection
}
else
{
Header(<somewhere for declined submits>);
die();
}
?>
Well we can do this in the following ways
You store all the data in session and use it in confirmation page and then on data insertion page. Do remember to update or delete it if user updates or cancel the order.
You can dynamically create the confirm order page using javascript and HTML and when user clicks confirm button then only we post it to the PHP page. This will also reduce a server call.
One other ways is to again send the collected posted values and keep it as hidden fields in the confirmation page and post it when clicked confirm.
create a form and store variables in hidden fields , then create this submit button in the form
So clicking this form will store the info. See the exmple here
<form class="form-horizontal well" action="confirm-order.php" method="POST">
<input type="hidden" value="<?php echo $itemName; ?>" />
<input type="submit" value="Confirm Order" />
</form>
Well there are couple of ways about doing this:
Store all the data from the previous page i.e. from order.php in the $SESSION[] variables:
Explaination: Setting it in Session will enable you to access the same variable from anywhere in the site until the session of the user. Means that after you store it in session you can access it in pending-order.php page.
How to do it: In this page at the top, instead of setting the variables at top write the following:
$SESSION['itemName'] = $_POST['itemName']
then echo it using:
echo $SESSION['itemName']
and then in the pending-order.php you can assign a value to a variable like so:
$itemName = $SESSION['itemName']
and now you can store the variable in the db.
Put hidden fields inside the form of confirm-order.php page:
Explaination: Create hidden input fields in confirm-order.php form and set the values that are in the variables. This way when you click the submit button you can access them in pending-order.php in the same way you are doing on confirm-order.php.
How to do it: Simply put the variables in value attribute of the hidden input like so:
<form action="pending-order.php" method="post" name="confirmed-order">
<input type="hidden" value="<?php $itemID ?>" id="someID">
</form>
Try
<button type="submit" class="btn btn-primary" NAME="submit">Confirm Order</button>
And use
IF (isset($_POST['submit]) {
$itemName = $_POST['itemName'];
$plan = $_POST['plan'];
$itemID = $_POST['itemID'];
$itemPrice = $_POST['element_3'];
$processService = $_POST['element_8'];
$itemDetails = $_POST['itemDetails'];
$streetAddress = $_POST['streetAddress'];
$City = $_POST['City'];
$State = $_POST['State'];
$PostalCode = $_POST['PostalCode'];
$Phone = $_POST['Phone'];
$Country = $_POST['Country'];
$fullName = $_POST['fullName'];
$Email = $_POST['Email'];
$itemURL = $_POST['itemURL'];
$itemLocalShipCost = $_POST['element_7'];
// your mysql INSERT codes here
}
EDIT 1:
change <button type="submit" class="btn btn-primary">Confirm Order</button>
TO <input type="submit" class="btn btn-primary" value="Confirm Order">
isset() function work when the input field type is submit.like
<input type="submit" value="Confirm Order" />
so update the code form
<div class="form-actions">
<button type="submit" class="btn btn-primary">Confirm Order</button>
<button type="reset" class="btn">Cancel Order</button>
</div>
to
<div class="form-actions">
<input type="submit" class="btn btn-primary" value="Confirm Order" />
<input class="btn" type="reset" value="Cancel Order" />
</div>

Categories