I have a user registration from, but since it has five fields, I need to separate them to be filled one form at a time. I have achieve this by making 5 pages, and taking user from one page to the next by creating values, and sessions to remember the user's filled-in values. But, I no longer need the script to run in 5 pages. I need all scripts to run in 1 page only. Without Javascript. I need PHP to make this, but the problem is I can't. I got close by giving the page ?id=name , ?id=lastname and getting the id, to move to the next. But, it always has some problems. I can't get it fully done. for example check this: <?php if(!isset($_GET['id']) && empty($_GET['id'])) { // ID does not EXIST so, user must be on first phase, so.. // Fill in your First name //post will have a value of action="registere.php?id=lastname" to send user to that url } else { if(isset($_GET['id']) && !empty($_GET['id'])) { // If id exists if($_GET['id']=="lastname") { //and if id = lastname //then last name must be filled. //please inser your last name, //post will have a value of action="registere.php?id=age" to send user to that url } else if($_GET['id']=="age") { // Id is age, /// fill your age here.. } } Code (markup): I hope you got my point and the example. Can you just give me a little example like that to work on thanks
PHP runs on the server and creates the HTML that's sent to the user's browser. You can't have the user filling in a value in one part of your PHP code, then execute the next part of the PHP code. It's not hard to create, it just can't be done. The proper way to do this is to present the user with all the input fields (this can be written as an HTML page, or you can use PHP to create the HTML.). When the user clicks the submit button you use Javascript to make sure that all the required fields are filled in, that they're sane (no alpha for phone numbers, postal codes in the right form, etc.), then send that to a PHP page that does what's needed.
You don't get the point. I don't want the script to run without PHP loading the page. I need the page to load. Each form will be submitted until the fifth page. for example, when you fill your name and submit the result, then the page will create an ?id=lastname, now PHP will check if id exists after submitting, and will get results depending on the ID submitted. In this case, since ?id=lastname, it will show the form for the last name.... and so on..
Hi, The below code will help you, i did this with 3 fields. <?php session_start(); ?> <?php if(!isset($_GET['id'])) { session_destroy(); ?> <form name="form" action="example.php?id=2" method="POST"> <pre> Field1 <input type="text" name="field1" /> <input type="submit" value="Next"> </pre> </form> <?php } else { switch($_GET['id']) { case 2: { $_SESSION['field1']=$_POST['field1']; echo '<form name="form" action="example.php?id=3" method="POST"> <pre> Field2 <input type="text" name="field2" /> <input type="submit" value="Next"> </pre> </form>'; break; } case 3: { echo '<form name="form" action="example.php?id=4" method="POST"> <pre> Field3 <input type="text" name="field3" /> <input type="submit" value="Finish"> </pre> </form>'; $_SESSION['field2']=$_POST['field2']; break; } case 4: { $_SESSION['field3']=$_POST['field3']; echo "<br>Field 1 : ".$_SESSION['field1']; echo "<br>Field 2 : ".$_SESSION['field2']; echo "<br>Field 3 : ".$_SESSION['field3']; //Write DB code here echo "<br>Successfully Stored"; break; } } } ?>
Thank you glagokulan that worked well. Although, I hoped to store the form in a variable / function and re-use it instead of creating a form after each case: tag. But, the problem is the URL would stay the same if I was to store the form in a variable like this: $form = echo "<form action="example.php" method="POST"> <input name....."; then, the example.php would never change, and that would be a problem. nevertheless, thanks.
Not the vaule of the $_POST array. But, to make it nicer, I will replace the id's with worlds like, first name, lastname, country... Is there anyway, to store the form in a function and re-use it instead of having 5 similar posts? I know the action="" would have to change, but maybe by sending a parameter: function($current_url) {//code} ?
Check the following code: <?php session_start(); ?> <?php if(!isset($_POST['id'])) { session_destroy(); ?> <form name="form" action="example.php" method="POST"> <pre> Field1 <input type="text" name="field1" /> <input type="hidden" name="id" value="2"/> <input type="submit" value="Next"> </pre> </form> <?php } else { switch($_POST['id']) { case 2: { $_SESSION['field1']=$_POST['field1']; echo '<form name="form" action="example.php" method="POST"> <pre> Field2 <input type="text" name="field2" /> <input type="hidden" name="id" value="3"/> <input type="submit" value="Next"> </pre> </form>'; break; } case 3: { echo '<form name="form" action="example.php" method="POST"> <pre> Field3 <input type="text" name="field3" /> <input type="hidden" name="id" value="4"/> <input type="submit" value="Finish"> </pre> </form>'; $_SESSION['field2']=$_POST['field2']; break; } case 4: { $_SESSION['field3']=$_POST['field3']; echo "<br>Field 1 : ".$_SESSION['field1']; echo "<br>Field 2 : ".$_SESSION['field2']; echo "<br>Field 3 : ".$_SESSION['field3']; //Write DB code here echo "<br>Successfully Stored"; break; } } } ?>
I know i am rewriting the form, Its just a logic implementation. After the code works well, We can do the code clean and other things..
Well we are at this, there is something I need to know about mysql. I have let's say a registration form, that has 20 inputs, and a user may fill all or some, or even randomly, now these values are submitted into mysql. Now, the problem is when echoing out the data from database, how can I make mysql understand, to echo out only the data that currently exists? I know i could use the mysql_num_rows to check, but I don't need to check on row, I need to check all at once, and only output the ones available.
Rows? SELECT ... WHERE field1 IS NOT NULL and field2 IS NOT NULL and ... Columns? Echo variables where trim($variable) isn't '' (2 single quotes). Or are you inserting a new row each time the user submits the form? (Which you absolutely should not do.)
No this has nothing to do with submitting code into db, I just want to echo out from 20 rows, that are not empty buy making a single function instead of making query for each rows if they are filled or not
As I said, if you want to eliminate any rows in which one or more fields are blank, use the SQL statement (with the IS NOT NULL). If you want to not echo blank field values, use the second method (check whether the variable is blank). If you want something else, specify exactly what. BTW, sometimes, you just can't do it all in one line, you have to make more than one query. It can all be in one function (there's no limit to the number of queries you can make in one function), but one query could result in such convoluted code that in 6 months, if you have to go back to it, you'll never figure out what you were trying to accomplish. And that's about the worst thing you can do when writing code. Bad code can be fixed. Code that can't be understood can only be deleted.
One way I have used to propagate values from form to form is to put the values from each form into hidden fields of the next form when you write it back from the php code. Of course, the user does not see these values, unless they look at source. And they come back each time to the php code. I tried session variables, but decided that they were too complicated. This way I could present the next form based on what the user had entered in the current form. If you have only one form and do not want blank entries, simply validate the entries and ask the user to enter a value. You can also do a remember me check box and drop a cookie so that the next time you already know who they are and you can show something like "Welcome back John - you last visited 3 days ago" Use their email or some other id for the index in the database to retrieve their information. You can use java script or server side code for entry validation. Or even AJAX if you want to do things like fill out the rest of their form from the database once they have entered their email or user id. Java script for things like shipping address the same/different from credit card address to copy the same values to the rest of the form. Many times it is the little things that make the user perceive your entry form as friendly. Like putting the focus on the first entry field. I've simply expect that when a form is shown. It's a real bother when I have to click on the entry field.
And if there's an empty field or a field with the wrong data, put the cursor in that field after showing the user the error message. (After, not before, or it won't work.)