Jump to content

Simple php form with results file question


schris403

Recommended Posts

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

}



?>

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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 = "?>";

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.