Could I ask why you're using a txt file to store html code? ANyway.
$quantity = "<a href=\"...whateverurl...\">" . $_POST['quantity'] ."</a>";
//what's the link url?? Seems strange that the quantity would be the link text...
As for numbering, many different ways:
1. keep a separate text file with the last number in it. Read it, increment by 1, overwrite it after successfully adding to the main output.txt file.
2.keep a running total at the bottom (last line) of the output.txt file like:
"Total XX entries". Extract the text via preg_match() [see manual if you don't know about this]. Read and overwrite the file [more complicated than 1.]
3. Don't add the entry number to the file at all. Whenever you read the file via PHP, run a function that extracts the data into an array and dynamically apply numbers:
$x=1;
foreach($lines as $line){
echo $x . ". " . $line . "";
$x++;
}
Of course you don't get any numbers if you simply open the output.txt file in a text editor.
I bet there are even more ways to do it - just 3 off the top of my head.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
OK
$quantity = "<a href=\"{$_POST['quantity']}\">{$_POST['quantity']}</a>";
For reading and overwriting:
$str = file_get_contents($filename);
$addstr = $str . "\n" . $newdata; //newdata is the new data from the posted form
file_put_contents($filename,$addstr);
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
.htm or .html
However, you really need to have tags and a DTD and other page elements if it is truly a well-formed html doc. That means you'd need a little 'jiggery pokery' to insert a new record into text that has end tags like
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
OK, the html file ISN'T going to be distributed, it's just going to be viewed by users in a browser. You're going to view the data in a browser from what I can gather, so you don't need html tags, just a container for the data.
SO you need 4 files:
1. Form file (e.g. senddata.php)
contains form
2. Form handler (e.g. handler.php)
receives data
extracts string from output.txt file
overwrites file with old data + new data
returns user to senddata.php with a message - either successful write or unsuccessful write
3. Data file (e.g. output.txt)
just raw output as text (you can write html in it if you want - but probably best to let the data viewer parse it)
4. Data viewer (e.g. dataview.php)
Grab the file contents and echo out (or even parse) with the relevant html tags.
You could get away with just two files, but that would make things extremely untidy and difficult to maintain.
If you use the data file and data viewer 'model', you could store your data in CSV or XML or even JSON format. The delimited format is probably the easiest to use - where the delimiter for CSV is usually a ',' but you could use anything you want.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
OK, as you're using 'long' text as the second piece of data, delimiting the input could be tricky, but should be straightforward with preg_split().
you can read in your file thus:
$str = file_get_contents($filename);
$lines = explode("\n",$str);
This just places each entry into an array called $lines
Next you need to split each line on ',' to get each part (quantity and item).
$output = "";
foreach($lines as $line){
$s = preg_split('/[\s]*[,][\s]*/', $line,2);
$output .= "<p>{$s[0]}</p><p>{$s[1]}</p>" ;
}
//...place the line below anywhere you want in the page...
echo $output;
That should output the entire contents of the file. Obviously play with the html tags and CSS to get it to look as you want.
There's probably an easier way to do this, but I've just started to get my head around regex functions, so I'm trying to use them everywhere!
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
1, Use VALIDATE FILTERS to check for valid url
if(filter_input(INPUT_POST, 'quantity', FILTER_VALIDATE_URL) !== false){
//valid url
}
//I'm not expert on this function, so have a look at what the flags do:
http://uk3.php.net/manual/en/filter.filters.validate.php
Inside this if block, you need to check that the 'scheme' is acceptable to you. Assume you only want http, https.
if(filter_input(INPUT_POST, 'quantity', FILTER_VALIDATE_URL) !== false) && $_POST['item'] != ""){
$url = $_POST['quantity'];
$desc = $_POST['item'];
$allowed = array("http","https");
$scheme = parse_url($url, PHP_URL_SCHEME);
if($scheme == "")$url = "http://" . $url; //just forces absent scheme to default to http://
if(in_array($scheme,$allowed){
//this is where you now add the data to the file
}
}
Off top of my head, so it may not work as is. Also I've not included any error checking / feedback
2. You can use JS to prevent form submission, BUT data validation MUST take place on the server as well (php) - as above with the
if(filter_input(INPUT_POST, 'quantity', FILTER_VALIDATE_URL) !== false) && $_POST['item'] != ""){
3. For this to work properly, you need to set a 'session', That'll take too much time for me to explain
Anyway, I think I've given you enough to get on with.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080