Welcome to WebmasterWorld Guest from 54.147.44.13

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Displaying data from a form

Trying to display form data on the screen

     
3:41 am on Sep 26, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 1, 2005
posts:369
votes: 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>
4:14 pm on Sept 26, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 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>
10:26 pm on Sept 30, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 1, 2005
posts:369
votes: 0


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