Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple html/php form to output on the same page

Tags:

html

forms

php

Basically all I want is for users to come into the site, type in their message and name, and the results should be displayed in the same page and when another user comes in again, the results from previous user should be there and anything that comes up should just be added to the list.

currently I have:

 <form id="form1" name="form1" method="post" action="">
 <label>Please type in a message
 <input type="text" name="msg" id="msg" />
 </label>
 <label>and your name
 <input type="text" name="pin" id="name" />
 </label>

 <p>
 <label>Submit
 <input type="submit" name="submit" id="submit" value="Submit" />
 </label>
 </p>
 </form>

<?php 
$msg = $_POST[msg];
 $name = $_POST[name];

?>
 <br />
 <?php echo "$msg"?>
 <?php echo "$name"?>

but when another record is type in, the previous one is lost...

thanks in advance

like image 424
khalid Avatar asked Jun 28 '11 16:06

khalid


3 Answers

This should do what you want. It loads previous posts from posts.txt, adds the current one, displays the posts and saves it. You'll need to make sure posts.txt exists and has correct permissions.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Untitled Document</title>
 </head>

<body>
 <form id="form1" name="form1" method="post" action="">
 <label>Please type in a message
 <input type="text" name="msg" id="msg" />
 </label>
 <label>and your name
 <input type="text" name="name" id="name" />
 </label>

 <p>
 <label>Submit
 <input type="submit" name="submit" id="submit" value="Submit" />
 </label>
 </p>
 </form>

 <?php
    $msg = $_POST["msg"];
    $name = $_POST["name"];
    $posts = file_get_contents("posts.txt");
    $posts = "$msg - $name\n" . $posts;
    file_put_contents("posts.txt", $posts);
    echo $posts;
 ?>

</body>
 </html>
like image 139
Andrea Avatar answered Nov 18 '22 16:11

Andrea


That's where a database is needed. Just make a simple sql database or create a file that is appended every time a user posts his/her message. So whenever your page loads, it must first load the previous stored data and then proceed further.

like image 39
Nishchay Sharma Avatar answered Nov 18 '22 15:11

Nishchay Sharma


Couple things:

$first = $_POST['msg'];
$second = $_POST['pin'];

You need those single quotes around the post argument in order to save it to a variable.

Secondly, in order to store data, you need to keep it somewhere, whether it be a text file or MySQL database. Post data only exists when the form is posted. After that, it's destroyed.

Probably the easiest thing for you to do is append to a text file (if this is a simple application).

This should get you started.

like image 33
Steve Robbins Avatar answered Nov 18 '22 16:11

Steve Robbins