Adding to an .inc File Through a Web Form

Discussion in 'Web Design and Development' started by 20rogersc, Dec 5, 2007.

  1. 20rogersc macrumors 65816


    Jun 28, 2005
    Brighton, UK
    How can this be done!?
    I have downloaded the Cj Random Quote php script, which takes a line randomly from a .inc file.

    What I want to do is allow users to add their own 'quotes' through a web form. However I have no experience in this field (I know CSS and html fairly well, but no server/forms knowledge), and would like to know the easiest way to perform this task, or be directed to a tutorial (as I can't find one personally). Or if there are alternatives, I would be interested.

  2. Super Macho Man macrumors 6502a

    Super Macho Man

    Jul 24, 2006
    Hollywood, CA
    It's hard to give a direct answer without knowing how that quote script works etc. I would recommend opening up the text file in which the quotes are stored and taking a look at the structure. Your goal is to get user input from an HTML form and append it to that file using PHP - manipulating it into the necessary structure beforehand. Take a look at the PHP file functions - fopen(), fwrite(), etc.

    Be careful to sanitize user input. Use functions like substr() to limit length to X characters, and perhaps limit the frequency at which a user can submit a quote using PHP sessions.

    This isn't exactly what you're looking for, but take a look at the PHP page in this example. Try to understand how they do it. It's basically the same idea, except in your case you're capturing text input from an <input type="text"> field rather than an <input type="radio"> field. Ignore the JavaScript.
  3. 20rogersc thread starter macrumors 65816


    Jun 28, 2005
    Brighton, UK
    Thanks very much for the helpful reply.
    I'll give it a go...
  4. SrWebDeveloper macrumors 68000


    Dec 7, 2007
    Alexandria, VA, USA
    I'm going to expand a little on the excellent advice already given, to summarize it all for you:

    It sounds like a flat file (a simple text file, the .inc file mentioned) stores quotes and is loaded into memory, one line (quote) is pulled at random and included in the output. This is how standard taglines work.

    Of course to add quotes to that .inc file involves:

    1) knowing how the .inc file is structured (are there special characters or separators to distinguish one quote from another, or simply a newlines?)
    2) the full path must be known and permissions for read/write set properly

    So if you need some coding help, we need to know the answer to each unknown listed above and that you can do #2 on your own.

    In a nutshell it all works like this:

    A form on your page is filled in and submitted by the user. The form calls a PHP script on your server that processes the user input (a quote of some sort), locks then opens the .inc file, appends the user's quote on the end (or beginning), unlocks and writes.

    Please supply what you can, or hire a pro (not me, sorry, too busy) if the basic concepts I explained are not within your experience level. If you do, consider asking the pro if using a database method is more efficient in the long run. Using flat files to randomly pick out content is not efficient once the file gets large depending on your site activity. Not to mention wouldn't it be prudent to permit users (or an admin) to remove their submitted quotes as well?


Share This Page