Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Katsulynx on August 12, 2011, 12:11:24 PM

Title: Making a new .html form for users - how to get user name?
Post by: Katsulynx on August 12, 2011, 12:11:24 PM
Hey everyone,

I am about to write a new registration page for some stuff for my forum but actually I don't know how to solve my problem...
I allready coded how the page should look like and the form itself, which is allready insering everything in the data base except the user who is sending... Now I would like to know how i could tell my script who the user is and what his ip is so it can insert it into the database. Does anyone have an idea for me?
Title: Re: Making a new .html form for users - how to get user name?
Post by: IchBin™ on August 13, 2011, 12:15:02 AM
You can't just use straight HTML for that. Your form page will have to use PHP to grab that data.

$user_info['name'] has the username stored in it.

To get the user's IP you could also use $user_info['ip'] to get that info.
Title: Re: Making a new .html form for users - how to get user name?
Post by: Katsulynx on August 13, 2011, 07:47:44 AM
Thanks for the help. I tried to add those two lines into my process.php als well as into my .html-document, but unluckily, it did'nt work at all...

I added it into my html-page like that:
Code: [Select]
    <?php
  $user_info
['name'];
$user_info['ip'];
$ip=$user_info['ip']
$name=$user_info['name']
?>
directly under the java-script which is at the beginning of the header. There is no error when sending the form to the database, it is sucessfully added, but the field with the name and the ip still remain empty.
With the information that is set in the header, the form fills in itself:
Code: [Select]
<input type="hidden" name="ip" value="$ip">
<input type="hidden" name="name" value="$name">
and at last, the process.php gets the information like any other else:
Code: [Select]
$name=$_POST['name'];
$ip=$_POST['ip'];
Title: Re: Making a new .html form for users - how to get user name?
Post by: Joker™ on August 13, 2011, 09:48:12 AM
Can you attach your files here?
Title: Re: Making a new .html form for users - how to get user name?
Post by: IchBin™ on August 13, 2011, 11:36:10 AM
How are you pulling the variables into your page? Are you including SSI.php or something?
Title: Re: Making a new .html form for users - how to get user name?
Post by: Katsulynx on August 13, 2011, 01:11:10 PM
Edit:
I now changed the page to an .php page following the tutorial in this forum. (http://"http://www.simplemachines.org/community/index.php?topic=390247.0") But to be honest, I dont know where to add the codes you gave me now... I want to fill the variables $name and $ip with the current users name and ip, which are then added to their fields automatically:
Code: [Select]
<input type="hidden" name="id" value="$ip">
<input type="hidden" name="name" value=""$name">

and maybe someone can help me with integrating a javascript in this page which tests whether all fields are filled out or not, because there is no header to integrate it in and if I make one, there is an error on the final page which says, that the page already gots his header from the subs.php
Title: Re: Making a new .html form for users - how to get user name?
Post by: IchBin™ on August 13, 2011, 03:50:13 PM
Sorry don't have the time as I'm headed out the door. However a quick explanation is in order. When you want to output a variable into HTML you need to tell PHP your going to do this. In your code you should do something like this:

<input type="hidden" name="id" value="'.$ip.'">

That tells echo ''; to escape into PHP mode basically, and then escape back into HTML (string) mode.
Title: Re: Making a new .html form for users - how to get user name?
Post by: Katsulynx on August 13, 2011, 04:36:19 PM
Maybe I expressed myself a little bit missunderstandable.. sorry, its my fault because I only speak school-english
let's try to explain again:

I created a new page based on php, so its now no html-document any more but an .php document. It's located in the same folder as my forum and does exactly look like the rest of the forum. If you want to try it, here is the page: http://nightfox.bplaced.de/Forum/Charakteranmeldung.php
Now there are a lot of fields to be filled out by the user and when he submits the document with the last button, he will be redirected to the next file which adds all the information he sent to the database. But now I want the next file to send also his name and his ip to the database. So it is necessary to insert his name and his ip into a hidden field in the form which fills itself with a variable that includes his name and his ip.
Code: [Select]
<form name="Formular" action="xyz.php" method="post">
<input type="hidden" name="id" value="$ip">
<input type="hidden" name="name" value=""$name">
Now I need to get the name and ip of the user and put them into the variables ip and name. My problem is now, that the two commands $user_info['name'] and $user_info['ip'] seems not to work. However, the SSI.php is already included in my .php file:
Code: [Select]
//Path to SSI.php
require(dirname(__FILE__) . '/SSI.php');

So now I need to call the name and the ip of the current user and put them into those two variables and that is, what I am not able to do..
Title: Re: Making a new .html form for users - how to get user name?
Post by: IchBin™ on August 13, 2011, 06:57:56 PM
Without knowing what is in each file I cannot tell how you need to do this. It looks like your Charakteranmeldung.php file is sending the form input data to the CharakteranmeldungAbsenden.php file. Why not include SSI.php in the Charakteranmeldung.php page , and then you can add the hidden inputs to that page which you then add the name and IP to?

If you include SSI in that page and then setup the inputs like I showed you a post above the ip/name will be sent in the form, which you can then grab and do whatever you need to when the data is sent to the Charakteranmeldung.php page.

The Charakteranmeldung page should then be able to grab the name and ip from the $_POST array.
Title: Re: Making a new .html form for users - how to get user name?
Post by: Katsulynx on August 13, 2011, 08:04:43 PM
I already included SSI in my Charakteranmeldung.php, but the commands
$user_info['name'] and $user_info['ip']
cause
Code: [Select]
Parse error: syntax error, unexpected T_VARIABLE in /users/nightfox/www/Forum/Charakteranmeldung.php on line 21line 21 is where i add those two things. I tried to only write them there as
Code: [Select]
$user_info['name']or write them like
Code: [Select]
"$name = $user_info['name']" or
Code: [Select]
$ip='.user_info['ip'].'is also causing the same error.

If I would be able to get the name and the ip stored into the two variables $name and $ip, it shoult work.
Title: Re: Making a new .html form for users - how to get user name?
Post by: IchBin™ on August 13, 2011, 08:12:10 PM
Code: [Select]
$name = $user_info['name'];
$ip = $user_info['ip'];

If you are inside a function make sure that you are either calling the SSI.php file inside that function, or that you global $user_info at the top of your function if the include is outside the function.
Title: Re: Making a new .html form for users - how to get user name?
Post by: Katsulynx on August 13, 2011, 08:19:04 PM
it does now look like that:
Code: [Select]
require(dirname(__FILE__) . '/SSI.php');
global $name = $user_info['name'];
global $ip = $user_info['ip'];
but it also causes the Parse error in the first line beginning with global...
Title: Re: Making a new .html form for users - how to get user name?
Post by: IchBin™ on August 13, 2011, 08:21:10 PM
Global is only used if you are pulling a variable in from outside a function.

Code: [Select]
require(dirname(__FILE__) . '/SSI.php');
$name = $user_info['name'];
$ip = $user_info['ip'];
Title: Re: Making a new .html form for users - how to get user name?
Post by: Katsulynx on August 13, 2011, 08:27:47 PM
Oh jeah!! Finally it is working!! Thanks a thousand times!!!
I thought it should be global because I added it at the beginning of my .php file and the point where it is used is very much further down, but it seems it should not.
So in the end, it was so simple, but I failed epically...
Thanks a lot for your help!  ;D
Title: Re: Making a new .html form for users - how to get user name?
Post by: IchBin™ on August 13, 2011, 08:29:13 PM
No problem. Simple things often take time to learn. Once you wrap your head around it you just laugh at yourself. Makes for some good times. lol