...

View Full Version : Help with PHP Forms



Feriscool
09-05-2011, 04:25 PM
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?

mlseim
09-05-2011, 07:45 PM
First off, you'll want to use "a" (append) instead of "w" (write), since you
are appending to your text file and not overwriting it.

You don't really need to number each line, because you can do that when
you display the text file. The only concern you need to have is how the
file gets sorted. As you append, the newest line goes on the bottom of
the .txt file.

If that's OK, you can leave it alone ... if not OK, you have to determine how
you want the file sorted ... by video name, by date entered, by quantity?

So, append to the file like you're doing ....


<?PHP

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

$fp = fopen ($filename, "a+"); // a = append to the existing file, + = create if it doesn't exist
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.");
}

?>



Now, any time you wish to display the data from the file ...


<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){
echo "Row: $index &nbsp;&nbsp; Quantity: ".$items[0]." &nbsp;&nbsp; <a href='".$items[1]."'>".$items[1]."</a> <br />";
$index++;
}
}


?>



Test that out first ... then make decisions on how to sort your lines.



.

Feriscool
09-05-2011, 09:25 PM
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)?

Feriscool
09-06-2011, 12:33 AM
First off, you'll want to use "a" (append) instead of "w" (write), since you
are appending to your text file and not overwriting it.

You don't really need to number each line, because you can do that when
you display the text file. The only concern you need to have is how the
file gets sorted. As you append, the newest line goes on the bottom of
the .txt file.

If that's OK, you can leave it alone ... if not OK, you have to determine how
you want the file sorted ... by video name, by date entered, by quantity?

So, append to the file like you're doing ....


<?PHP

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

$fp = fopen ($filename, "a+"); // a = append to the existing file, + = create if it doesn't exist
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.");
}

?>



Now, any time you wish to display the data from the file ...


<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){
echo "Row: $index &nbsp;&nbsp; Quantity: ".$items[0]." &nbsp;&nbsp; <a href='".$items[1]."'>".$items[1]."</a> <br />";
$index++;
}
}


?>



Test that out first ... then make decisions on how to sort your lines.



.

For some reason it's not changing the text link to an actual link. Here are both of the codes:

process.php:

<?PHP

$filename = "output.txt"; #Must CHMOD to 666
$quantity = $_POST['quantity'];
//$quantity = "<a href=\"{$_POST['quantity']}\">{$_POST['quantity']}</a>";
$item = $_POST['item'];

$fp = fopen ($filename, "a");
if ($fp) {
fwrite($fp, $quantity.",".$item."\r\n");
fclose ($fp);
if ($item == "") {
echo ("Go back and fill in the description!");
}
else {
echo header("Location: thankyou.html");
}
}
else {
echo ("There was a problem submitting your video. Try again, or report the issue.");
}

?>

submissions.php:

<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){
echo "$index. ".$items[0]."<a href='".$items[1]."'></a>, ".$items[1]." <br />";
$index++;
}
}

?>

mlseim
09-06-2011, 01:38 AM
This line:
echo "$index. ".$items[0]."<a href='".$items[1]."'></a>, ".$items[1]." <br />";

Needs to be:
echo "$index. ".$items[0]."<a href='".$items[1]."'>".$items[1]."</a> <br />";

Revised:


<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){

// check for http://
if(strpos($items[1], "http://")){
$x="";
}
else{
$x="http://";
}

echo "$index. ".$items[0]."<a href='".$x.$items[1]."'>".$x.$items[1]."</a> <br />";
$index++;
}
}

?>





The timer part is going to be harder ... it requires you to use a COOKIE and/or SESSION to
check the last time the user submitted. A bit more coding ... I'm not sure how well you know PHP.

I'm guessing that as you keep going, you'll be adding more and more things ...
The more you know what is possible, the more you'll want. So, keep trying to add things yourself,
and if you get stuck, repost what you have that isn't working. This forum is a great place to
discuss things, but not so good to be troubleshooting scripts.



.

Feriscool
09-06-2011, 02:03 AM
This line:
echo "$index. ".$items[0]."<a href='".$items[1]."'></a>, ".$items[1]." <br />";

Needs to be:
echo "$index. ".$items[0]."<a href='".$items[1]."'>".$items[1]."</a> <br />";

Revised:


<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){

// check for http://
if(strpos($items[1], "http://")){
$x="";
}
else{
$x="http://";
}

echo "$index. ".$items[0]."<a href='".$x.$items[1]."'>".$x.$items[1]."</a> <br />";
$index++;
}
}

?>





The timer part is going to be harder ... it requires you to use a COOKIE and/or SESSION to
check the last time the user submitted. A bit more coding ... I'm not sure how well you know PHP.

I'm guessing that as you keep going, you'll be adding more and more things ...
The more you know what is possible, the more you'll want. So, keep trying to add things yourself,
and if you get stuck, repost what you have that isn't working. This forum is a great place to
discuss things, but not so good to be troubleshooting scripts.



.
Currently, what the text looks like in the output file is:


47. www.google.comhttp://Regular text.

I'm not sure what's wrong. :/

mlseim
09-06-2011, 02:41 AM
You're writing your data like this?
fwrite ($fp, $quantity . ", " . $item);

So, the text file looks like:

2,www.google.com
5,www.yahoo.com

?

That means the URL is array item [1] ... quantity is array item [0] ...

Show me some lines from your .txt file.


.

Feriscool
09-06-2011, 02:50 AM
You're writing your data like this?
fwrite ($fp, $quantity . ", " . $item);

So, the text file looks like:

2,www.google.com
5,www.yahoo.com

?

That means the URL is array item [1] ... quantity is array item [0] ...

Show me some lines from your .txt file.


.

This is how it's writing to the file:


fwrite($fp, $quantity.",".$item."\r\n");

Here are the lines from the output file (this is exactly as the file is):

1. www.google.comhttp://Descriptionof video here.
2. www.yahoo.com http://The yahoo website.
3. www.youtube.comhttp://So many videos!

The text after each http:// is a link. It's not putting the comma that's supposed to be there when it writes the text.

mlseim
09-06-2011, 03:14 AM
What does "quantity" mean?
Do you mean "line numbering"?

How about this ...

$string=$quantity.",".$item;
fwrite ($fp, $string);

See if that works better.



.

Feriscool
09-06-2011, 03:20 AM
What does "quantity" mean?
Do you mean "line numbering"?

How about this ...

$string=$quantity.",".$item;
fwrite ($fp, $string);

See if that works better.



.

$quantity is just a badly named variable. It holds the link though which is grabbed from the text field. What you gave me still doesn't work, shows like this in the output:

4. www.google.com, http:// Hi

mlseim
09-06-2011, 03:24 AM
Where does the "4." come from?

It looks to me like you have 3 items?

Can I see the form that you're using to input these?
And badly named variables suck.


.

Feriscool
09-06-2011, 03:28 AM
Where does the "4." come from?

It looks to me like you have 3 items?

Can I see the form that you're using to input these?
And badly named variables suck.


.

The form:


<form action="process.php" type="post"><br /><br />
Link to YouTube Video: <input name="youtubelink" type="text" /><br />
Description of Video: <input name="comments" type="text" /><br />
<input type="submit" />
</form>

The process.php:


<?PHP

$filename = "output.txt"; #Must CHMOD to 666
$quantity = $_POST['quantity'];
//$quantity = "<a href=\"{$_POST['quantity']}\">{$_POST['quantity']}</a>";
$item = $_POST['item'];

$fp = fopen ($filename, "a+");
if ($fp) {
fwrite($fp, $quantity.", ".$item."\r\n");
fclose ($fp);
if (empty($_POST['quantity'])) {
echo ("Go back and fill in the link!");
}
else if (empty($_POST['item'])) {
echo ("Go back and fill in the description!");
}
else {
echo header("Location: thankyou.html");
}
}
else {
echo ("There was a problem submitting your video. Try again, or report the issue.");
}

?>

The output:

<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){

// check for http://
if(strpos($items[1], "http://")){
$x="";
}
else{
$x="http://";
}

echo "$index. ".$items[0]."<a href='".$x.$items[1]."'>".$x.$items[1]."</a> <br />";
$index++;
}
}

?>

mlseim
09-06-2011, 03:32 AM
holy crap ...
no wonder why I couldn't figure it out ....



<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){

// check for http://
if(strpos($items[0], "http://")){
$x="";
}
else{
$x="http://";
}

echo "$index. <a href='".$x.$items[0]."'>".$x.$items[0]."</a> &nbsp;&nbsp; ".$items[1]."<br />";
$index++;
}
}

?>




And on the first script, change your $_POST names to match your new form variable names.



.

Feriscool
09-06-2011, 03:38 AM
holy crap ...
no wonder why I couldn't figure it out ....



<?php
// put all lines of the file into an array
$filename = "output.txt";
$rows=file($filename);

// loop through the array and separate the items by commas
$index=1;
foreach($rows as $row){
$items=explode(",", $row);
if($items[0]){

// check for http://
if(strpos($items[0], "http://")){
$x="";
}
else{
$x="http://";
}

echo "$index. <a href='".$x.$items[0]."'>".$x.$items[0]."</a> &nbsp;&nbsp; ".$items[1]."<br />";
$index++;
}
}

?>




And on the first script, change your $_POST names to match your new form variable names.



.

It's working great, but the only last problem is that if the person types in http://www.google.com in the link text field (quantity), then in the output it prints http://http://www.google.com

mlseim
09-06-2011, 04:03 PM
That means this part isn't working ...

// check for http://
if(strpos($items[0], "http://")){
$x="";
}
else{
$x="http://";
}


And I think I know why ...
It's finding "http://" at position 0.
PHP has this quirky thing where 0 means "false",
even if it's a value of a function that successfully found the string ...
so although though it found it, it's coming back as "false".

So try this instead ...

// check for http://
if(strpos($items[0], "ttp://")){
$x="";
}
else{
$x="http://";
}


Let's look for only "ttp://" ...
that would be at position 1.

See if that works.


.

Feriscool
09-06-2011, 04:09 PM
That means this part isn't working ...

// check for http://
if(strpos($items[0], "http://")){
$x="";
}
else{
$x="http://";
}


And I think I know why ...
It's finding "http://" at position 0.
PHP has this quirky thing where 0 means "false",
even if it's a value of a function that successfully found the string ...
so although though it found it, it's coming back as "false".

So try this instead ...

// check for http://
if(strpos($items[0], "ttp://")){
$x="";
}
else{
$x="http://";
}


Let's look for only "ttp://" ...
that would be at position 1.

See if that works.


.

It works! You sir are a genius! Thanks for helping so much, I definitely learned much more than I expected from this community.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum