I have 2 php scripts one that takes the values from a form on an html page and just return them to the user so he can check they are correct i then want to allow him to confirm then by pressing a submit button, the values will then be written on a txt file.
Here is my code that i have so far, the script return that the values are being written on the file but there is nothing in the file>
code:
fist php:
$surname = $_POST['surname'];
$lastname = $_POST['lastname'];
$mail = $_POST['mail'];
<div id="oneConatiner">
<h1>Please confirm the details of your order:</h1>
<div align="left" id='php'>
<form name="info" method="post" action="confirmation.php">
<p>Your surname is: <?php echo $surname . "<p>"; ?>
<p>Your lastname is: <?php echo $lastname . "</p></div>"; ?>
<p>Your email is: <?php echo $mail . "<p>"; ?>
<p><input type="submit" name="Submit" value="Confirm" /></p>
</form>
</div>
Second php:
<?php
$surname = $_POST['surname'];
$lastname = $_POST['lastname'];
$mail = $_POST['mail'];
$page = ceil($quantity / 10);
$data = "$surname,$lastname,$mail\r\n";
$fh = fopen("user.txt", "a");
fwrite($fh, $data);
fclose($fh);
if (fwrite == FALSE){
echo "fail";
} else {
echo "successful";
}
?>
The values you want to submit need to be form inputs of some kind. I'll use text fields for this example:
<form name="info" method="post" action="confirmation.php">
<p>Your surname is: <input name="surname" value="<?php echo htmlentities($surname); ?>"></p>
<p>Your lastname is: <input name="lastname" value="<?php echo htmlentities($lastname); ?>"></p>
<p>Your email is: <input name="mail" value="<?php echo htmlentities($mail); ?>"></p>
<p><input type="submit" name="Submit" value="Confirm" /></p>
</form>
If you don't want these fields to be editable, you can either set the fields to readonly or use hidden inputs instead.
The only error that I see and needs to be corrected is that the word fwrite in the if-conditional should be changed to $fh otherwise the code works. It worked for me.
Make sure the $surname, $lastname and $mail are not empty and you have read and write permissions for the folder where user.txt is supposed to be stored.
Related
I have not found an answer anywhere else.
Still having a problem adding form textbox values to the session. The code as it stands won't even add to the session from this particular form. It had before until I closed the browser to restart the session. When it was sending the session correctly to the second page the values for fname, lname, address ect was fname:|N which I am assuming in a null value? Every page the sesson_start(); is called at the top.
Code for the form:
<form method='post' action='email.php'>
First name: <input type="text" name="fname"/><br />
Last name: <input type="text" name="lname"/><br />
Address: <input type="text" name="address"/><br />
City: <input type="text" name="city"/><br />
State: <input type="text" name="state" /><br />
Zip code: <input type="text" name="zip"/><br />
Email: <input type="text" name="email"/><br />
<button type="submit" name="submit">Continue to checkout</button>
</form>
<?php
if(isset($_POST['submit'])){
$_SESSION['fname'] = $_POST['fname'];
//$_SESSION['fname'] = $var_fname;
$_SESSION['lname'] = $_POST['lname'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['city'] = $_POST['city'];
$_SESSION['state'] = $_POST['state'];
$_SESSION['zip'] = $_POST['zip'];
$_SESSION['email'] = $_POST['email'];
}
?>
Code on second page echo'ing the session:
<?php
session_start();
echo $var_session = json_encode($_SESSION["shopping_cart"]);
?>
<html>
<body>
<br /><br /><br />
<?php
echo session_encode();
//echo $var_email = json_encode($_SESSION["email"]);
//echo $var_fname = json_encode($_SESSION["fname"]);
//echo $var_email = $_SESSION["email"];
?>
</body>
</html>
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed last year.
I am trying to add data from a form on one page and send it to another. I have done this elsewhere on the first page and it adds the data. I try it again and when I echo it out on the second page I get a null value.
Session was already started at the top of the page.
Code on the first page:
<?php
if(!isset($_POST['submit'])){}
else{
$var_fname = $_SESSION['fname'] = $_POST['fname'];
$var_lname = $_SESSION['lname'] = $_POST['lname'];
$var_address = $_SESSION['address'] = $_POST['address'];
$var_city = $_SESSION['city'] = $_POST['city'];
$var_state = $_SESSION['state'] = $_POST['state'];
$var_zip = $_SESSION['zip'] = $_POST['zip'];
$var_email = $_SESSION['email'] = $_POST['email'];
};
?>
Code on the second page:
<?php
/**
* #author
* #copyright 2022
*/
session_start();
echo $var_session = json_encode($_SESSION["shopping_cart"]);
?>
<html>
<body>
<br /><br /><br />
</body>
</html>
<?php
//echo $var_email = json_encode($_SESSION["email"]);
echo $var_fname = json_encode($_SESSION["fname"]);
?>**
The second echo outputs NULL.
Form I used to gather the data:
<form method="post" action='email.php'>
First name: <input type="text" name="fname"/><br />
Last name: <input type="text" name="lname"/><br />
Address: <input type="text" name="address"/><br />
City: <input type="text" name="city"/><br />
State: <input type="text" name="state" /><br />
Zip code: <input type="text" name="zip"/><br />
Email: <input type="text" name="email"/><br />
<button type="submit">Continue to checkout</button>
</form>
--output on second page:
{"Bag01":{"name":"Laptop Bag","code":"Bag01","price":"50.00","quantity":1,"image":"product-images\/laptop-bag.jpg"}}
shopping_cart|a:1:{s:5:"Bag01";a:5:{s:4:"name";s:10:"Laptop Bag";s:4:"code";s:5:"Bag01";s:5:"price";s:5:"50.00";s:8:"quantity";i:1;s:5:"image";s:29:"product-images/laptop-bag.jpg";}}
Change -> if(!isset($_POST['submit'])){}
to isset , because you want to post it if the submit button is set right?
i would change this:
$var_fname = $_SESSION['fname'] = $_POST['fname'];
$var_lname = $_SESSION['lname'] = $_POST['lname'];
.....
to something like this
$var_firstname = $_POST['fname'];
$_SESSION['fname'] = $var_firstname;
and so on
I am new in php tried to write script that read and display data on same page.
Here is my code.
<?php
$fnameErr = $passErr = "";
$fname = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fname"])) {
$fnameErr = "Name is required";
} else {
$fname = $_POST["fname"];
}
if (empty($_POST["password"])) {
$passErr = "Password is required";
} else {
$password = $_POST["password"];
}
}
?>
<p><span class="error">Field Required</span></p>
<form method="post" action="assignment.php">
Name: <input type="text" name="fname">
<span class="error">* <?php echo $fnameErr;?></span>
<br><br>
E-mail:<input type="text" name="password">
<span class="error">* <?php echo $passErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
After running it on web browser it display an error.
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.37 (Win32) OpenSSL/1.1.1 PHP/7.2.12
You should remove your form action - by default the form action is set to the current page
You can test this by adding this into your current .php file (above everything else):
<?php
if (!empty($_POST)) {
echo '<pre>'. print_r($_POST, 1) .'</pre>';
}
?>
<form method="post">
<input type="text" name="foo" />
<input type="submit" name="submit" />
</form>
You'll notice on initial load, the $_POST array won't dump out to the screen. Fill out the form and it will "refresh", posting the input value into the $_POST data with the key foo. This is now shown on the page.
In this program you just creating a form and not specifying any script that display data.
just simply write this script where form creation ends.
<?php
echo "<h2>Your Data:</h2>";
echo $fname;
echo "<br>";
echo $password;
?>
I'm trying to make a verify page for my reservation website but I can't show only specific data from picking the specific id.
For example, I submitted a new customer and it generated an ID = 1. Then the form will take me to another PHP page and I want it to show the name of the customer I just submitted by choosing it's specific ID (which is 1 or whatever id was generated from before).
Here's my first submit form:
<form action="menuactions/temporestoaction.php" method="post" enctype="multipart/form-data">
<label class="control-label">First Name:</label>
<input class="form-control" placeholder="John" type="text" name="first_name" required autofocus/>
<br />
<label>Last Name:</label>
<input class="form-control" placeholder="Doe" type="text" name="last_name" required/>
<button type="submit" name="submit" class="btn btn-success btn-md">Submit</button>
</form>
and this is the temporestoaction.php which will submit all the values into mysql database:
<?php
if(isset($_POST['submit'])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "records";
//Form Inputs to Db
$foodid = $_POST['foodid'];
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO `temporesto` ( first_name, last_name)
VALUES ( '$firstname', '$lastname')";
if (mysqli_query($conn, $sql)) {
header('Location: ../temporesto.php?id='.$row['food_id'].'');
exit();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Which will then redirect to a new PHP page, the temporesto.php:
<?php
include 'menuactions/temporestopick.php';
while($row = mysqli_fetch_array($data, MYSQLI_ASSOC)){
?>
<input type='hidden' value=" <?php echo $_GET['food_id'];?>" name="iduse">
<label class="control-label">First Name: <h2><?php echo $row['first_name'];?></h2></label>
<input class="form-control" type="text" name="first_name" />
<br />
<label class="control-label">Last Name: <h2><?php echo $row['last_name'];?></h2></label>
<?php
}
?>
The problem I have with this is that it shows all of the values submitted instead of a specific one, see this image for reference.
P.S
temporestopick.php is using "SELECT * FROM temporesto";
If you are trying to display a verify page before you process form, then I don't see the need to save first into the database. You can simply post all form values from your form and they would be contained in your $_POST which is an array. You then sanitize all inputs, loop through to get all their values then display them for verification. If all is okay, you sanitize again then insert into your database.
In case you are a little lost, you can do the following.
Submit your form with the values filled to your action page.
In your action page, sanitize all received values from the form.
You can use extract($_POST); to get the values of your form fields into strings.
Display for viewing and confirmation then submit values to database after sanitizing.
Sample:
process.php
<?php
if(isset($_POST['submit']))
{
//sanitize your $_POST values
foreach($_POST as $key => $value)
{
$_POST[$key] = sanitize($value);
//You can out errors from empty fields here if you want
}
//extract $_POST values into strings
extract($_POST);
/*
If your form has something like input name="fname" when you extract you will get the value for name as $name
You can then echo Name: $fname
*/
$values = <<<EOD
Name: {$name}
Email: {$email}
EOD;
}
I hope you are able to get on with this. After this stage of verification then you can save to database to avoid you saving unwanted data into your database.
I removed the whole code inside temporestoaction.php and added this:
<html>
<body>
<?php
if(isset($_POST['submit']))
{
//Form Inputs to Db
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$contact = $_POST['contact'];
$eventdate = $_POST['eventdate'];
$eventtime = $_POST['eventtime1'];
$eventhours = $_POST['eventhours1'];
$packages = $_POST['packages'];
$food = $_POST['food'];
$prices = $_POST['price-total'];
$treats = $_POST['treats'];
$chkfood = "";
$chktreats = "";
foreach($food as $chkfood1)
{
$chkfood.= $chkfood1.",";
}
foreach($treats as $chktreats1)
{
$chktreats.= $chktreats1.", ";
}
?>
<label><h2>NAME: <?php echo $firstname . " " . $lastname; ?></h2> </label>
<br/>
<label><h2>Contact: <?php echo $contact; ?></h2></label>
<br/>
<label><h2>Food: <?php echo $chkfood; ?></h2></label>
<br/>
<label><h2>Event Date: <?php echo $eventdate; ?></h2></label>
<br/>
<label><h2>Event Time: <?php echo $eventtime; ?></h2></label>
<br/>
<label><h2>Event Hours: <?php echo $eventhours; ?></h2></label>
<br/>
<label><h2>Packages: <?php echo $packages; ?></h2></label>
<br/>
<label><h2>Food: <?php echo $chkfood; ?></h2></label>
<br/>
<label><h2>Prices: <?php echo $prices; ?></h2></label>
<br/>
<label><h2>Treats: <?php echo $chktreats; ?></h2></label>
</body>
<?php
}
?>
</html>
With this code, it takes all the inputs from my submit form and transfers it into the next page for verifying.
I just started to familiarize myself with PHP, and what I am trying to do at the moment is when form is submitted, you get redirected to confirmation page where you will see message like:
Thank you John Smith for submiting folowing info:
Address: blah blah blah
Mobile phone number: blah blah blah
Landline phone number: blah blah blah
Email: blah blah blah
(where blah blah blah is data that user entered in form)
So here is my main code:
<?php
if (isset($_POST['submit'])){
header("Location: form.php");
}
?>
<html>
<head>
******
</head>
<body>
<header class="main-header">
***
</header>
<main id="main">
<?php
define('DB_NAME', '***');
define('DB_USER', '***');
define('DB_PASSWORD', '***');
define('DB_HOST', '***');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$forenameErr = $surnameErr = $emailErr = $sendMethodErr = "";
$forename = $surname = $address = $mobile = $landline = $email = $sendMethod = "";
if (isset($_POST['submit'])) {
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$address = $_POST['postalAddress'];
$mobile = $_POST['mobileTelNo'];
$landline = $_POST['landLineTelNo'];
$email = $_POST['email'];
$sendMethod = $_POST['sendMethod'];
}
$sql = "INSERT INTO CT_expressedInterest (forename, surname, postalAddress, mobileTelNo, landLineTelNo, email, sendMethod)
VALUES('$forename', '$surname', '$address', '$mobile', '$landline', '$email', '$sendMethod')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> ">
<fieldset class="formfield"> <!--adds box around content of this element-->
<legend>Your Details</legend> <!--Gives title to this fieldset-->
<p>Fields marked with * are required for form submition.</p>
<label for="forename">First Name:</label>
<input type="text" name="forename" id="forename" placeholder = "Enter your First Name" tabindex="12" required />
<label for="surname">Last Name(s):</label>
<input type="text" name="surname" id="surname" placeholder = "Enter your Last Name" tabindex="13" required />
<label for="postalAddress">Postal Address:</label>
<input type="text" name="postalAddress" id="postalAddress" placeholder = "Your house number, street name and postcode"tabindex="14"/>
<label for="mobileTelNo">Mobile Telephone Number:</label>
<input type="text" name="mobileTelNo" id="mobileTelNo" placeholder = "Enter your mobile number" tabindex="19"/>
<label for="landLineTelNo">Landline Telephone Number:</label>
<input type="text" name="landLineTelNo" id="landLineTelNo" placeholder = "Enter your landline number" tabindex="19"/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" placeholder = "Enter your email address" tabindex="20"/> <br/>
<p> How Would You Like To Recieve Further Information?</p>
<label id="post">Post</label>
<input type="radio" name="sendMethod" value="Post" tabindex="34" placeholder = "please choose one" required >
<label id="eemail">Email</label>
<input type="radio" name="sendMethod" value="Email" tabindex="35">
<label id="sms">SMS Text</label>
<input type="radio" name="sendMethod" value="SMS" tabindex="36">
<p><input type="checkbox" name="checkbox" value="check" id="agree" required /> I have read and agree to the Terms and Conditions and Privacy Policy </p>
<p>
<input type="submit" name="submit" value="Submit!" />
</p>
</fieldset>
</form>
</main>
<footer class="main-footer">
***
</footer>
</body>
</html>
And here is what i have so far for form.php, but obviously it is not working. (i am not sure if i need to connect to database in this file as well)
echo "<p>Thank you $forename $surname</p>";
echo "<p>We have recieved following information:</p>";
echo "<p>Address: $address</p>";
echo "<p>Mobile phone number: $mobile</p>";
echo "<p>Landline phone number: $landline</p>";
echo "<p>Email: $email </p>";
Can anyone please help me to finish form.php code?
And one more thing. After I press submit button, two new records are added to my database. One with info that was entered it the form and one is completely empty.
I would appreciate any help. Thank you!
p.s. i an not allowed to use any jquery or ajax things.
Few comments for this code...
First, if you use:
header("Location:form.php");
You actually redirect the navigation to the file "form.php" on the same path than your main code. Two different way:
1 - Don't redirect to "form.php" by removing:
<?php
if (isset($_POST['submit'])){
header("Location: form.php");
}
?>
and just test if your data are set and do the job, don't change the location.
if ( !empty($_POST['submit'])
&& !empty($_POST['postalAddress'])
&& ...
&& !empty($_POST['sendMethod']) ) {
// Use data
else {
// Show form
}
2 - You can actually use a file called form.php but directly with your html form:
<form method="post" action="form.php">
...
</form>
And so, just get your code for "form.php", with some changes:
<?php
/* You have to valide all your inputs
* And use empty instead of isset
* it tests for undifined (like isset), null value
* and if it's empty
*/
if ( empty($_POST['submit'])
|| empty($_POST['postalAddress'])
|| ...
|| empty($_POST['sendMethod']) ) {
// There is no (valid) form
header("Location: yourMainCode.php");
else {
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$address = $_POST['postalAddress'];
$mobile = $_POST['mobileTelNo'];
$landline = $_POST['landLineTelNo'];
$email = $_POST['email'];
$sendMethod = $_POST['sendMethod'];
// Database stuff...
// Redirect to a result page or something
header("Location:result.php");
}
?>
Hope it helps !