Learn to Code via Tutorials on Repl.it!

← Back to all posts
Forms in websites that give you responses in three steps
catchacoled (22)

For all my unexperienced programmers out there trying to make themselves a site you prob know that if you just put a form there it will not give you the answers when someone uses it. To make it give you the answer follow this tutorial.

Step1:

index.php

Type this above all code on the entire page. This goes outside of the <html> at the top of the file

<?php
//This will make you able to create session variables
session_start();
?>

Step 2:

index.php

To make your form appear on your site use this code

<form action="form.php" method="post>
<!-Your form text space->
<input type="text" name="text">
<!-Your submit button->
<input type="submit" name="submitBtn">
</form>

That will make the form appear on your site. That one is just a text bar and submit button.

Step 3:

form.php

For the PHP part. This part will make it where when a person submits the form it will create a new file named response.txt and will show you what they said.
type this only in the PHP file

<?php
//creates session variable out of text
$_SESSION['response'] = $_POST['text'];
//opens response.txt
$file = fopen("response.txt", "w");
//Writes what they said
fwrite($file, $_SESSION['response'];
//Ends the writing process
fclose($file);
?>

Now when you wanna check if anyone has responded to your form go to response.txt and it will have everything that has been said in the form.

Comments
hotnewtop
proinject (1)

What did I do Wrong??? Every single time I work with php it doesn't work out.

catchacoled (22)

I would have to see what you have on your project to help you with that. Did you start the project in PHP, not HTML
@proinject

proinject (1)

@catchacoled i figured it out thank you.

Highwayman (1441)

I didn't even know I needed this until you put it up thank you so much XD

catchacoled (22)

I hope this helps anyone who had had trouble with this