PHP Form Processing help?
-
I have a form that I want to use with my website, but I don't know what I need to write in the separate file for the form's "action". I know it has to be PHP, if somebody out there can help, could you please give me a code? I've looked up this stuff on the PHP website and I still don't really understand it. Basically, I want it to put all of the information into a text file and upload it to a directory to my site, the file would have to have a random name in case two files are the same, the text file would look like this: Name: Example Song: Example Band: Example Album: Example Type: Tab or Lyrics Body: Body of whatever was submitted This is the form I have.. <form action="form.php"> <!-- there is nothing in form.php right now, what do I put in it? //-> Name: <input type="text" name="nameby" /> Song: <input type="text" name="song" /> Band: <input type="text" name="band" /> Album: <input type="text" name="album" /> Type: <select name="type"> <option value="Lyrics">Lyrics</option> <option value="Tab">Tab</option> </select> Body: <textarea rows="15" cols="60" name="body">Tab Or Lyrics Here</textarea> <input type="submit" value="Submit" /> </form> How can I make this work?
-
Answer:
Try this. The way it operates is by including the processing code on the same page as the form, so the form sends it's data to itself. I made two changes to the form itself, the rest of the code goes at the top of the file before you write your html. Remember to save this as a PHP file, not HTML as well. I tried to comment as much as possible, you can email if you need any clarification or it doesn't work (I did test it!). Alternatively you could use a second file, change the action attribute of the <form> tag, dump all the PHP above <head> into a new file, and change the header() call to point back to the file with the form in it. <?php // THIS SECTION PROCESSES DATA SENT BY USER if (isset($_POST['submit'])) // check for submitted form { // here's the data from your form $name = $_POST['nameby']; $song = $_POST['song']; $band = $_POST['band']; $album = $_POST['album']; $type = $_POST['type']; $body = $_POST['body']; // put the path to wherever you want to store your files here $folder = 'myfolder/'; // creates a file name $filename = $band . ' - ' . $song . ' - ' . $type . '.txt'; // full path to file $filepath = $folder . $filename; $version = 1; // check if filename exists, if so append a version no. /* * This code should create file name like: * Soundgarden - Outshined - Tab.txt * If someone submits a second version we'd get * Soundgarden - Outshined - Tab[1].txt and so on */ while (file_exists($filepath)) { $filename = $band . ' - ' . $song . '-' . $type . '[' . $version . '].txt'; $filepath = $folder.$filename; $version++; } // create file for writing $output = fopen($filepath, "w") or die("Cannot open file."); // Write to the file here fwrite($output, "Band: " . $band . "\r\n"); fwrite($output, "Song: " . $song . "\r\n"); fwrite($output, "Album: " . $album . "\r\n"); fwrite($output, "Submitted By: ". $name . "\r\n\r\n"); fwrite($output, $body); // close file fclose($output); // immediately redirect to ourselves // this prevents the data being resent if user clicks refresh header('Location: http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); } ?> <!-- The rest of your form page now goes here --> <head> </head> <body> <!-- note the change here! we are now sending the form data back to this same page --> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> Name: <input type="text" name="nameby" /> Song: <input type="text" name="song" /> Band: <input type="text" name="band" /> Album: <input type="text" name="album" /> Type: <select name="type"> <option value="Lyrics">Lyrics</option> <option value="Tab">Tab</option> </select> Body: <textarea rows="15" cols="60" name="body">Tab Or Lyrics Here</textarea> <!-- another change here --> <input type="submit" value="Submit" name="submit" /> </form> </body> </html>
JJ at Yahoo! Answers Visit the source
Related Q & A:
- Which rebate processing jobs are real?Best solution by workathomecareers.com
- What is data processing?Best solution by Stack Overflow
- How can I remove a picture's watermark using Matlab's image processing toolbox?Best solution by Yahoo! Answers
- What is an easy code for a form in PHP?Best solution by Stack Overflow
- Can I pick up my USPS package from the processing facility before its out for delivery?Best solution by about.usps.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.