Web Analytics Made Easy -
StatCounter Is there a way to allow guests to add lines to a .txt or .js file on my site? - CodingForum

Announcement

Collapse
No announcement yet.

Is there a way to allow guests to add lines to a .txt or .js file on my site?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there a way to allow guests to add lines to a .txt or .js file on my site?

    Is there a way to allow guests to add lines to a .txt or .js file on my site- Like I have a random quote script running and it uses a .txt or .js file that looks like this:

    var quotes=new Array()

    quotes[0]='There are some people who live in a dream world, and there are some who face reality; and then there are those who turn one into the other. <i>-By Douglas Everett</i>'

    quotes[1]='Whether you think you can or whether you think you can\'t, you\'re right! <i>-Henry Ford</i>'
    ...
    quotes[30]='To hell with circumstances; I create opportunities. <i>-Bruce Lee</i>'

    function displayQuote(){
    var whichquote=Math.floor(Math.random()*(quotes.length))
    document.getElementById("quote").innerHTML = quotes[whichquote];
    setTimeout("displayQuote()", 3000); //2.6 secs interval
    }


    How can I allow guests to add quotes to the file via a form?
    http://www.freewebs.com/pozzuolana/ran.htm - page
    http://www.freewebs.com/pozzuolana/ran.txt - txt file

    Thanks- coacl

  • #2
    there are two methods - count number of times quotes[*] appears, and add one, insert line, and two, read all in, parse last instance of value between the brackets in 'quotes[]' before function, add one, append line...

    i'lll whip something up soon.

    Comment


    • #3
      Thanks Celtboy, and please whip something up- I could use an example.

      Comment


      • #4
        provided you don't change the ran.txt file any, this should get you started:

        PHP Code:
        <?php

        /* Display the Form */
        if (!isset($_POST["go"])) {
           print 
        "<html>\n\n";
           print 
        "<body>\n";
           print 
        "   <form action=\"\" method=\"post\">\n";
           print 
        "      <input type=\"hidden\" name=\"go\" value=\"1\" />\n";
           print 
        "      Quote Text <input type=\"text\" name=\"quote\" /><br/>\n";
           print 
        "      Quote Author <input type=\"text\" name=\"author\" /><br/>\n";
           print 
        "      <input type=\"submit\" value=\"Add Quote\">\n";
           print 
        "   </form>\n\n";
           print 
        "   </body>\n";
           print 
        "</html>";
           exit;
        }


        $quote_file "ran.txt";
        $js_function_name "function displayQuote()";
        $fp_array explode($js_function_name,file_get_contents($quote_file));

        $content explode("quotes[",$fp_array[0]);

        /* this should all be one line */
        $new_quote "quotes[" . (sizeof($content) - 1) . "] = " "'" $_POST["quote"] . " <i>-" $_POST["author"] . "</i>" "'\r\n\r\n";

        /* Actually write the new quote to the file */
        $fp fopen($quote_file,"w+");
           
        fputs($fp$fp_array[0]);
           
        fputs($fp$new_quote);
           
        fputs($fp$js_function_name);
           
        fputs($fp$fp_array[1]);
        fclose($fp);

        print 
        "You sucessfully added the following quote: " $_POST["quote"] . " <i>-" $_POST["author"] . "</i>";

        ?>
        let me know if you don't understand the code.

        HTH,
        -Celt
        Last edited by Celtboy; Mar 2, 2004, 09:55 PM.

        Comment


        • #5
          I think I understand but I'm having some problems with server restrictions, just to be sure-

          I add the exact php code you gave to a .php file with nothing but the code, then add this form to my random page :

          <form action= "http://www.freewebs.com/pozzuolana/ran.php" method= "post">
          <input type="hidden" name="go" value="1"/><br />
          <input type="text" name="quote" value="Quote"/><br />
          <input type="text" name="author" value="Author"/><br />
          <input type="submit" value="Add Quote" />
          </form>


          That should work right? Would it matter if I changed the server?
          Because the method post is not allowed on Freewebs, so I tried using




          But got this error:

          Fatal error: Call to undefined function: file_get_contents() in /www/palbum/html/upload/ran.php on line 24


          Is my form wrong or is it just the servers?

          Thanks- coacl

          Edit: I got hosting on http://pulp.fragism.com/ran.htm but it still doesn't seem to work; any ideas on what's wrong?
          Last edited by coacl; Mar 3, 2004, 11:01 PM.

          Comment


          • #6
            The first server's version of php is too old. the function file_get_contents requires version 4.3.0 or newer.

            The problem with server 2 is file permissions.

            To work around the server 1 problem, try this:
            PHP Code:
            $quote_file "ran.txt";
            $js_function_name "function displayQuote()";

            /* Added this part because of older PHP Version */
            $file_contents "";
            $file_contents_array file($quote_file);
            foreach (
            $file_contents_array as $line) {
               
            $file_contents .= $line;
            }

            /* Again, I altered this for the older PHP Version */
            //$fp_array =  explode( $js_function_name, file_get_contents(  $quote_file ) );
            $fp_array =  explode($js_function_name,$file_contents);


            $content explode("quotes[",$fp_array[0]);

            /* this should all be one line */
            $new_quote "quotes[" . (sizeof($content) - 1) . "] = " "'" $_POST["quote"] . " <i>-" $_POST["author"] . "</i>" "'\r\n\r\n";

            /* Actually write the new quote to the file */
            $fp fopen($quote_file,"w+");
               
            fputs($fp$fp_array[0]);
               
            fputs($fp$new_quote);
               
            fputs($fp$js_function_name);
               
            fputs($fp$fp_array[1]);
            fclose($fp);

            print 
            "You sucessfully added the following quote: " $_POST["quote"] . " <i>-" $_POST["author"] . "</i>";

            ?> 
            HTH,
            -Celt

            Comment


            • #7
              pulp.fragism.com/ran.htm

              Permission still seems to be denied with the php:

              <?php


              $quote_file = "ran.txt";
              $js_function_name = "function displayQuote()";


              $file_contents = "";
              $file_contents_array = file($quote_file);
              foreach ($file_contents_array as $line) {
              $file_contents .= $line;
              }

              $fp_array = explode($js_function_name,$file_contents);

              $content = explode("quotes[",$fp_array[0]);

              $new_quote = "quotes[" . (sizeof($content) - 1) . "] = " . "'" . $_POST["quote"] . " <i>-" . $_POST["author"] . "</i>" . "'\r\n\r\n";

              $fp = fopen($quote_file,"w+");
              fputs($fp, $fp_array[0]);
              fputs($fp, $new_quote);
              fputs($fp, $js_function_name);
              fputs($fp, $fp_array[1]);
              fclose($fp);

              print "You sucessfully added the following quote: " . $_POST["quote"] . " <i>-" . $_POST["author"] . "</i>";

              ?>


              I really appreciate all the help, I just hope I can get this working.
              Thanks again.

              Edit: You can try the server here, if you have the time-
              ftp://ftp.fragism.com/ user: pozz password: random
              Last edited by coacl; Mar 4, 2004, 12:13 AM.

              Comment


              • #8
                I think I have to CHmod it to 777, it should work then.

                Thanks- coacl
                Last edited by coacl; Mar 6, 2004, 04:59 PM.

                Comment


                • #9
                  Hi coacl, just to let you know in the future, if you have some PHP code that you would like coloured, you can use:

                  [php]
                  <?php
                  code ...
                  ?>
                  [/php]

                  which would come out as

                  PHP Code:
                  <?php
                  code 
                  ...
                  ?>
                  This is generally the preffered way to show your PHP code
                  PHP Weekly - A PHP Developers Resource
                  PHP 5.1.4 and Ruby on Rails web hosting
                  Moderator of PHP and Work offers and Requests
                  Install Apache/PHP/MySQL
                  (by marek_mar) | TinyPlugin Architecture

                  Comment


                  • #10
                    Yeah, I was wondering how to use that-
                    Thanks for everything and feel free to add a quote

                    PHP Code:
                    http://pulp.fragism.com/ran.htm 
                    : )

                    Comment


                    • #11
                      Heh, I added a quote, one that a mate of mine made up.

                      Can I just suggest on a non PHP note that you make the change between quotes slightly longer, as I am having a job getting to the end of the line before it changes.
                      PHP Weekly - A PHP Developers Resource
                      PHP 5.1.4 and Ruby on Rails web hosting
                      Moderator of PHP and Work offers and Requests
                      Install Apache/PHP/MySQL
                      (by marek_mar) | TinyPlugin Architecture

                      Comment

                      Working...
                      X