Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Exclamation Help with PHP Forms

    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:
    Code:
    <?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:
    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?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 Code:
    <?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 Code:
    <?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.



    .
    Last edited by mlseim; 09-05-2011 at 07:49 PM.

  • Users who have thanked mlseim for this post:

    Feriscool (09-06-2011)

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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)?
    Last edited by Feriscool; 09-05-2011 at 11:14 PM.

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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 Code:
    <?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 Code:
    <?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:
    Code:
    <?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:
    Code:
    <?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++;
       }
    }
    
    ?>

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 Code:
    <?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.



    .
    Last edited by mlseim; 09-06-2011 at 01:43 AM.

  • Users who have thanked mlseim for this post:

    Feriscool (09-06-2011)

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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 Code:
    <?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. :/
    Last edited by Feriscool; 09-06-2011 at 02:40 AM.

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.


    .

  • #8
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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:

    Code:
    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.
    Last edited by Feriscool; 09-06-2011 at 02:53 AM.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    What does "quantity" mean?
    Do you mean "line numbering"?

    How about this ...

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

    See if that works better.



    .

  • #10
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.


    .

  • #12
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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:

    Code:
    		<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:

    Code:
    <?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:
    Code:
    <?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++;
       }
    }
    
    ?>

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    holy crap ...
    no wonder why I couldn't figure it out ....

    PHP Code:
    <?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.



    .

  • Users who have thanked mlseim for this post:

    Feriscool (09-06-2011)

  • #14
    New Coder
    Join Date
    Sep 2011
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    holy crap ...
    no wonder why I couldn't figure it out ....

    PHP Code:
    <?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

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.


    .

  • Users who have thanked mlseim for this post:

    Feriscool (09-06-2011)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •