0

My first question. I am currently learning html, and am using 000webhost to create a type of trial site to test things for myself. I have the premium one, so have many features. I am currently making a login system, starting with the registration part, which is the struggle.

I have tried many different types of code, to maybe get it to save in a file IN MY file manager for example. I looked at SQL but it is slightly complex for my goal. Even looking at free tutorials I couldn't really find anything which can directly guide me, it was mostly generic stuff.

<form action="/action_page.php">
    First name:<br>
     input type="text" name="Username"> <br>
    Last name:<br>
    input type="text" name="Last name"> <br>
    Email:<br>
    input type="text" name="Email"> <br>
    Password:<br>
    input type="password" name="password"

    <input type="submit" 
    form action="/action_page.php">
 </form>

This is some simple code which I copied and edited from some free tutorial. What needs to be added to make the inputs be saved so that i can continue to make a login system. And would this file be saved within the file manager.

So from what I understood, this would write the inputs to a file with the name action page. But i have played around and nothing seems to be happening. Any advice or a link to a useful tutorial which can directly help me?

NOTE: I removed some of the < and > so the code appears in raw form, as it is html and yeah.

Suraj Kumar
  • 5,290
  • 8
  • 18
  • 37
  • The code at the most will try and submit a query string within the URL containing the input values. If action_page.php isnt setup to read the URL query then it will do nothing if you dont use the information in the URL. The URL on the action_page.php will look like this `/action_page.php?username=Mickey&email=Mouse&password=Mouse` You have to create code to read that string and use it in which ever way you want. Seems like a login. Which is horrible for security because the password and username is passed through the URL. But I understand this could be for fun. – Rick Sibley Jun 06 '19 at 15:07
  • Some sources: https://www.w3schools.com/html/html_forms.asp https://www.w3schools.com/howto/howto_css_login_form.asp PHP Handling of the Form: https://www.w3schools.com/php/php_forms.asp – Rick Sibley Jun 06 '19 at 15:11
  • I went into an old project I had and fixed/added a few things. This is a fun way to play around with login information. It uses cookies and JavaScript; So if cookies are cleared the data is gone, or if you move to a different computer. Anyway, head [here to the project](https://paymaster.com/other/test/misc/login-script/) then press on "Open Prompt" Type the word "download" then press enter or hit ok. It will download all the files used to create the page. I did it on an asp server but I didnt use any asp. You can just rename it .html and drop the first line of code in the .aspx & Del the .vb – Rick Sibley Jun 06 '19 at 21:10
  • Hi Hassan - just following up. Was your question answered satisfactorily? If there is more I can help with, please add a comment below my answer, or edit your question to clarify what else you want to know. Otherwise, it would be great if you could choose a "best answer" (by clicking the checkmark beside the answer) to close out the question. If no answer provided helpful information, please add your own answer and select that as the best answer. *(You won't get any points for doing so, but that will close out the question.)* Thanks! – cssyphus Jun 11 '19 at 23:27

1 Answers1

1

So from what I understood, this would write the inputs to a file with the name action page. But i have played around and nothing seems to be happening.

No, that is not what <form action="action_page.php"> would do.

When the user clicks the submit button, that would SEND the user over to a different web page, as specified in the action= attribute of the <form> tag - in this case, one called action_page.php. If action_page.php just said:

<h1>Hello</h1>

(and that was the full content of the page), then Hello would appear on the screen and nothing at all would be done with the form data.

So, you need to create a page that gets the data variables sent over from the first page and does something with them. The variables are formed on the first page like this:

<input name="first_name" type="text" />

When the form is submitted, there will be a variable called first_name with the contents being whatever the user typed.

On the second page (action_page.php), you need to get those variables:

$fname = $_REQUEST['first_name'];

Now, you have what the user typed for a first name in a variable called $fname (all PHP variables must begin with a $).

So if this was the full contents of action_page.php, submitting the form would output "Hello John, how are you?":

<?php
    $fname = $_REQUEST['first_name'];
?>
<h1>Hello <?php echo $fname; ?>, how are you?</h1>

Try it.


Also read this important article:

https://stackoverflow.com/a/54757176/1447509


Update - response to comment:

It is the user who types into the form fields, so nothing in there would tell the PHP processing file (action_page.php in your example) where to put the data. That is your (the developer's) decision. As Rick said in his comment, you have options. 99% of the time we stick the info into a MySQL (now called MariaDB) database - but you could practice by first saving data into a text file.

Using the above example, you can change the action_page.php file to this:

<?php
    $fname = $_REQUEST['first_name'];
?>
$myFile = fopen('___mytest.txt', 'w');
$fwrite($myFile, "Received from the HTML side: " . $fname);
fclose($myFile);

Note: Be verrrry careful to end every line with a semi-colon ; -- EVERY LINE! When PHP breaks, there is no message - it just stops. The most common mistake is forgetting to end a line with the semi-colon. (I started the file name with 3 underscore chars just to make it stand out and appear at the top of the other files.)

When you get that working, then your next step is to create a MySQL database and figure out how to write the data into that. There are somewhere close to a gazillion YouTube tutorials that will help.

Writing the data to a text file (as a first step) will: (1) prove to you that you're on the right track, (2) prove that everything works so far, and (3) give you successful experience receiving data from HTML and saving it somewhere.

PHP - Secure member-only pages with a login system

https://www.thoughtco.com/write-to-a-file-from-php-2693790

Community
  • 1
  • 1
cssyphus
  • 31,599
  • 16
  • 79
  • 97
  • i think this could be the solution. But my problem is that obviously i just copied the form action stuff and it doesnt seem to relate at all. But what would you put with to allow the input to be saved to a file of some sort. I dont want it to be outputted in anyway at this point, just for it to be saved. – Hassan Ayoub Jun 06 '19 at 17:08
  • Saved where? in a database? As a cookie? A local txt file on the server? – Rick Sibley Jun 06 '19 at 17:51
  • 1
    @HassanAyoub See updated answer with response to your comment. – cssyphus Jun 06 '19 at 18:52