homepage Welcome to WebmasterWorld Guest from 54.227.160.102
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Displaying data from a form
Trying to display form data on the screen
Adam5000

5+ Year Member



 
Msg#: 4207011 posted 3:41 am on Sep 26, 2010 (gmt 0)

Greetings all

I'm trying to validate user input into a form by using PHP. The data entered into the fields will be put into a MySql database. And I read this on the W3school's website.

"You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error."

So I'm trying to post the form to itself and see what the page looks like with both the form and a message on it.

But I'm not having any luck. Below is the code I'm using.

Help!

<html>
<head>
<title>Display field</title>
</head>

<body>

<?php

function display_field()
{
echo $_POST["user_name"];
}

?>

<form action="display_field.php" method="post">

Username: <input type="text" name="user_name">
<input type="submit" value="Submit" onClick="display_field()">

</form>

</body>
</html>

 

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4207011 posted 4:14 pm on Sep 26, 2010 (gmt 0)

You need to call the PHP function you have display_field from somewhere. Or you need to fetch the variables from the form for example:

<?php
$user_name = isset($_POST['user_name'])?preg_replace("/[^0-9a-z]+/i", '', $_POST['user_name']):'';
if( $user_name != $_POST['user_name']) $user_name = 'Error Invalid Username';
?>

<html>
<head>
<title>Display field</title>
</head>

<body>
<form action="display_field.php" method="post">

Username: <input type="text" name="user_name" value="<?php echo $user_name; ?>">
<input type="submit" value="Submit">

</form>

</body>
</html>

Adam5000

5+ Year Member



 
Msg#: 4207011 posted 10:26 pm on Sep 30, 2010 (gmt 0)

enigma: Thanks for your help. Fetching the variables from the form was the way that worked for me. You're terrific!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved