Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable problem

Status
Not open for further replies.

veronika1982

Technical User
May 15, 2006
6
YU
hi all!
I got the page registration.php and another page save.php .
From registration.php if Someone insert his name and other infos,how could link that $name variable to save.php page to INSERT INTO ???

please help someone
 
I got the page registration.php and another page save.php .
From registration.php if Someone insert his name and other infos,how could link that $name variable to save.php page to INSERT INTO ???

First of all, I'm not quite sure I understand your question.
Your user registers on the page registration.php.
Then you want save.php to store the information in the database?

If so, it's quite simple.

On submitting page:
Code:
<form name="foo" action="adduser" method="post">
Username: <input type="text" name="username" value="<?=$_POST['username']?>" /><br />
Password: <input type="password" name="password" value="<?=$_POST['password']?>" />
</form>

On saving-page:
Code:
<?php
if (!empty($_POST['username']) && !empty($_POST['password'])) {
// your code here
}
else {
// do something else, as password or username was missing!
}
?>

ps. remember to secure your variables, before they come near your db.:

I would also trim the strings of whitespace, with trim() and maybe also run strip_tags.
ref.:

Good luck in your project.

Olav Alexander Mjelde
Admin & Webmaster
 
actuali I do something like this this! This is page registration.php

<html>
<head>
<title>registration</title>
<body>

<form action="save.php" method="POST">
<table>
<tr>
<td width=90><span class=style12>Ime:</td><td width=150><input type="text" name="firsname" /></td></tr>

<tr><td width=90><span class=style12>Prezime:</td><td width=150><input type="text" name="lastname" /></td></tr>

<tr><td width=90><span class=style12>Mesto Stanovanja:</td><td width=150><input type="text" name="City" /></td></tr>

<tr><td width=90><span class=style12>Adresa:</td><td width=150><input type="text" name="Adress" /></td></tr>

<tr><td width=90><span class=style12>Email:</td><td width=150><input type="text" name="Email" /></td></tr>

<tr><td width=90><span class=style12>Korisnicko Ime:</td><td width=150><input type="text" name="username" /></td></tr>

<tr><td width=90><span class=style12>Lozinka:</td><td width=150><input type="password" name="password" /></td></tr>
</span>

</tr></table>


<input type="submit" value="go" name="btSubmit" />
<input type="reset" value="reset" name="btReset" />


</form>
</body>
</html>

$firsname
$lastname
$City
$Adress
$Email
$username
$password

now i got a page save.php something like this

<html>
<head>
<title>save</title>
</head>
<body>



<?php




require 'DB.php';

$db = DB::connect('mysql://root:mad:localhost/test');

if(DB::isError($db)) {die("Can't connect: " .$db->getmessage());

/*
*
*$q = $db->query("CREATE TABLE registration(
*F_name INT,
*L_name INT,
*C_ity VARCHAR(255),
*Aders_s INT,
*Emai_l INT,
*user_name INT,
*pass_word INT)");
*/

$q=$db->query(INSERT INTO regitracija

Values( ??????????????????????????????????? )");

?>
</body>
</html>

HOW CAN I LINK ANY OF THE VARIABLE FROM PAGE registration.php in to PAGE save.php to INSERT INTO
 
First, you have to write code that is compatible with web applications. Your script outputs HTML and then immediately tries to use input from that form. This would likely be the way a console app works, but not a web app.

Web applications are discontinuous:[ol][li]The script runs, producing output, then quits.[/li][li]The browser, which has captured the HTML, produces a web page with which the user interacts.[/li][li]The browser then can send data to a script on the web server.[/li][/ol]

One HTTP-compatible method is to have your HTML in one file and your program code in another. The HTML <Form> tag is configured to submit to the script.

Another method is to have a single script produce both the form and process the data. This looks like what you are trying to do. You just have to keep in mind that your script will have to run twice: once to produce the blank form and a second time to process the input. Generally, when I am writing a single two-role script, I create a script of the general format:

Code:
<?php
//first, check whether a form was submitted
if (isset($_POST['some_field_from_the_form']))
{
  //we have input, let's proccess it
  if (is_valid_input_from_form())
  {
    //produce "success" web page or send user
    //to another page using header('Location...')
  }
  else
  {
    //reproduce the form, but with an error
    //message describing what the user did wrong
    //and the fields pre-filled-in with the user's
    //last-submitted data
  }
}
else
{
  //we have no input, so we output our blank form
}
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
I believe what Veronika is asking is how to access the values form the from.

If that is the case, then taking a part of your form as an example:
Code:
...
<form action="save.php" method="POST">
<table>
<tr>
<td width=90><span class=style12>Ime:</td><td width=150><input type="text" name="[red]firsname[/red]" /></td></tr>

<tr><td width=90><span class=style12>Prezime:</td><td width=150><input type="text" name="[red]lastname[/red]" /></td></tr>
...

TRhe contents of the texboxes whose names i highlited in red would be found in the $_POST['[red]firstname[/red]'] and $_POST['[red]lastname[/red]'] variable.
If they were submitted.

You should however head Sleipnir's advice and check first wether something has been submitted before attempting to use it.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It's not a question of accessing the right values. It's a question of whether the values will be there at all or not.

The script veronika1982 has submitted will never work. It requires that the script output the HTML and then wait for user input. Web scripts can't do that. Until the script is run a second time by submitting data to the script, the values simply won't be there.

The reason my suggested script contains the conditional:

if (isset($_POST['some_field_from_the_form']))

is not to test that a variable exists before using it. It's there to detect that a necessary HTML field value has been submitted. If the value has not been submitted, the script assumes this is the first time the script has been run and outputs a blank form.

If a user fills in the form and submits, the field value tested for will exist, the script will assumes the form as been submitted and tries to process that data. Every test for the correctness of a value would, indeed, test for the presence of that value before trying to use them, but that functionality does not appear in the pseudocode I posted.



Want the best answers? Ask the best questions! TANSTAAFL!
 
That's what I meant Something has to be submitted. The way veronika has it it will produce countless errors and warnings.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Play with this:

Code:
<?php
session_start();

if ((!empty($_POST['username']) && !empty($_POST['password'])) && $_POST['do'] == "add") {
// your code here
$_SESSION['unam'] = $_POST['username'];
$_SESSION['upass'] = $_POST['password'];
//unset($_POST['username']);
//unset($_POST['password']);

echo "username: {$_SESSION['unam']} - Password: {$_SESSION['upass']}
<form name=\"foo\" action=\"?\" method=\"post\">
<input type=\"submit\" value=\"modify\" action=\"submit\" />
</form>
";
}
else {
echo "
<form name=\"foo\" action=\"?\" method=\"post\">
Username: <input type=\"text\" name=\"username\" value=\"{$_SESSION['unam']}\" /><br />
Password: <input type=\"password\" name=\"password\" value=\"{$_SESSION['upass']}\" />
<input type=\"hidden\" name=\"do\" value=\"add\" />
<input type=\"submit\" value=\"add\" action=\"submit\" />
</form>";
}
?>

Olav Alexander Mjelde
Admin & Webmaster
 
yes vacunita I mean that u wrote uphere!

so if I want to link some variable from other page i need to
use $something=$_POST['Some_variable_from_other_page'];

right????

I tryed and it works
 
so if I want to link some variable from other page i need to
use $something=$_POST['Some_variable_from_other_page'];

why do that?
You can use the $_POST['some_variable']

eg:
Code:
echo $_POST['some_variable'];

However, I know I mentioned it above, but please do yourself the favour and implement this:

mysql injection is less than fun, if you encounter it.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top