Jump to content

Store data into php file without being directed to that page


Bentley4

Recommended Posts

Hello!

 

I'd like to know how one can save inputted data using a form to store it into another php file.

The user should just input their name in the php webpage called "MainPage.php". When they write their names and hit submit, they would just stay on the same page. Their submitted names should be stored permanently into a "Answers.php" file.

What do I need to change in my "MainPage.php code?

 

<html>
  <body>
<form method="post" action="Answers.php" >
Name:<input type="text" name="Name" />
<input type="submit" value="Submit name" />
</form>
[some php code not relevant to question]
  </body>
</html>

Link to comment
Share on other sites

You mean that they fill in their name in MainPage.php and it shows up on Answers.php? Whatever that case would be, there's no permanent storage unless you go for a database approach. The options you have, appart a database, are sessions and cookies.

 

Answers.php

<?php
session_start(); //initializes sessions and should be place at the top of the document
if (isset($_POST['name'])) {
     $_SESSION['name'] = htmlentities($_POST['name']);
     echo $_SESSION['name'];
}
?>

 

Once $_SESSION['name'] has been set, it can be called from any page you like, be it Answers.php, Questions.php or even Ducks.php :)

Link to comment
Share on other sites

Thank you for your response guilty gear.

 

I just found a blog that claims it is possible and explains how to save web form data through using this php code:

 

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$fp = fopen(”formdata.txt”, “a”);
$savestring = $name . “,” . $email . “n”;
fwrite($fp, $savestring);
fclose($fp);
echo “<h1>You data has been saved in a text file!</h1>”;
?>

From tutorial:

http://www.howtoplaza.com/save-web-form-data-text-file

 

1. Why does the the person who wrote the tutorial save this php code into a seperate php file?

 

2. When I use this code in my php file of my webpage I get this error:

Parse error: syntax error, unexpected ','

And it refers to this line:

$savestring = $name . “,” . $email . “n”;

Why?

 

Link to comment
Share on other sites

No, the problem isn't solved!

 

I did everything according to that tutorial:

http://www.howtoplaza.com/save-web-form-data-text-file

 

1. I Implemented that first piece of code in my main webpage php file.

2. Made a "formdata.txt" file,  inputted 'Name,Mail,[enter]' and saved it in my webfolder.

3. Then made a “process-form-data.php” in my webfolder and inserted this code:

 

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$fp = fopen(”formdata.txt”, “a”);
[color=blue]$savestring = $name . “,” . $email . “n”;[/color]
fwrite($fp, $savestring);
fclose($fp);
[color=blue]echo “<h1>You data has been saved in a text file!</h1>”;[/color]
?>

 

When I check the webpage of “process-form-data.php” it says

Parse error: syntax error, unexpected ',' in XXXX/process-form-data.php on line 7

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in XXXX/process-form-data.php on line 10

Why? I don't see any concatenation errors in line 7 nor any mistakes in line 10

 

Link to comment
Share on other sites

The error is caused by the curly quotes used throughout the code. If you change everything to straight quotes, it should work just fine.

 

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$fp = fopen("formdata.txt", "a");
$savestring = $name . "," . $email . "n";
fwrite($fp, $savestring);
fclose($fp);
echo "<h1>You data has been saved in a text file!</h1>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.