mazinger Posted June 4, 2017 Share Posted June 4, 2017 I'm trying to learn php from a book and one of the first examples is a simple form where the user enters a name and the name is displayed, all this is made on one file. I want to split it into two files, but can't get it to work. This is what I have on the first file where the form is. <html> <head> <title>Form Test</title> </head> <body> Your name is <?php echo $name ?> <form method="post" action="formtest.php"> What is your name <input type="text" name="name"/> <input type="submit"/> </form> </body> </html> This is the second file I created by taking the php out of the first file. <?php if(isset($_POST['name'])) { $name = $_POST['name']; } else { $name = "Enter a name"; } require_once('formtest.php'); ?> The variable $name contains whatever the user typed into the form and I want that to be displayed but when I test it I get the following error message Notice: Undefined variable: name in C:\xampp\htdocs\testing\formtest.php on line 7 Hopefully you guys can tell me what I'm doing wrong, I know I could do this in one file but I want to see If I can do it in two separate files, thanks in advance for your help Quote Link to comment Share on other sites More sharing options...
requinix Posted June 4, 2017 Share Posted June 4, 2017 The second file has to execute before the first one. Call that formtest.php and rename the other to something else. Quote Link to comment Share on other sites More sharing options...
mazinger Posted June 4, 2017 Author Share Posted June 4, 2017 I guess I should have specified the names of the files. The first file on my post is called formtest.php and the second one is called formtest-controller.php Are you saying I should reverse the names? Thanks for helping. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 4, 2017 Share Posted June 4, 2017 Yes. Think about it: if the first executes first then there won't be a $name. It has to come from the second file to get defined so the first can use it. Quote Link to comment Share on other sites More sharing options...
mazinger Posted June 4, 2017 Author Share Posted June 4, 2017 I tried it, when the second executes first then there is no form where the user can type their name because the first file never executes, maybe I'm not connecting the two files correctly. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 4, 2017 Share Posted June 4, 2017 You still need the require_once, but remember to change the filename. The first file that executes (second code you posted) has the code for $name. That will be formtest.php. You can then use require_once to include the second file (first code you posted) which has the HTML. Quote Link to comment Share on other sites More sharing options...
mazinger Posted June 4, 2017 Author Share Posted June 4, 2017 After all of that I still get the same error, Notice: Undefined variable: name in C:\xampp\htdocs\testing\formtest.php on line 7 I used to get it as soon as the page loaded but now i get it after I enter the name. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 4, 2017 Share Posted June 4, 2017 What is the code for both files and which one is which? Quote Link to comment Share on other sites More sharing options...
mazinger Posted June 4, 2017 Author Share Posted June 4, 2017 I fixed it by changing to action="" in the form. that made it work, even though I don't know why. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 7, 2017 Share Posted June 7, 2017 An empty action is invalid markup. If you want to post the form to the current page, simply leave out the action altogether. 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.