hacklinux

learn more share more....
i have been having this doubt lately that,when you get details from a user on a form using php.then how do i display the details on another page?and is it possible to display only one detail of that user?

help me at the earliest.thanks in advance.
 

khmadhu

change is constant!!
yes you can display the form values to another page. you should mention the action and method(POST or GET) in form. and at the other end retrieve it through $_GET or $_POST,

if you don't mention method default is GET

here is quick example..

The example below contains an HTML form with two input fields and a submit button:

form page
<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

When a user fills out the form above and click on the submit button, the form data is sent to a PHP file, called "welcome.php":

"welcome.php" looks like this:
<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>
 
OP
H

hacklinux

learn more share more....
thanks for the answer.but ya i know this.now if i link welcome.php to some other page and i need to display the "name" on that page.how to do that?
 

krishnandu.sarkar

Simply a DIGITian
Staff member
You can store that name in session.

In welcome.php, add
Code:
session_start();
to the first(top), and then do
Code:
$_SESSION['name'] = $_POST['name'];

and then on any page you can call...
Code:
echo $_SESSION['name'];

to display the name.

Or you can use cookies too.
 
OP
H

hacklinux

learn more share more....
thank you so much.now it is stored in a variable.if i store it in a database and how do i call only that particular name?hope you understood my question.
 

khmadhu

change is constant!!
now it is stored in a variable.if i store it in a database and how do i call only that particular name?hope you understood my question.


@hacklinux
what r u exactly trying to achieve..? please elaborate u r problem..
 
OP
H

hacklinux

learn more share more....
@rashmi37 i knw about the adding the data to db.for example:

1.i enter a name "varun".
2.it should get saved in the db.
3.now the name "varun" should be called from the db.


in simple words i want the user data to be saved and displayed to the user simultaneously.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Hey that was spam. Anyway tell us what you want to achieve.

Looks like you are trying some wrong way.

Still, if you want to sore the name in database and retrieve it from database...here is an example...

You need to have a table. So for eg. assume your database name is test, table name is tbltest, and the field you want to store the name is simply name.

PHP:
<?php

//Get the value from form and store it in a variable
$name = $_POST['name'];

//Connect MySQL Database
$link = mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test");

//Insert name into MySQL Database
$query = "INSERT INTO tbltest(name) VALUES('" . $name . "')";
mysql_query($query, $link) or die(mysql_error());

//Retrieve name from MySQL Database
$query = "SELECT * FROM tbltest WHERE name =" . $name;
$result = mysql_query($query, $link) or die(mysql_error());
$row = mysql_fetch_row($result);
echo $row['name'];

//Close the Connection
mysql_close($link);
?>

Now see, while fetching the value, you can fetch the whole table using SELECT * FROM tbltest. But that would fetch all the names you have inserted, which I'm sure you don't want. And the example that I showed you, the where is clause is simply vogus, but I didn't find any other way out to show you. So you'd need a where clause, which depends on what you actually want to implement.

Even the avobe example looks much vogus to me. So tell us what you want to achieve, we can suggest you better code.

This is just an example, in real life we should implement, if $link > 1, to verify whether the database have been connected or not, though die() ensures that here, then mysql_num_rows($result) > 1, to verify whether any data has been fetched or not before populating that data, to avoid errors, etc.
 
Top Bottom