PHP Form Processing (again)

CannonBallGuyCannonBallGuy ModeratorShared Hoster
Okay.. Imagine someone fills out a form and this info is sent to a process page:
$name
$email1
$email2
$phone

Then all that info is displayed, if it is incorrect then they go back and do it again. If it is correct they click confirm and all the info is written to 2 separate .txt files and sent in an email.

I could guess at how to do this but it would take my weeks to se-bug it, so if anyone can help, I would be extremely gratefull. :)

Comments

  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Hey CBG, here goes:

    1. Cut all of the code below and save it into a file called form.php. If you want to use another file name, make sure you change the "action" value in the html form.
    2. Create a file named details.txt and put it in the same location as the form.php file. Again, if you want to use a different file name and path, make sure you change the first parameter (new name + full path) in the fopen command.
    3. If you want to activate email sending, please uncomment the email command first
    4. What it does is a. allow the user to enter name and email info, b. send an email (if the email comman is uncommented) to the admin and c. save the info to a file.
    5. Load the form.php file and the details.txt file to your server and call it using a browser.
    6. Let me know how it goes (it may be a crude little piece of code but I've finally got my butt to install Apache, MySQL and PHP in my laptop - so I've actually tested the script and it works :))

    <html>
    <head>
       <title>Test Form</title>
    </head>
    
    <body>
    
    <?php
    $form = "
    <form action=\"form.php\" method=\"post\">
    <input type=\"hidden\" name=\"seen\" value=\"y\">
    <b>Enter Details:</b><br>
    Name: <input type=\"text\" name=\"name\" size=\"20\" maxlength=\"20\" value=\"\"><br>
    Email: <input type=\"text\" name=\"email\" size=\"20\" maxlength=\"50\" value=\"\"><br>
    <input type=\"submit\" value=\"Submit\">
    </form>";
    
    if($seen != "y") {
       print "$form";
    }
    else {
       // cannot continue as user did not enter name information
       if($name==""){
          print "Name field cannot be blank!\n";
          print "$form";
       }
       // cannot continue as user did not enter email information
       elseif($email==""){
          print "Email field cannot be blank!\n";
          print "$form";
       }
       // everything OK - so start processing
       else{
          // write to file
          $file=fopen("details.txt", "a");
          $row=$name.", ".$email."\r\n";
          fwrite($file, $row);
          fclose($file);
          // send email
          $to="you@yourdomain.com";
          $subject="you@yourdomain.com";
          $header="you@yourdomain.com";
          //mail($to, $subject, $row, $header); //uncomment this line to activate email sending :)
          // inform user
          print "Your details have been saved";
       }
    }
    ?>
    
    </body>
    </html>
    
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Thanks for that, but I need the details to be written to TWO separate .txt files one depending on a hidden variable in the form.

    This is the form:
    <html>
    <head>
    <title>Form...</title>
    </head>
    <body>
    <form name="input" action="process.php" method="post" style="text-align:center;">
    Personal Info:<br />
    First Name: <input type="text" name="firstname"><br />
    Last Name: <input type="text" name="lastname"><br />
    eMail: <input type="text" name="email1"><br />
    eMail again (to confirm): <input type="text" name="email2"><br />
    Daytime Phone Number: <input type="text" name="dayphone"><br />
    Evening Phone Number: <input type="text" name="nightphone"><br />
    <br />
    Address:<br />
    House Name/Number: <input type="text" name="house"><br />
    Street/Road: <input type="text" name="road"><br />
    Village/Estate: <input type="text" name="village"><br />
    Town/City: <input type="text" name="town"><br />
    County: <input type="text" name="county"><br />
    Country: <input type="text" name="country"><br />
    Postcode: <input type="text" name="postcode"><br />
    <br />
    How will you Pay for this game?<br />
    PayPal: <input type="radio" name="payment" value="paypal"><br />
    Bank Transfer: <input type="radio" name="payment" value="transfer"><br />
    Cheque: <input type="radio" name="payment" value="cheque"><br />
    Cash: <input type="radio" name="payment" value="cash"><br />
    <br />
    <span style="font-size:14pt;font-weight:bold;">Terms and Conditions</span><br />
    <textarea rows="7" cols="40">Copyright Infoformation: Nobody is gonna screw us over because we have such cool copyright infor etc... Guess what. we rule!! WOooo!! I like candy because it shines like gold. I like gold because its shiny.Did you see "The Apprentice: Big Finale" It was so cool! I really cannot wait for "Season 2".... yehaw!!!!!! Copyright Info: Nobody will screw us over cuz we have such cool copyright infor etc... Guess what. we rule!! WOooo!! I like fooooood because it shines like gold. I like gold because its shiny.Did you see "The Apprentice: Big Finale" It was so cool! I really cannot wait for "Season 2"! Copyright Infoformation: Nobody is gonna screw us over because we have such cool copyright infor etc... Guess what. We rule!!!!!!!! WOooo!!!!!!!!!!!! I like candy because it shines like gold. I like gold because its really really shiny. Did you see "The Apprentice: Big Finale" It was so cool! I really cannot wait for "Season 2".... yehaw!!!!!!!!!!!!!!!!!!!!!</textarea><br />
    I agree to these Terms and Conditions: <input type="radio" name="terms" value="agree"><br />
    I do NOT to these Terms and Conditions: <input type="radio" name="terms" value="disagree"><br />
    
    <input type ="submit" value ="Submit!"><br />
    <input type ="reset" value ="Reset!">
    
    </form>
    </body>
    </html>
    

    But imagine, in order to get to the page with that form on, they clicked a link that was something like www.website.com/form.php?ProductName ...
    Then, after the details are enterd and submitted, if there is an error they go back, if not the see a page with all their details on, if the think they are correct they click Submit again and then the details are written to a file named general.txt and a file named ProductName.txt
    And all the details are sent in an email to one specific email address.

    I know thats complicated and I understand it may not be possible... but please, any help is greatly appreciated.

    Thanks in advance!
    -CBG
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    I won't attempt it until I understand what you want fully. If you confirm the following is what you want, I can get it coded:

    1. User get to form by 2 ways - either:
    a. www.website.com/form.php?ProductName or
    b. www.website.com/form.php

    2. If user got to the website through (a), then the information needs to be saved to 2 files (general.txt and productname.txt). If the user to the site through (b), then the information only needs to be saved to 1 file (general.txt)

    3. When the user clicks submit, some checking neds to be performed - if there errors, show them the form again. If there are no errors, show them a confirmation page with all their information.

    4. At the confirmation page, if they click on OK, the information gets saved, an email gets sent and a Thank You page is displayed.

    Correct?
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    That is exactly right EXCEPT for point 1. The user would only ever get to www.website.com/form.php if the typed that in the address bar themselves.

    What I would need is in the error checks, to check that the ProductName field (hidden if possible) is not empty. If it is empty then its an error and it says "You Did Not Pick a Product!!!" and sends them back TWO pages to the page where they clicked the link that takes them to the form....


    Ok???
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Got it!

    I've got a wedding dinner to attend in an hour's time. Hopefully I'd still be sober when I get home. Otherwise, I'll get it coded up tomorrow for you.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay. Thanks very much.... ForumPoints in the post ;)
    And I'll put a link on the site too ;)
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    OK, hopefully this covers what you need:
    1. I am attaching a zip
    2. The zip contains 4 files - 2 PHP scripts and 2 empty text files where the user's information will be saved. If you rename the files, you will have to change the scripts to reflect the new names (which should be easy)
    3. Upload all the 4 files into your server - put them together in the same location. If location needs to be different, you will have to modify the paths to the files in the scripts (which again, should be easy)
    4. Test it out viewing form.php through a browser

    Logic:
    1. The user either gets to form.php by keying in the url in the browser or through a generated link (e.g. form.php?productname=abc)
    2. The user keys in his information
    3. The script does some checking - if there are problems, inform the user about the problems and force the user to rectify. I have only put in checking for a few fields - if you need more checking, just copy the existing checking to cover more fields
    4. If there are no errors, show the details to the user for confirmation
    5. When the user confirms, write the information to a file named repo_general.txt
    6. If the user came in through a generated link, write the information to an additional file called repo_product.txt

    Good luck - tell me how it goes...
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Umm...

    If the user came in through the link website.com/form.php then that is an error becasue the ProductName field is empty.
    And there will be lots of generated links such as:
    form.php?productname=item1
    form.php?productname=something2
    form.php?productname=no3
    form.php?productname=thisis4
    So, if a user uses the form at form.php?productname=no3
    then the info is saved to a general.txt file AND a file called no3.txt
    So at some point it needs to check if that file exists. If not, create it and then add the line of info....

    Sorry... Maybe i didnt explain it well enough before :(
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    No prob. Clarification:

    1. If $productname is empty, don't proceed - display error message
    2. If $productname is not empty, save in 2 files - general.txt and a file named after the product. The script needs to check if the file exists (i.e. if it's an existing product or a new product) - if does not exist, create a new file and save into the new file. If exists, save to the existing file

    3. Will there be a situation in which data gets saved to general.txt only and not to a product file?
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    3. No. Everythng will always be saved to general.txt AND a file named after the product.

    Also, could you make it so that its not called "product name"... For example, The links would be site.com/form.php?p=NAME
    so the field is called "p" ...?

    Thanks Ever So Much.
    I will be linking from this site to yours and when I get more forum points, ill send some more to you too! :)
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    I'll get something up soon...

    Hey, don't worry about the "links" and the "forum points"... I'm doing it for fun.
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Done!

    1. Zip file contains only 2 files - form.php and process.php. As usual, if you change the names of these files, you need to change the references to these files in the scripts too (very simple to do).
    2. Upload both files to the same location in your server. If you need to put them in different directories, you will need to change the paths in the scripts.
    3. Test out by calling form.php - http://www.yoursite.com/form.php?p=whatever and http://www.yoursite.com/form.php
    4. The logic you requested will be performed
    5. The script will write data into general.txt (if not found, the script will create one)
    6. The script will also write data into <whatever>.txt (also, if not found, the script will create one). <whatever> will correspond to the parameter pass in through http://www.yoursite.com/form.php?p=whatever
    7. The data files will be automatically created in the same directory as the scripts (i.e. form.php and process.php). If you need to keep the data files in a different directory, modify the path variable in the process.php file.

    Hope it goes well. Appreciate if you could tell me the results.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay... All seems to work PERFECTLY up untill I confirm the details and click the final submit button...

    I got these errors:
    Warning: fopen(./general.txt): failed to open stream: Permission denied in /home/matsim68/public_html/PS2mall/process.php on line 12
    
    Warning: fwrite(): supplied argument is not a valid stream resource in /home/matsim68/public_html/PS2mall/process.php on line 13
    
    Warning: fclose(): supplied argument is not a valid stream resource in /home/matsim68/public_html/PS2mall/process.php on line 14
    
    Warning: fopen(./100.txt): failed to open stream: Permission denied in /home/matsim68/public_html/PS2mall/process.php on line 18
    
    Warning: fwrite(): supplied argument is not a valid stream resource in /home/matsim68/public_html/PS2mall/process.php on line 19
    
    Warning: fclose(): supplied argument is not a valid stream resource in /home/matsim68/public_html/PS2mall/process.php on line 20
    Your details have been saved.
    
    I suspect I just need to chnage some CHMODS... But what files do I need to change the CHMOD of and what too?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    Hmm.... I'm stumped actually, it works on my laptop (which has apache, php and mysql).

    You only need to worry about lines 12 and 18 - the other errors are caused by 12 and 18. Basically lines 12 and 18 are trying to create files to store the information. Creation is done using the fopen command - and both failed. Do you need to specify any settings in your host to allow the PHP scripts to create files?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    I'll try to do some research on this...

    In the worst case, if we cannot get this fixed, you could manually create the files and upload it to the server...
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    daboss wrote:
    Hmm.... I'm stumped actually, it works on my laptop (which has apache, php and mysql).

    You only need to worry about lines 12 and 18 - the other errors are caused by 12 and 18. Basically lines 12 and 18 are trying to create files to store the information. Creation is done using the fopen command - and both failed. Do you need to specify any settings in your host to allow the PHP scripts to create files?

    I dont believe there are any special settings or anything...
    I'm sure I have fopen commands in a link directory.
    I will check.

    Edit:
    I have this code in a link directory process page:
    <?php
    $filename=$category;
    $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);
    ?>
    
    If thats the same commands as this form process then at least we know it should work.

    Are you sure its not just the CHMOD...
    I can't help thinking thats what it means when it says "Permission denied"...
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Daboss!!! Stop researching!!!
    I spoke to someone else who knows a little PHP and they said to just try to change the Chmod anyway...
    So I set the folder to 777 and it all works!
    Thanks ever so much daboss!
    Your a star!

    And I will be putting a link on the site to yours because I never could ahve done this without your help and with any luck it's gonna be a big earner for me ;)

    Let me know your Url and link text! :)
  • ForgeForge Senior Member The Royal RAM
    I know you guys worked it out, But why not just have mySQL ? It would be much easier to use.
    Can fat people Go skinny Dipping?
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    CBG, you still got to put in some work on the cosmetics - I was too lazy to make it look good (also I don't know what font/colors your site is using).

    If you want it in a database as Forge suggested, I only have to change a couple of lines in process.php - 10 minutes work. Give me a shout if you decide to use a database :)

    Anyway, good luck.

    If you do want to link to my site (:)), the details are as per my signature.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    If I want to use a database, I have to set it up.
    I ahve never set up or run a MySQL database. And, to be frank, I can't be arsed to work it all out.

    As the old saying goes: Don't fix it if it ain't broke.
    And text files have worked fine for me before... :)

    As for the cosmetics, I know...
    But I don't want it to look particulalry fancy. I will probably just put everything pretty much as it is into the layout. :)


    Thanks Ever So Much, once again, daboss. ;)
Sign In or Register to comment.