introduction screenshots faq forum blog modules components download
Please use the search function and/or read the FAQ first.

Go to Topic: PreviousNext
Go to: Message ListNew TopicSearchLog InPrint View

initialize variables



Posted by: fiorelina21
January 08, 2008 01:46AM
in the fllowing script how do i initialize the$n1 $n2 $n3 variables?
is this script correct???
thanks!!!
could u give me any tips???
PLEASE LET ME KNOW

<?php
echo "<form name=\"mod\" method=\"post\" action=".$_SERVER['PHP_SELF'].">";
?>
<p>primo numero
<input name="n1" type="text">
</p>
<p>secondo numero
<input type="text" name="n2">
</p>
<p>terzo numero
<input type="text" name="n3">
</p>
<p>
<input type="submit" name="Submit" value="calcola maggiore e minore">
</p>
</form>
<?php
if (($n1>$n2) && ($n1>$n3))
{
$mag=$n1;
}
else
{
if (($n2>$n3) && ($n2>$n1))
{
$mag=$n2;
}
else
{
$mag=$n3;
}
}
echo "MAggiore: ".$mag;
?>
Options: ReplyQuote
Posted by: Amour
January 08, 2008 06:25AM
FAQ, you must use $_POST ;)
Options: ReplyQuote
Posted by: fiorelina21
January 08, 2008 11:51AM
the fallowing


[php]
<?php
echo "<form name=\"modulo\" method=\"post\" action=".$_SERVER['PHP_SELF'].">";
?>
<p>primo numero
<input name="n1" type="text">
</p>
<p>secondo numero
<input type="text" name="n2">
</p>
<p>terzo numero
<input type="text" name="n3">
</p>
<p>
<input type="submit" name="Submit" value="calcola maggiore e minore">
</p>
</form>
<?php
echo $_POST['n1'];
echo $_POST['n2'];
echo $_POST['n3'];

if (($n1>$n2) && ($n1>$n3))
{
$mag=$n1;
}
else
{
if (($n2>$n3) && ($n2>$n1))
{
$mag=$n2;
}
else
{
$mag=$n3;
}
}
echo "MAggiore: ".$mag;

?>
[/php]

returns the fallowing errors:

Notice: Undefined index: n1
Notice: Undefined index: n2
Notice: Undefined index: n3
Notice: Undefined variable: n1
Notice: Undefined variable: n2
Notice: Undefined variable: n2 i
Notice: Undefined variable: n3
Notice: Undefined variable: n3



could u pls tell me what did i do wrong?
thank you for your patience.
Options: ReplyQuote
Posted by: Thierry
January 08, 2008 02:53PM
It's a programmation problem, not an easyphp one.
Options: ReplyQuote
Posted by: fiorelina21
January 09, 2008 11:31AM
WHAT DO U MEAN?
Options: ReplyQuote
Posted by: Lucius
January 10, 2008 08:27PM
Ciao,
questo problema ti capita perché per accedere direttamente a delle variabili provenienti da una richiesta POST o GET, come nel tuo script, dovresti abilitare l'opzione 'register_globals' nel file di configurazione php.ini, che di default è posta a 'Off' (ma è sconsigliabile).
L'ideale è invece abituarti a recuperare le variabili dagli array $_POST, $_GET o $_REQUEST (quest'ultimo contiene le variabili provenienti da entrambe i tipi di richiesta). Gli script diventano più sicuri e sono maggiormente portabili.
Di seguito lo script modificato:

== English:
You run into this problem whenever you try to access directly to variables originated by a POST or GET request. You could enable the 'register_globals' option in php.ini, set to Off by defalut, but doing so is not advisable.
The right approach is instead to access them the $_POST, $_GET and $_REQUEST (that contains both types) arrays. Scripts are more portable and safe.

<form name="mod" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<p>primo numero <input name="n1" type="text"></p>
<p>secondo numero <input type="text" name="n2"></p>
<p>terzo numero <input type="text" name="n3"></p>
<p><input type="submit" name="Submit" value="calcola maggiore e minore"></p>
</form>
<?php
$n1 = $_POST["n1"];
$n2 = $_POST["n2"];
$n3 = $_POST["n3"];

if (($n1>$n2) && ($n1>$n3))
{
$mag=$n1;
}
else
{
if (($n2>$n3) && ($n2>$n1))
{
$mag=$n2;
}
else
{
$mag=$n3;
}
}
echo "Maggiore: ".$mag;
?>

--
Luciano S.
luciano AT allforweb DOT biz
Options: ReplyQuote
Posted by: mega-squall
January 12, 2008 04:36PM
register_globals is OFF for security purposes !!!
You shall __NEVER EVER__ put it on.

Enabling it is like pluging a microwave oven before a circuit breaker because "it doesn't work otherwise".
Moreover, you should consider testing if $_POST values are set and not harmfull.

---
mega-squall
Options: ReplyQuote
Posted by: Lucius
January 11, 2008 12:27AM
In alternativa, questo metodo è più veloce ed è molto comodo quando hai dei form con parecchi campi.
==
This alternative is quicker and comes in handy when you have to handle forms with many fields.

<?php
if ($_POST["Submit"]) {
$form = array_map ("floatval",$_POST["form"]);
// in pratica anche $form = $_POST["form"] sarebbe andato bene, ma per prima cosa bisogna assicurarsi che i valori siano numerici,
// poi è sempre bene controllare l'input utente e vagliare i dati con delle funzioni (come addslashes o mysql_escape_string) che impediscano agli utenti di fare i "furbetti"

if (($form["n1"]>$form["n2"]) && ($form["n1"]>$form["n3"]))
{
$mag=$form["n1"];
}
elseif (($form["n2"]>$form["n3"]) && ($form["n2"]>$form["n1"]))
{
$mag=$form["n2"];
}
else
{
$mag=$form["n3"];
}

echo "Maggiore: ".$mag;
}
?>

<form name="mod" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<p>primo numero
<input name="form[n1]" type="text" value="<?php echo $form["n1"] ?>">
</p>
<p>secondo numero
<input name="form[n2]" type="text" value="<?php echo $form["n2"] ?>">
</p>
<p>terzo numero
<input name="form[n3]" type="text" value="<?php echo $form["n3"] ?>">
</p>
<p>
<input type="submit" name="Submit" value="calcola maggiore e minore">
</p>
</form>

--
Luciano S.
luciano AT allforweb DOT biz
Options: ReplyQuote
Posted by: mega-squall
January 12, 2008 04:35PM
He means that this forum is here to help EasyPHP users with using EasyPHP.
We are officially providing EasyPHP support, not PHP nor programming support.

This is why you are advised to ask PHP-prgoramming related questions on PHP-programming forums.
You shall get a better answer on such forums.

---
mega-squall
Options: ReplyQuote
Posted by: fiorelina21
January 08, 2008 12:18PM
it still turns me an error
Options: ReplyQuote


Go to: Message ListSearchLog In
Your Name: 
Your Email: 
Subject: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.