More form processing...

CannonBallGuyCannonBallGuy ModeratorShared Hoster
I want to make a basic link directory taht fits very easily into my current design (www.kaziaz.com).

What I want is this:

Someone fills out a form Putting in this:

URL (text field)
Their Name (text field)
Description (text area)
Category (drop-down menu)

If for example they put this in:
URL = www.google.com
Their Name = Bob
Description = A cool SE.
Category = General

Then In A File called "general.txt" a new line is generated which contains the Url their name and the description.

Then back on the directory there would be a link to the General Category and in there would be a list of the links in the general.txt file with the submitters name and Descrition beneath...

Is that possible?
Or am I gonna have to use a database?

Comments

  • PythonPython Forum Leader The Royal RAM
    no it is possible.

    if your going to write to a text file you will need to take a look at the fopen and fwrite functions

    The Royal Ram

  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Hmmm... If someone knows of a tutorial that does something very similar to what I want can you give me an url please?
    Or maybe you just want to code it for me? ;)
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    You'll be very very much better off using a database table for this.

    A text file will not be able to support you site if your users increase. If you have 1 text file per category, you'll have a big headache maintaining so many text files. If you have 1 text file for all the categories, you'll have a very inefficient and complex script. The 1 text file design will also not be able to cater for multi-access.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Firstly, I only intend to ahve say 5/6 categories and secondly, I do not like databases. Using Access just as it is I get along fine. It's easy...
    But using MySQL on my server? No chance! lol

    Anyway... I tried to have a go myself and this is what I did:

    I made an "Add" page which looked like this:
    <html>
    <head>
    <title>Test jtgtnjtngjtn</title>
    </head>
    <body>
    <form  action="phptest1.php" method="post">
    
    <input type="text" name="cat" value="1.txt" size="50" />
    <br />
    <input type="text" name="name" value="Bobby" size="50" />
    <br />
    <input type="text" name="url" value="www.url.com" size="50" />
    <br />
    <input type="text" name="site" value="URL .com" size="50" />
    <br />
    <input type="text" name="desc" value="This is the description of the site... Its really good! Go there now!!!!" size="200" />
    <br />
    <input type="submit" value="Submit" />
    
    </form>
    </body>
    </html>
    

    I made a php page to process the info in the form:
    <?php
    $filename=$cat
    $file=fopen($filename,"a");
    fwrite($file, "<table border="1px" cellpadding="2px" cellspacing="0px" width="400px" style="text-align:left;position:relative;margin:0px auto;width:auto;height:auto"><tr style="background-color:#DDEEFF;"><td width="200">Submitted by $name: </td><td width="200"><a href="$url" target="_blank">$site</a></td></tr><tr><td colspan="2">$desc</td></tr></table><br /> \n");
    fclose($file);
    ?>
    

    I also made a file called 1.txt which is where the stuff whould be written to.

    The error I get is:
    Parse error: parse error, unexpected T_STRING in /home/matsim68/public_html/tests/phptest1.php on line 2
    

    Can anyone tell me what I ahve done wrong... Or if its quicker, maybe you could tell me what I did right? lol.

    Thanks in advance!
  • ForgeForge Senior Member The Royal RAM
    For this, make sure you have all of the .txt files for the categorys.

    have the input fields you have, and just have a pretty basic php script to right to the .txt
    <form  action="phptest1.php" method="post">
    
    Site Category:<br>
    <select name="cat">
      <option value="general.txt">General</option>
    </select>
    <br />
    <input type="text" name="name" value="Bobby" size="50" />
    <br />
    <input type="text" name="url" value="www.url.com" size="50" />
    <br />
    <input type="text" name="site" value="URL .com" size="50" />
    <br />
    <input type="text" name="desc" value="This is the description of the site... Its really good! Go there now!!!!" size="200" />
    <br />
    <input type="submit" value="Submit" />
    
    </form>
    

    This is going to be the HTML form to gather the info. Now we need a pretty basic PHP script to gather this information...
    <?php
    
    $file = $cat;
    
    $fp = fopen($file, "a");
    
    if($fp){
    fwrite($fp, "<tr><td>Submitted by $name: </td><td width="200"><a href="$url" target="_blank">$site</a></td></tr><tr><td colspan="2">$desc</td></tr></table><br /> \n");
    fclose($file);
    }
    else {
    echo "Could not open the file named \"$cat\" Please try again";
    }
    
    ?>
    

    This opens the file, and if it cant, use the if and else statements, if it can, write to the file, and then close it. You can edit this, just dont change much to it...
    Can fat people Go skinny Dipping?
  • cptnwinkycptnwinky Junior Member Shared Hoster
    heres my stab at it...
    <form name="form1" method="POST" action="test.php">
      <select name="category">
        <option value="General" selected>General</option>
      </select>
      <br>
      Name:
      <input type="text" name="name" size="50" />
      <br />
      URL: 
      <input type="text" name="url" size="50" />
      <br />
      Site: 
      <input type="text" name="site" size="50" />
      <br>
      Site Description: 
      <br>
      <textarea name="desc" cols="46" wrap="VIRTUAL" id="desc"></textarea>
      <br>
      <input type="submit" name="Submit" value="Submit">
    </form>
    
    [php]
    $file = 'links.txt';
    $cat = $_POST;
    $name = $_POST;
    $url = $_POST;
    $site = $_POST;
    $desc = $_POST;

    if($_POST) {
    if(!file_exists($file) || !is_writable($file)) {
    echo 'Make sure the file '.$file.' is writable by the server and does exist.';
    }else{
    $string = "$cat\t";
    $string .= "$name\t";
    $string .= "$url\t";
    $string .= "$site\t";
    $string .= "$desc\n";

    $fp = fopen($file,'a');
    fputs($fp,$string);
    fclose($fp);
    echo 'done...';
    echo $string.' Written succesfully.';
    }
    }
    [/php]

    Now thats a rather simplistic script. In order to handle more than one person accessing the file at one time look into the flock function.
    PM me for small freelance php jobs.
    I have never been too needy but I lost my job due to the company shutting down our location. I just had a child and live in a pretty depressed area at the moment. I need some freelance work fairly bad.
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    CBG, I'd suggest again that you change the approach. Files are not meant for purposes like this.
  • cptnwinkycptnwinky Junior Member Shared Hoster
    Actually, in web scripting this is exactly what files are for. When you only have a few users using something like this then using a db like mysql just makes no sense, especially since he does not already have it running on his server. With only a few users the filesystem will be faster and take less space on the server.
    PM me for small freelance php jobs.
    I have never been too needy but I lost my job due to the company shutting down our location. I just had a child and live in a pretty depressed area at the moment. I need some freelance work fairly bad.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Thanks soo much for the help guys, but what's wrong with the script I have?
  • cptnwinkycptnwinky Junior Member Shared Hoster
    Well first the error you got was because you forgot a ; on line 1. Second you should always code your scripts as though register globals is off. Soon it will be deprecated completely and you will be forced to change. By this I mean that you should not use $cat instead use $_POST. Third, I wrote the string to the file in a tab delimeted format to ease the use of information later on. This way when you want to display results from the file you can read each line into an array using the explode() function. Or you can even import it into excel if you wanted to.
    PM me for small freelance php jobs.
    I have never been too needy but I lost my job due to the company shutting down our location. I just had a child and live in a pretty depressed area at the moment. I need some freelance work fairly bad.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay... I can see that I need to add an ; and I kinda get that I need to use $_POST['cat' not $cat... but the rest just flew way over.

    You changed the line that is written to the txt file by putting tabs in so that If I want, I can display each bit of info seperately by using a fuction called explode?
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Sorry.... I guess you all missed this...
    Can anyone explain the tabs and explode () thing?
Sign In or Register to comment.