I've created an html page with forms, and what I need is for each new person that clicks the 'submit' button, have the text in the fields print to a new line in the output.txt file on the web host.

This is what I have so far:

<?PHP

$filename = "output.txt"; #Must CHMOD to 666
$quantity = $_POST['quantity'];
$item = $_POST['item'];

$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $quantity . ", " . $item);
fclose ($fp);
echo ("Thank you for submitting your video!");
}
else {
echo ("There was a problem submitting your video. Try again, or report the issue.");
}

?>

Here's the HTML code:

<form action="process.php" method="post"><br /><br />
Link to YouTube Video:<br /><input name="quantity" type="text" size="50"/><br /><br />
Description of Video: <br /><textarea cols="38" rows="5" name="item"></textarea><br /><br />
<input type="submit" />
</form>

Also,
1. How could I have each line numbered for each new submission?
2. How can I make the text from '$quantity' turn into a link in the output.txt file?

Recommended Answers

All 11 Replies

Member Avatar for diafol

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 . "<br />";
  $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.

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 . "<br />";
  $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.

For the first text field (which is represented as $quantity, don't ask why), is a link which the person would put there. I need the link (which is in text form), to be a URL in the output file. Also, how can I have each new submission create the text on a new line in the output file?

Member Avatar for diafol

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);

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);

Since the output is a .txt file, it can't change it to a link. What would I have to change the extension to?

EDIT: just changed it to output.html. I'm not sure how I would use the newline code you gave me though, I've been fiddling with it, but can't seem to get it to work. Every time I try, it just overwrites the existing text when I submit again.

Member Avatar for diafol

.htm or .html

However, you really need to have <html> 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 </body></html>.

Is this file supposed to be used as a stand-alone file, e.g. to be distributed? or can you use it as text (e.g. my 3rd suggestion for overwriting) to be opened in a html (with SSI) or php file?

.htm or .html

However, you really need to have <html> 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 </body></html>.

Is this file supposed to be used as a stand-alone file, e.g. to be distributed? or can you use it as text (e.g. my 3rd suggestion for overwriting) to be opened in a html (with SSI) or php file?

The html page (which contains all of the forms), is to be distributed. Once the submit button is pressed by whoever fills out the forms, I need those lines of text to be sent to the output.html (which used to be output.txt) file which is just for my viewing. Every time someone new submits text, I need it to output it on a new line, so that it doesn't over-write the other lines in the output file.

Member Avatar for diafol

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.

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.

I already have all of the forms ready, and what you gave me to print the text when you hit "Submit" works fine. All I need is for the next person to submit a new form, for THAT text to be put in the output file BELOW the already existing ones.

So it looks like this in the output file:

http://www.google.com, Description
http://www.youtube.com, Hello
Next submission
Next submission
etc.
Member Avatar for diafol

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><br />" ;
}
//...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!

Okay, I got it all working, but I need a couple things:

1. If someone doesn't include the http:// in the link, it won't redirect in the file (it brings me to public_html/www.google.com).
2. How can I make it so that if there is nothing in the two forms, you can't submit?
3. How can there be a timer between each submission (5 minutes)?

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.