Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing form problem - Username taken 1

Status
Not open for further replies.

sipps

Technical User
Feb 9, 2003
133
GB
Hi,

I have created a form where users/admin can add new users to the system. I managed to get the username to be unique by checking usernames that are already in those tables. If a username already exists, then it goes to an error page. But what I would really like, and think is more professional, is for it to almost refresh the registration page, with all the correct details filled in, but take out the username they entered, and have some text above the input box that just says "Please choose a different username". I really don't know how to do this! Please can someone point me in the right direction. I have tried creating a new page that looks the same, and to get the values from POST but this doesn't work.

Thanks!
 
One way is to have the script submit data to itself.

If no input exists, then the script is being run for the first time, so the script should output the form.

When the form is submitted with input, then input exists. The script can process that input. If there are any errors, it can output the form again, only with filled-in fields and error messages.

If there are no errors, the page can create the user and redirect the browser to an appropriate page. Want the best answers? Ask the best questions: TANSTAAFL!
 
Sorry sleipnir214,

I don't understand how to do that. If all the details are correct, then it goes to a success page, but if that username exists, it should submit the data to itself? How would I get it to do that?

Thanks
 
The basic structure of the script can be:

if (input exists)
{
if (username does not exist)
{
create user
show success page
}
else
{
Display error message "user exists"
display form with pre-filled out values
}
}
else
{
Display blank form
}

Want the best answers? Ask the best questions: TANSTAAFL!
 
Hi

I have decided to set up a variable every time the index page is viewed for new customers. Once they go to fill in their details, if ath username is taken, they are sent to a new registration page. I want to carry the form data accross using variables that are stored in the session. I have started the session, but cannot seem to get the results I'm after.

The session starts on the first page like so:

session_start();
if (isset($HTTP_POST_VARS['Customer_Fname'])) {$details = $HTTP_POST_VARS['Customer_Fname'];
session_register("details");
}

This registers the variable details with the form text box data, the customers first name. (well that's what it's supposed to do!).

The next page is like so:

session_start(); (in header)

Then in the form, the Customer_Fname textbox:

<input name=&quot;Customer_Fname&quot; type=&quot;text&quot; value=&quot;<?php echo $HTTP_SESSION_VARS['Customer_Fname']; ?>&quot; size=&quot;15&quot; maxlength=&quot;10&quot;>

I get an error message saying undefined index Customer_Fname.

I'm not sure whether I'm assigning the value of teh text box to the session variable correctly.

Do you have any ideas?

Thanks
 
Sorry,

The form textbox now reads:

<input name=&quot;Customer_Fname&quot; type=&quot;text&quot; size=&quot;15&quot; maxlength=&quot;10&quot; value=&quot;<?php echo $HTTP_SESSION_VARS['details']; ?>&quot;>

session_start; is still in the head. It still gives the error message undefined index.

Do I have to register the session on the second page as well? I have tried that, but still same error. I have read that I only have to have the session_start();

I know that sessions work, because I have written a test script from a book:

First page:
<?php
session_register( &quot;test&quot; );
$HTTP_SESSION_VARS['test'] = 5;
?>

Next Page:
<?php
print &quot;test is.. &quot;;
print $HTTP_SESSION_VARS['test'];
?>

and it prints 5, so it passes that ok. I just cant get it to assign the value of my form to that variable.
Do I have to write it like 'form.textboxname',
ie.
session_start();
session_register( &quot;details&quot; );
$HTTP_SESSION_VARS['details'] = register.Customer_Fname;

Thank you for all your help
 
I'm not sure exactly what's going on... but I think what you're looking for is...

session_register(&quot;details&quot;);
$HTTP_SESSION_VARS['details'] = $_POST['Customer_Fname'];

 
Thank you skiflyer!

That worked. Now how would I carry all the form data from the previous page across using this session, or would I have to create a new session variable for every form element? I have Fname, Lname, DOB, Address etc.... so there are quite a few elements that would need to be taken across.

Thanks again! It's very frustrating when you are just learning and it's getting late!
 
The easiest way I could imagine would be to do something like this...

session_register(&quot;form_elements&quot;);
$HTTP_SESSION_VARS['form_elements'] = $_POST;

Then on the next page You'll have all of those variables stored in the session variable as an array. Eventually you'll need to pull them out but no need to do it more than once.

-Rob
 
So if that takes all of the elements that were POST from the form, how do I pull them out the next page, do I have to go through <?php echo $HTTP_SESSION_VARS['form_elements[0]'];?>,
<?php echo$HTTP_SESSION_VARS['form_elements[1]'];?> etc?

Thanks Rob
 
I hope I get this right... it'd be more like

echo $HTTP_SESSION_VARS['form_elements']['Customer_Fname'];

Or if you to just see them...

foreach ($HTTP_SESSION_VARS['form_elements'] as $key=>$value) {
echo '$HTTP_SESSION_VARS[\'form_elements\'][\''.$key.'\'] holds '.$value.'<BR>';
}

Going to have to double check me on that one, all those single quotes escaped and such without emacs... but the idea is there...

Good luck.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top