idkwhy Posted January 13, 2012 Share Posted January 13, 2012 Hi! I'm new. I'm Gabby. :3 I'm currently self teaching myself php, MySQL, and Ajax for a simulation game I'm opening up where you can breed, show, and train dogs. I've been reading tutorials on how to create a sign up page with a confirmation email being sent to you. However, I don't understand why I need multiple pages. For instance, with the tutorial I learned, you have to create three pages. One to configure and connect your site to the DB, one to display the form to sign up with, and one to display a success/failed message. My issue with that is I know it's possible using php (or is it Ajax?) to have one page. An example is on http://dogdayzz.com, when you sign up the page doesn't redirect you anywhere, it uses the same php file the entire time. Is this because the code for the form is printed in an if statement, then cleared and executed with an error/success prompt? Hopefully someone can help me out.. I couldn't find anything on google. :/ Thanks everyone for your time Gabby Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/ Share on other sites More sharing options...
Psycho Posted January 13, 2012 Share Posted January 13, 2012 An example is on http://dogdayzz.com, when you sign up the page doesn't redirect you anywhere, it uses the same php file the entire time. Is this because the code for the form is printed in an if statement, then cleared and executed with an error/success prompt? I think you are mixing up the concept of pages and scripts. For example, you say the tutorial said you would need a separate "page" for the database connection. Well, a user would not navigate to a URL to do that. It is all handled int he PHP code. For the purposes of clean/efficient code you want to break down the functionality into separate scripts (files) that do specific tasks. If you have a site that is going to be interacting with the database a lot you don't want to write your database connection routine on every single "page". instead you create the script once and include() it on the pages you need it. A simple login "page" can use many different files/scripts. I suggest you read those tutorials a little more in-depth and actually try to understand the flow of the logic. Here is a rogh exampl of a possible login "page": <?php ]//Check if form was posted if($_SERVER['REQUEST_METHOD'=='POST') { //Insert code to validate the posted data if($valid) { include('form_processing_script.php'); include('confirmation_message.php'); exit(); } else { //Create error message to show in form } } include('form_script.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307385 Share on other sites More sharing options...
wolfcry Posted January 13, 2012 Share Posted January 13, 2012 That's because the form is submitting back upon itself (to the same page). To accomplish this use the name of the file in the form action like so: <form action ="same_page_of_form.php" Method="POST"> When the user submits the form, it will simply submit back to same_page_of_form.php. So if your form is on index.php and you want to stay on index.php, simply use index.php in the action. Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307388 Share on other sites More sharing options...
jcbones Posted January 13, 2012 Share Posted January 13, 2012 Is this because the code for the form is printed in an if statement, then cleared and executed with an error/success prompt? It is very likely that is how they have it. Although, there are multiple ways you can do it. 1. AJAX, this method will submit data to the server without reloading the page. 2. PHP is able to send the data to a second page, check it, then re-direct back to the main page, behind the scenes. 3. PHP can also use if/else clauses to do the same affect. So there is not 1 defined way of doing anything in PHP. Although, everyone will have their own opinions of what 'right' is. However, when thinking of designing anything that relates to databases, and/or a user base, there are fundamentals that must be addressed. 1. security a. user information protection. b. site protection. 2. data a. integrity b. validation. There are other things, but I personally think those are the MOST important. There is one VERY important thing about MySQL that many fail to learn until it causes a full re-write of their code base. And that is normalization. , it would be wise to watch these 9 videos. Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307391 Share on other sites More sharing options...
idkwhy Posted January 13, 2012 Author Share Posted January 13, 2012 An example is on http://dogdayzz.com, when you sign up the page doesn't redirect you anywhere, it uses the same php file the entire time. Is this because the code for the form is printed in an if statement, then cleared and executed with an error/success prompt? I think you are mixing up the concept of pages and scripts. For example, you say the tutorial said you would need a separate "page" for the database connection. Well, a user would not navigate to a URL to do that. It is all handled int he PHP code. For the purposes of clean/efficient code you want to break down the functionality into separate scripts (files) that do specific tasks. If you have a site that is going to be interacting with the database a lot you don't want to write your database connection routine on every single "page". instead you create the script once and include() it on the pages you need it. A simple login "page" can use many different files/scripts. I suggest you read those tutorials a little more in-depth and actually try to understand the flow of the logic. Here is a rogh exampl of a possible login "page": <?php ]//Check if form was posted if($_SERVER['REQUEST_METHOD'=='POST') { //Insert code to validate the posted data if($valid) { include('form_processing_script.php'); include('confirmation_message.php'); exit(); } else { //Create error message to show in form } } include('form_script.php'); ?> This here: http://phpeasystep.com/phptu/24.html was the tutorial I read. I did thoroughly read through it, but as you can see, it's very simple and vague I appreciate what you told me about the include. I did actually know that, and in the tutorial it follows the include code. However my issue is that once you complete the form it redirects you to a separate file ("signup_ac.php") where it says if your confirmation was sent successfully. That's my issue because I know it's possible to eliminate that extra step and have it so the prompt replaces the form on the original sign up page. Is that a little clearer? I apologize -- That's because the form is submitting back upon itself (to the same page). To accomplish this use the name of the file in the form action like so: <form action ="same_page_of_form.php" Method="POST"> When the user submits the form, it will simply submit back to same_page_of_form.php. So if your form is on index.php and you want to stay on index.php, simply use index.php in the action. That's actually what I thought was how to fix it. I'll try that and get back to you. Thank you! -- Is this because the code for the form is printed in an if statement, then cleared and executed with an error/success prompt? It is very likely that is how they have it. Although, there are multiple ways you can do it. 1. AJAX, this method will submit data to the server without reloading the page. 2. PHP is able to send the data to a second page, check it, then re-direct back to the main page, behind the scenes. 3. PHP can also use if/else clauses to do the same affect. So there is not 1 defined way of doing anything in PHP. Although, everyone will have their own opinions of what 'right' is. However, when thinking of designing anything that relates to databases, and/or a user base, there are fundamentals that must be addressed. 1. security a. user information protection. b. site protection. 2. data a. integrity b. validation. There are other things, but I personally think those are the MOST important. There is one VERY important thing about MySQL that many fail to learn until it causes a full re-write of their code base. And that is normalization. , it would be wise to watch these 9 videos. I like choice two and three the best. And I do agree with everything you said regarding security and data. Andd I'll watch those videos soon Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307408 Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2012 Share Posted January 13, 2012 I would strongly advise you not to use phpeasystep.com as a learning resource. Nearly everything on that site is over 8 years out of date. Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307409 Share on other sites More sharing options...
idkwhy Posted January 13, 2012 Author Share Posted January 13, 2012 Thank you!!!! So much!!! I had a feeling they were outdated but being a total newb, I would have went and used them stupidly. Is it possible you can provide me with some learning sources which in your opinion are better and more up to date? Thank you soo much!! Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307410 Share on other sites More sharing options...
Psycho Posted January 13, 2012 Share Posted January 13, 2012 I appreciate what you told me about the include. I did actually know that, and in the tutorial it follows the include code. However my issue is that once you complete the form it redirects you to a separate file ("signup_ac.php") where it says if your confirmation was sent successfully. That's my issue because I know it's possible to eliminate that extra step and have it so the prompt replaces the form on the original sign up page. Is that a little clearer? I apologize There are 101 ways to build the functionality for a form. You can have everything update on the page without an actual page refresh using AJAX. But, that is something you should implement AFTER you have a fully working page without AJAX. Also, a good reason why you would want to redirect the user after a successful form submission is that it will clear the POST data. Otherwise you have to handle the situation of users clicking the refresh button and preventing duplicate entries. A redirect is an easy way yo prevent this. Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307411 Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2012 Share Posted January 13, 2012 People seem to like tizag.com, and you really can't beat the manual for information. www.php.net Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307412 Share on other sites More sharing options...
wolfcry Posted January 13, 2012 Share Posted January 13, 2012 Thank you!!!! So much!!! I had a feeling they were outdated but being a total newb, I would have went and used them stupidly. Is it possible you can provide me with some learning sources which in your opinion are better and more up to date? Thank you soo much!! Yeah, Pikachu2000 hit the nail on the head with the manual reference and you need to bookmark that site. Though, sometimes it can be confusing for those just learning PHP IMHO. If you like books, one of my favorites is Beginning PHP and MySQL and if you're more of a visual type of learner through "live video" teaching, http://thenewboston.org/tutorials.php has awesome tutorials on quite a few languages, including PHP (It's under the Computer Programming) section on that page. Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307415 Share on other sites More sharing options...
idkwhy Posted January 13, 2012 Author Share Posted January 13, 2012 That was my issue :/ I was really intimidated by the php.net because I'm new Quote Link to comment https://forums.phpfreaks.com/topic/254975-question/#findComment-1307417 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.