-5

I want to do following task, first of all I have created this html file, namely form.html

<html>
<head>
<title>please insert  your personal information </title>
<head>
<body>
<form  action="table.html"method="post">
ID : <input type="text" name="ID" value=" " > </br>
FIRST_Name : <input type="text" name="FIRST_Name" value=" "/></br>
Last_name :<input type="text" name="Last_name" value= " " /></br>
job_id :<input type="text" name="job_id" value=" " /></br>
JOB_Description : <input type="text" name="JOB_Description" value=" " /></br>
Locations : <input type="text" name="locations" value=" "></br>
<input type="submit"   name="submit" value="submit"/></br>

</form>
</body>
</html>

My aim is that when user enters his/her information, it should redirect to another file, where is created table form and this information is written in this table, such that every time anybody does fill this fields,this table will grow and does not overwrites existed information, so I am interested how to do it in table. HTML (which is a page where my form.html should go after clicking submit buttons) also I am interested should I save my table in html extension or php extension? Reason why I am asking it is that can I access variables declared in one html file from another html file without php $_POST method? Or can I use php in a table. html for just variable access?

Havelock
  • 6,745
  • 3
  • 35
  • 41
dato datuashvili
  • 17,789
  • 59
  • 193
  • 310
  • are you sure you can't use database? – lvil Jun 03 '12 at 08:44
  • You will have to use a database to store this data. – Oliver Jun 03 '12 at 08:44
  • you did not understand me,i have not said that i can't use database,but i want to do it myself,just practice,so please don't downvote if you don't know what i am going – dato datuashvili Jun 03 '12 at 08:47
  • 4
    A friendly reminder: there's a space after punctuation, sentences start with a capital letter as does the word "I". I wouldn't normally mention it, but you have almost 500 questions that all seem to be written like this. – JJJ Jun 03 '12 at 08:51
  • @Juhana this comment is not problem for me,because i am not native english man,so yes maybe i have some wrongly written sentences – dato datuashvili Jun 03 '12 at 08:52
  • 3
    That's ok. Just try to use capital letters and spaces after punctuation ("me, because", not "me,because") in the future. – JJJ Jun 03 '12 at 08:54

2 Answers2

2

html does not have variables by itself. In order to save data you will need a programming language like PHP or GAE with Python for example. You can either save the data into a database or into a simple file using php. You can then load the data from the file into a table with a simple 'for' loop.

Like you mentioned, you will take the data from $_POST and save it to file. (data.txt) And then display data.txt in a table format.

To save data:

<?php

$filename = "someData.txt";
$text = $_POST['form_field_name_here'] . ' ' .  $_POST['second_field_name_here']; //just continue with as many fields as necessary    
$fp = fopen ($filename, "a+");
if ($fp) {
    fwrite ($fp, $text);
    fclose ($fp);
}

To print a table from a file you can use a for loops like this:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
    <table id="myTable">
        <?php // PHP code starts here
        $f = fopen("someData.txt", "r");
        while (($line = fgets($f)) !== false) {
            echo "<tr>";
            $cells = explode(" ", $line);
            foreach ($cells as $c) {
                echo "<td>" . htmlspecialchars($c) . "</td>";
            }
            echo "<tr>\n";
        }
        fclose($f); 
        // end of PHP code
        ?>
    </table>
</body>
</html>

And here's your style.css

#myTable  
{
    border: solid 1px black;
}
AturSams
  • 7,059
  • 17
  • 59
  • 94
  • if for example i use php in file ,which has html extension could i do it so? – dato datuashvili Jun 03 '12 at 08:51
  • No, you can't, file extension should be .php. Files with .html extension won't be interpreted by the PHP interpretor. – Havelock Jun 03 '12 at 08:56
  • but i can't create tables in php files yes? – dato datuashvili Jun 03 '12 at 08:57
  • You can create a table in a php file. php files can out html – AturSams Jun 03 '12 at 08:58
  • please could you inform me,how could i do it? – dato datuashvili Jun 03 '12 at 09:02
  • You can create any html tag with php, you simply open a php section: ... whatever – AturSams Jun 03 '12 at 09:03
  • and does it adds new rows when user enters his information in form.html file? – dato datuashvili Jun 03 '12 at 09:07
  • also so it means that table should be php file instead of html right? – dato datuashvili Jun 03 '12 at 09:08
  • Yes, you probably wish to use a php file. – AturSams Jun 03 '12 at 09:09
  • To add new rows you will need to write to someData.txt - see the edit above – AturSams Jun 03 '12 at 09:10
  • also i need two php file yes?one for save this document into data.txt file and second read from this right? – dato datuashvili Jun 03 '12 at 09:11
  • You don't have to use two php files. You can use one, first run the code that saves and then the code that reads. – AturSams Jun 03 '12 at 09:14
  • ok thanks @ Arthur Wulf White,i will try and inform you if any problem i have – dato datuashvili Jun 03 '12 at 09:15
  • Do you have Wamp to run php? Other than that, the code up there in the edited answer will do the trick. – AturSams Jun 03 '12 at 09:17
  • That is fine, let me know if it worked as expected, if not you're welcome to lets me know what error you get. Or if you have questions – AturSams Jun 03 '12 at 09:36
  • thanks very much @Arthur Wulf White,you know one biggest paradox is that people think downvoting is a good help for poster,but in really this is a great help,answer not more – dato datuashvili Jun 03 '12 at 09:38
  • yes are you here?i have question fwrite function in php has two parameter yes?i want to append new information from new line and how could i do? – dato datuashvili Jun 03 '12 at 10:06
  • You do this using the [fopen()](http://de.php.net/manual/en/function.fopen.php) function, using **a** or **a+** (stands for **append**) as `$mode`, something like this `$handle = fopen('path/to/your/file', 'a+');` But I'd still recommend using a database instead of file ;) – Havelock Jun 03 '12 at 10:13
  • so in my case it would be $fp = fopen ($filename, "wa+"); right? – dato datuashvili Jun 03 '12 at 10:15
  • 'a+' would suffice - read the manual [fopen()](http://de.php.net/manual/en/function.fopen.php) – Havelock Jun 03 '12 at 10:23
  • 1
    Yeah, I am not even sure you need a+, I think 'a' would be fine. Havelock is correct about using a DB, if you have thousands of users I'd suggest using Google App Engine with Python.. using it with Django whole code would be simpler and rely on a reliable storage system. The problem with files is that if you have many users and two access the file in the same time for writing, one of the user's data might be lost. – AturSams Jun 03 '12 at 10:36
  • yes i know it,but also you loop for each has bugs,for example this one Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\base_table\table.php on line 20 – dato datuashvili Jun 03 '12 at 10:58
  • Back to the php/htlm question - you can also use plain html in .php files. You don't need to `echo '...'` if it's a static element, you could simple use plain HTML `
    ...` and only use php to create the dynamic content.
    – Havelock Jun 03 '12 at 10:59
  • you may wanna try `foreach($cells as $cell)` instead of `foreach($c as $cells)` that should fix it ;) – Havelock Jun 03 '12 at 11:02
  • yes it works,last question can i put it into table now?i mean it is in table form,but without borders,could i do it? – dato datuashvili Jun 03 '12 at 11:06
  • Well, that's just simple CSS. You can do the following `
    – Havelock Jun 03 '12 at 11:20
  • yes,but in my code i have made just echo table and ho can i do it? – dato datuashvili Jun 03 '12 at 11:23
  • I've just edited @ArthurWulfWhite 's answer. You may need to wait though till someone has approved my edit... – Havelock Jun 03 '12 at 11:34
1

Use the GET instead of the POST method and process the data on your second page using JavaScript, see here:

Access GET directly from JavaScript?

Community
  • 1
  • 1
JohnB
  • 12,707
  • 4
  • 36
  • 64
  • Good luck with the *"such that every time anybody does fill this fields,this table will grow and does not overwrites existed information"* ;-) – Arjan Jun 03 '12 at 08:57
  • Sorry, obviously I did not understand that part correctly. Do you mean that the table should be stored *on the server*? – JohnB Jun 03 '12 at 09:00
  • no,my question refers what,when user fills his information,it automatically will be written table so that rows would be grown and adds new informations without overwrite existing informations,like database does it when insert values in table – dato datuashvili Jun 03 '12 at 09:03
  • Ok, then you need more than plain html. Your first page does not need php. Your second page could contain PHP which (1.) appends the data obtained from POST to a text file and (2.) afterwards reads the text file and creates a table from it. – JohnB Jun 03 '12 at 09:08