schris403 Posted December 12, 2007 Share Posted December 12, 2007 I'm trying to create a web server file based on multiple submitted fields on a html form page and can't figure out how to post multiple fields to one resulting file seperated by line breaks. I have posted what I have below. I've been looking for examples all over the place that do this and know it can be done just can't find anyone who has shown this. Thanks a lot for anyones help! Chris HTML code: <html> <body> <form action="testform.php" method="post"> <p> <p>contents 1 - <input type="text" name="contents1" /> <p> <p>contents 2 - <input type="text" name="contents2" /> <input type="submit" name="submit" value="Submit!" /> </p> </body> </html> PHP code <?php if(isset($_POST['submit'])) //has the form been submitted? { $contents1 = $_POST['contents1']; $contents2 = $_POST['contents2']; $file = 'test.txt'; //this is the entire filename $create_file = fopen($file, "w+"); //create the new file if(!$create_file) { die("There was an error creating/opening the file! Make sure you have the correct permissions!\n"); } //attempt to write basic content to the file if (fwrite($create_file, $contents1) === FALSE) { echo "Error writing to file: ($file)\n"; } fclose($create_file); echo "File was created successfully!\n"; //tell the user that the file has been created successfully exit; //exit the script }else{ //the form hasn't been submitted! header("Location: addfile.html"); //redirect the user back to the add file page exit; //exit the script } ?> Quote Link to comment Share on other sites More sharing options...
farkewie Posted December 12, 2007 Share Posted December 12, 2007 Try this. <?php if ( isset($_POST['submit']) ) //has the form been submitted? { $contents = ''; $contents .= $_POST['contents1'] . "\n"; $contents .= $_POST['contents2'] . "\n"; $file = 'test.txt'; //this is the entire filename $create_file = fopen( $file, "w+" ); //create the new file if ( !$create_file ) { die( "There was an error creating/opening the file! Make sure you have the correct permissions!\n" ); } //attempt to write basic content to the file if ( fwrite($create_file, $contents) === false ) { echo "Error writing to file: ($file)\n"; } fclose( $create_file ); echo "File was created successfully!\n"; //tell the user that the file has been created successfully exit; //exit the script } else { //the form hasn't been submitted! header( "Location: addfile.html" ); //redirect the user back to the add file page } ?> Quote Link to comment Share on other sites More sharing options...
schris403 Posted December 12, 2007 Author Share Posted December 12, 2007 This works, have one more question, I'm trying to put in static text before and the fields like this but when I add the last line, it doesn't show any of the other lines in the resulting file. The line I added works ok, is there something I am missing? Thanks! Chris $contents = ''; $contents = '<?php' . "\n"; $contents .= $_POST['contents1'] . "\n"; $contents .= $_POST['contents2'] . "\n"; $contents = "?>"; Quote Link to comment Share on other sites More sharing options...
sanguinious Posted December 12, 2007 Share Posted December 12, 2007 you need to put a fullstop before the equals sign on the last line so that it appends the string to the current contents. at the moment it is overwriting it. $contents = ''; $contents = '<?php' . "\n"; $contents .= $_POST['contents1'] . "\n"; $contents .= $_POST['contents2'] . "\n"; $contents .= "?>"; Angus. Quote Link to comment Share on other sites More sharing options...
schris403 Posted December 12, 2007 Author Share Posted December 12, 2007 That works, one last thing, on the html form I have created a field which I want to browse and select a path to a folder. All I want to do is submit the folder path, is there a way to allow folders to be selected rather then files on a form? This is what I have: <html> <body> <form action="testform.php" method="post"> <p> <p>Contents 1 - <input type="text" name="contents1" /> <p> <p>Contents 2 - <input type="text" name="contents2" /> <p> <p>Contents 3 - <input type="file" name="contents3" size="20"><br /> <p> <input type="submit" name="submit" value="Submit!" /> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.