...

View Full Version : Creating a simple form that displays field output on same page?



hatz
08-08-2007, 09:43 PM
Can someone help me with this? I posted in wrong forum to being with, so i'll link to that.

http://www.codingforums.com/showthread.php?p=597623

any help appreciated

mcjwb
08-08-2007, 10:30 PM
You're missing the closing bracket, ), from the if.


<?php
if (isset($_REQUEST["FirstName"])) {
$reply = $_REQUEST["FirstName"];
}
if (isset($_REQUEST["LastName"])) {
$reply += $_REQUEST["LastName"];
}

echo "Thanks for applying, ".reply;
?>

Also I would use $_GET or $_POST, depending on the method your form uses, instead of $_REQUEST.

Len Whistler
08-08-2007, 11:25 PM
Also to post to the same page you use $PHP_SELF, as in the sample below:


<form method="post" action="<?php echo $PHP_SELF;?>">

hatz
08-09-2007, 12:12 AM
alright that php self didn't work, thanks for suggestion though. I'm gonna try and make it print on another page now. Let me show u exactly what i have to make it easier:

THE PAGE WHERE TO REGISTER:

tournament.php


<?php

// standard hack prevent
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

// standard session management
$userdata = session_pagestart($user_ip, PAGE_TOURNAMENT);
init_userprefs($userdata);

// set page title
$page_title = 'TOURNAMENT';

// standard page header
include($phpbb_root_path . 'includes/page_header.'.$phpEx);




if (isset($_POST["FirstName"])) {
$reply = $_POST["FirstName"];
}

echo "Thanks for applying, ".reply;



// assign template
$template->set_filenames(array(
'body' => 'tournament.tpl')
);

$template->pparse('body');


?>



Template for Tournament.php:


<HTML>

<table width="100%" cellspacing="0" cellpadding="2" border="0" align="center">




<tr>
<td align="left" valign="bottom"><span class="gensmall">
<!-- BEGIN switch_user_logged_in -->
{LAST_VISIT_DATE}<br />
<!-- END switch_user_logged_in -->
{CURRENT_TIME}<br /></span><span class="nav"><a href="{U_INDEX}" class="nav">{L_INDEX}</a></span></td>
<td align="right" valign="bottom" class="gensmall">
<!-- BEGIN switch_user_logged_in -->
<a href="{U_SEARCH_NEW}" class="gensmall">{L_SEARCH_NEW}</a><br /><a href="{U_SEARCH_SELF}" class="gensmall">{L_SEARCH_SELF}</a><br />
<!-- END switch_user_logged_in -->
<a style="cursor:hand" onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://snkclan.nuclearfallout.net');" class="gensmall">Make SNK Your Homepage</a></span></td>

</tr>
</table>




<table width="100%" cellpadding="1" cellspacing="1" border="0">
<tr>
<td style="vertical-align:top;">


<table width="100%" cellpadding="5" cellspacing="5" class="forumline">
<tr>
<th width="center" cellspacing="2" cellpadding="2" class="thCornerL" height="25" nowrap="nowrap">&nbsp;Tournament Registration&nbsp;</th>
</tr>



<tr>
<td align="center" class="row1"><span class="nav">






<form method="post" action="registered.php" <!-- action may also be myform.php, depending on your Server Side language -->
Counter-Strike Name: <input type="text" name="FirstName" size="10" /><br />
<input type="submit" value="Submit" name="B1" />
</form>
<HTML>


Page to be taken and have names printed after registering:

registered.php


<?php

// standard hack prevent
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);


// standard session management
$userdata = session_pagestart($user_ip, PAGE_REGISTERED);
init_userprefs($userdata);

// set page title
$page_title = 'REGISTERED';

// standard page header
include($phpbb_root_path . 'includes/page_header.'.$phpEx);

// assign template
$template->set_filenames(array(
'body' => 'registered.tpl')
);

$template->pparse('body');


?>



Template for registered.php


<HTML>

<table width="100%" cellspacing="0" cellpadding="2" border="0" align="center">




<tr>
<td align="left" valign="bottom"><span class="gensmall">
<!-- BEGIN switch_user_logged_in -->
{LAST_VISIT_DATE}<br />
<!-- END switch_user_logged_in -->
{CURRENT_TIME}<br /></span><span class="nav"><a href="{U_INDEX}" class="nav">{L_INDEX}</a></span></td>
<td align="right" valign="bottom" class="gensmall">
<!-- BEGIN switch_user_logged_in -->
<a href="{U_SEARCH_NEW}" class="gensmall">{L_SEARCH_NEW}</a><br /><a href="{U_SEARCH_SELF}" class="gensmall">{L_SEARCH_SELF}</a><br />
<!-- END switch_user_logged_in -->
<a style="cursor:hand" onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://snkclan.nuclearfallout.net');" class="gensmall">Make SNK Your Homepage</a></span></td>

</tr>
</table>




<table width="100%" cellpadding="1" cellspacing="1" border="0">
<tr>
<td style="vertical-align:top;">


<table width="100%" cellpadding="5" cellspacing="5" class="forumline">
<tr>
<th width="center" cellspacing="2" cellpadding="2" class="thCornerL" height="25" nowrap="nowrap">&nbsp;Registered Users&nbsp;</th>
</tr>



<tr>
<td align="center" class="row1"><span class="nav">




<HTML>


I hope this helps. As of now, when a name is entered and submitted, it takes me to registered.php. But it does not post or print anything.

mcjwb
08-09-2007, 12:30 AM
OK, you need to move this code:

if (isset($_POST["FirstName"])) {
$reply = $_POST["FirstName"];
}

echo "Thanks for applying, ".reply;

out of tournament.php and into registered.php since that is where your form is pointed to. Oh and your form tag in the Template for Tournament.php looks like it's missing a >.

Also since you're using phpbb you'll have to check their documentation on how to assign variables so you can display them in templates. I'm not sure whether echo "Thanks for applying, ".reply; will have the desired effect.

P.S. I think Len meant $_SERVER['PHP_SELF'].

hatz
08-09-2007, 12:33 AM
it doesnt look like the echo does, i saw it printing thanks for applying at the top of the page before even applying.

I should also mention my forums use sql for storing so im assuming im gonna have to alter or insert a table somewhere.....man i didnt think it would be this complicated, damnit. IT SEEMS SO SIMPLE.

snkclan.nuclearfallout.net is my site if that helps it all, ill keep messing with the code

EDIT: Tried switching it around, no dice.

Will take me to registered.php. It seems to me that there is nothing telling it to print or post on that page, and to actually post in there i think i require a mysql table.

I'd rather it just print the person's name on registered.php along with everyone else who does this, nothing fancy, just to get it to print their entry in the form

hatz
08-09-2007, 05:23 AM
fyi this kid above is spamming, that website is bs.

Still workin on the problem if anyone can help, thanks. Perhaps ill try in the sql section as well cuz i think i need that too.

Len Whistler
08-09-2007, 10:42 AM
This is a simple form that displays field output on same page. It should give you an idea of what to do.

index.php


<html>
<body>
<?php
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];

echo "Thanks for applying $first_name $last_name";
?>
<br />
</table><br />
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name: <input type="text" size="30" maxlength="30" name="first_name" /><br /><br />
Last Name: <input type="text" size="30" maxlength="30" name="last_name" /><br /><br />
<input type="submit" value="submit" name="submit" /><input type="reset" value="clear" />
</form>
</body>
</html>

mcjwb
08-09-2007, 11:01 AM
I'd rather it just print the person's name on registered.php along with everyone else who does this, nothing fancy, just to get it to print their entry in the form

It sounds like you want it to print a list of names from everyone who has submitted your form, is this correct?
To do this you need to store the submitted name somewhere on your server, whether that be a database, an xml file or a text file, otherwise when you leave the page the submitted name will be lost. Your actual html file never gets modified.

So as well as storing the submitted names, you will also need a method of retrieving the names and displaying them.

It is not a trivial matter, but once you are familiar with php,mysql and html it becomes second nature:) .

You also have the complication of learning and using the phpbb template system, which changes how you would normally do things in php.

To get back to your problem, if we ignore the fact you are using phpbb for now, just to demonstrate how forms and php work. Modify your registered.php file to simlpy read:


<?php
if (isset($_POST["FirstName"])) {
$reply = $_POST["FirstName"];
echo "Thanks for applying, ".reply;
}
else{
echo "Form not submitted.";
}
?>
Now if you go ahead and submit your form you should see "Thanks for applying, TheNameYouEntered" without the quotes and TheNameYouEntered replaced with the value you put in the text field.
Now if you go to the address bar in your browser it should say .../registered.php, click on it and press enter.
Now the page should change to say "Form not submitted.";

Hopefully this should help you understand the relationship between html forms and php.

Now all you have to do is save the submitted name to a database or something and write the code to retrieve it!

usik
08-09-2007, 11:01 AM
This is a simple form that displays field output on same page. It should give you an idea of what to do.

index.php


<html>
<body>
<?php
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];

echo "Thanks for applying $first_name $last_name";
?>
<br />
</table><br />
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name: <input type="text" size="30" maxlength="30" name="first_name" /><br /><br />
Last Name: <input type="text" size="30" maxlength="30" name="last_name" /><br /><br />
<input type="submit" value="submit" name="submit" /><input type="reset" value="clear" />
</form>
</body>
</html>


Just following on from Whistler, nice example btw, you just better make sure that the $_POST variables are set and not empty, else errors will appear on your page, like variable not set and it would echo the thankyou message before you have submited anything. Also if $PHP_SELF isn't working for you, you can easily just put the name of the current script in the action area or leave it blank both methods will post it to itself.



<html>
<body>
<?php
if(isset($_POST["first_name"]) && $_POST["last_name"]))
{
if(empty($_POST["first_name"]) || empty($_POST["last_name"]))
{
die("Invalid submission!");
}
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
echo "Thanks for applying $first_name $last_name";
}
?>
<br />
</table><br />
<form method="post" action="">
First Name: <input type="text" size="30" maxlength="30" name="first_name" /><br /><br />
Last Name: <input type="text" size="30" maxlength="30" name="last_name" /><br /><br />
<input type="submit" value="submit" name="submit" /><input type="reset" value="clear" />
</form>
</body>
</html>

hatz
08-09-2007, 11:50 PM
Alright, thanks. It seems to be working better now. But, it is still not displaying the name on the page after submitted. And also, how would i use a txt file to save the names and display it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum