I'm looking for a simple solution to what is hopefully a simple problem. I want to have a laptop set up with an offline html file with a very short form that feeds a csv file. I've been eyeing the fputcsv() function to do this, but I'm not the most talented programmer. If I have a simple form that looks like this:
<?php
if(isset($_POST['submit']))
{
$myfile = fopen('file.csv', 'w');
fputcsv($myfile,
array($_POST['first-name'], $_POST['last-name'], $_POST['email']));
fclose($myfile);
}
?>
<article role="main">
<header role="banner">
<h1>Email Updates</h1>
</header>
<section>
<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF'];?>">
<input type="text" id="first-name" maxlength="100" autocorrect placeholder="First name" />
<input type="text" id="last-name" maxlength="100" autocorrect placeholder="Last name" />
<input type="text" id="email" maxlength="100" autocorrect placeholder="Email address" />
<button type="submit" id="submit" class="oneup">Submit</button>
</form>
</section>
</article>
what kind of code do I need to have it feed a simple csv file?
When (if) the form is submitted (correctly), do this:
if( $fp = fopen('file.csv', 'w') )
{
fputcsv($fp, $_POST);
}
fclose($fp);
When this form is submitted, it will populate the $_POST array.
So, you should add some PHP code that handles the submitted values.
For example:
<?php
if(isset($_POST['submit']))
{
$myfile = fopen('file.csv', 'w');
fputcsv($myfile,
array($_POST['first-name'], $_POST['last-name'], $_POST['email']));
fclose($myfile);
}
?>
Similar to djot's answer, I'd use this:
if( $fp = fopen('file.csv', 'w') ){
fputcsv($fp, print_r($_POST, true));
}
fclose($fp);
Note the print_r with the true flag, as this makes it more human readable.
If you actually wanted to write it as a CSV, just use:
$data = implode(',' $_POST);
if( $fp = fopen('file.csv', 'w') ){
fputcsv($fp, $data);
}
fclose($fp);
Related
Right now I have code that submits form data and gets put into a text file and then after I click a button to go to the next page. I want to be able to submit the form data and go to the next page on the same click. I'm using PHP and HTML right now. Is there anyway to successfully do this?? I tried an onclick like I have for my next button but it doesn't seem to work with the input type function.
<h2>Please enter your name.</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
First Name: <input type="text" name="fName">
<br><br>
Middle Initial: <input type="text" name="mInitial">
<br><br>
Last Name: <input type="text" name="lName">
<br><br>
<input type="submit" name="submit" value="Submit">
<button type="button" name="submit" value="Submit"
onclick="location.href='county.php'">Next</button>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['submit']))
{
replace();
}
function replace() {
$myFile = "freewill.doc";
$fh = fopen($myFile, 'a') or die("can't open file");
$fName = $_POST["fName"];
$mInitial = $_POST["mInitial"];
$lName = $_POST["lName"];
$placeholders = array('fin', 'min', 'lana');
$namevals = array($fName,$mInitial,$lName);
$path_to_file = 'freewill.doc';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($placeholders,$namevals,$file_contents);
file_put_contents($path_to_file,$file_contents);
fclose($fh);
}
?>
Change below line
<button type="button" name="submit" value="Submit"
onclick="location.href='county.php'">Next</button>
to
<input type="submit" name="submitform" value="Submit">Next</button>
and change PHP code to this
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['submitform']))
{
$myFile = "freewill.doc";
$fh = fopen($myFile, 'a') or die("can't open file");
$fName = $_POST["fName"];
$mInitial = $_POST["mInitial"];
$lName = $_POST["lName"];
$placeholders = array('fin', 'min', 'lana');
$namevals = array($fName,$mInitial,$lName);
$path_to_file = 'freewill.doc';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($placeholders,$namevals,$file_contents);
file_put_contents($path_to_file,$file_contents);
fclose($fh);
header('Location: county.php');
}
Try setting an onSubmit method for your form instead.
I'm trying to make a forum-like section for my website. It's not posting, and I don't know why. Here's my PHP and html
<?php
if ($_POST) {
$title = $_POST['title'];
$name = $_POST['name'];
$content = $_POST['commentContent'];
$handle = fopen("comments.html", "a");
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3
class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");
fclose($handle);
}
?>
<form action="" method="POST">
<textarea class="comment-boxmain" rows="20" cols="40" name="commentContent"
placeholder="Start Typing...."></textarea><br>
<input class="comment-boxname" placeholder="Title" type="text"
name="title">
<input class="comment-boxname" placeholder="Your Name" type="text"
name="name">
<input class="comment-btn" type="submit" value="post"><br>
</form>
<?php include "comments.html"; ?>
Please check out the answer at cvmblog.com/forum.php if that will help.
String concatenation is done with dots (.), and not commas (,).
Replace:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3
class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");
With:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>". "<br>". "<h3
class='Roboto-Slab'>By $name</h3>". "<p class='Roboto-Slab'>$content</p>");
And it will work. However, this concatenation is useless. You can do simply:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3 class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");
Also check if comments.html file has CHMOD 777. Furthermore, enable error_reporting on your php.ini file, as the PHP error thrown on this case could guide you to the error line easily.
Here's an implementation of your code secured against stored XSS (the vulnerability that allows people to insert HTML and Javascript code on your page) as well as RCE (remote code execution):
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$title = strip_tags($_POST['title']);
$name = strip_tags($_POST['name']);
$content = nl2br(htmlspecialchars($_POST['commentContent']));
$handle = fopen("comments.html", "a");
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3
class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");
fclose($handle);
}
?>
<form action="" method="POST">
<textarea class="comment-boxmain" rows="20" cols="40" name="commentContent"
placeholder="Start Typing...."></textarea><br>
<input class="comment-boxname" placeholder="Title" type="text"
name="title">
<input class="comment-boxname" placeholder="Your Name" type="text"
name="name">
<input class="comment-btn" type="submit" value="post"><br>
</form>
<?php echo file_get_contents("comments.html"); ?>
Also, do some searching about database engines (if you want to still using files, take a look on implementation of flat-files databases, as it's called).
im working on a website where i want to take the outcome from a from and want them saved in a seperate file as php variables so i can read them out later on a diffrent part of the site. i can write some php code to a file but i writes the name of the variable and not the value.
<form action="change.php" method="post">
<input type="text" class="form-control" id="inputlocation1" placeholder="<?php include 'data1.php'; echo $location1 ?>">
<br>
<input type="text" class="form-control" id="inputdate1" placeholder="<?php include 'data1.php'; echo $date1?>">
<br>
<textarea name="description1" class="form-control"></textarea>
<br>
<input type="submit" name="submit" value="update data" class="btn btn-primary">
i use numbers behind the id's because i have multiple dates and locations i want to change. this is my current php code:
<?php
$file = fopen("data1.php", "w") or die("Unable to open file!");
$date = $_POST["inputdate1"];
$loc = $_POST["inputlocation1"];
$desc = $_POST["description1"];
fwrite($file, '<?php $loc1 = $loc' );
fwrite($file, '$date1 = $date');
fwrite($file, '$desc1 = $desc');
fwrite($file, '?>');
fclose($file);
?>
who can help me?
$data = '$desc1 =' . $desc;
fwrite($file, $data);
you must use SIGLE QUOTES
help i have bit knowledge in html,css but
im kinda new to php forms,database,etc stuffs
my php is working well, when user click submit all forms goes to carlist.txt
but i want it output it through webpage html instead of .txt so i can view it online
how i can able to do that?
and i dont want the users sees their submisions... only me can view it
html :
<html>
<form action"awesome.php" method=get>
First name: <input type="text" name="firstname"><br>
blah.. blahh.
<select name="cars">
<option value="volvo">Volvo XC90</option>
<option value="saab">Saab 95</option>
<option value="mercedes">Mercedes SLK</option>
<option value="audi">Audi TT</option>
</select>
<input type="submit" value="Submit">
</form>
etc. etc. blah blah
</html>
awesome.php:
<?php
header("Location: thanksforsubmiting.html");
$handle = fopen("carslist.txt", "a");
foreach($_GET as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
thanksforsubmitting.html:
<html>
<title>thank you</title>
<h1> <font color=red>
thank you for submission </h1></font>
</html>
please be gentle to me im kinda newbie :)) TIA
<?php
foreach($_GET as $variable => $value) {
echo $value."<br />";
}
?>
If you want view it online create another page with something like this
$file = file_get_contents('carlist.txt');
$rows = explode("\n", $file);
foreach($rows as $row) {
print_r($row);
}
at awesome.php write following code
if(isset($_GET['submit'])){
$firstName = $_GET['firstname'];
$car = $_GET['cars'];
echo $firstname;
echo $car;
}
ok here is an example code you want. i'm using just 2 files form.php and form1.php. Form.php contains:
<?php
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
header("Location:form1.php");
}
?>
<form name="test" action="" method="post">
<label for="fname">First Name:</label>
<input type="text" name="fname"/>
<label for="lname">Last Name:</label>
<input type="text" name="lname"/>
<input type="submit" name="submit" />
</form>
and form1.php contains following statement:
<?php
echo "Form Submitted!";
?>
On submition form will stay on the same page since action is null in form. In the if statement it will be redirected!
So I have a variable well defined in a php page and I'm using it in an HTML page using include.
I am currently building a page where I can change the Var ( because it's a long text, more than one actually, and to change them it will be nice to have a page with a layout just for that) so I'm using a textbox and a submit button just like this:
<?php
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
?>
<form method="post">
Titre: <input name="titre" type="text" id="titre" value="<?php echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>
The problem is that in the echo it shows the new text but if I do a refresh it will show the old one...
any ideas how can I do this?
EDIT: Added extra fields and data handler. See extra code below original answer.
Here is some code I came up with to write content to a file.
Note: To add to the file with content written one under the other, use the a or a+ switch.
To create and write content to file and overwrite previous content, use the w switch.
This method uses the fwrite() function.
(tested)
Added to OP's code: action="write.php"
FORM
<?php
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
?>
<form method="post" action="write.php">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
PHP write to file handler (write.php)
This example uses the w switch.
<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>
<?php
$filename = "output.txt"; #Must CHMOD to 666 or 644
$text = $_POST['titre']; # Form must use POST. if it uses GET, use the line below:
// $text = $_GET['titre']; #POST is the preferred method
$fp = fopen ($filename, "w" ); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $text. "\n");
fclose ($fp);
echo ("File written");
}
else {
echo ("File was not written");
}
?>
EDIT: Added extra fields and data handler.
Extra fields can be added, and must be followed in the same fashion in the file handler.
NEW FORM with extra fields
File data example: test | email#example.com | 123-456-7890
<?php
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
?>
<form method="post" action="write.php">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<br>
Email: <input name="email" size="50" maxlength="50">
<br>
Telephone: <input name="telephone" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>
PHP write to file handler
<?php
$titre = $_POST['titre'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$data = "$titre | $email | $telephone";
$fp = fopen("data.txt", "a"); // a-add append or w-write overwrite
if ($fp) {
fwrite ($fp, $data. "\n");
fclose ($fp);
echo ("File written successfully.");
}
else{
echo "FAILED";
}
?>
<?php
if(!($titre = file_get_contents("filename.txt"))){
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
}
?>
<form method="post">
Titre: <input name="titre" type="text" id="titre" value="<?php echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$titre = $_POST['titre'];
if(#file_put_contents("filename.txt", $titre))){
echo 'Success - var stored.';
} else { echo 'Some error.'; }
echo($titre);
}
?>
Try this :
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
If you need to keep your value for ever, you should store it in a database or save it in a file (could be .txt).
[EDIT]
Here is the code for .txt solution (you first create a file.txt in the same folder):
<?php
$file = 'file.txt';
$lines = file("file.txt");
if (!isset($lines[0])) {$titre='Bienvenido a PARIS EXPERT LIMOUSINE ! ';}
else {$titre=$lines[0];}
?>
<form method="post">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
echo($_POST['titre']);
$titre = $_POST['titre']."\n".$titre;
file_put_contents($file, $titre);
}
?>
Hope it helps :)
this is normal because you're showing the new content upon form submission. When you refresh the page, unless you tell it to send the POST data again with the refresh (which the browser asks you for confirmation), your form (and hence the input field) will have nothing in.