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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating numeric input 1

Status
Not open for further replies.

rs51

Technical User
Oct 13, 2001
163
PT
i wrote this function:
function numero($limite, $normalizado)
{
$normalizado = $_POST['numero'];
$normalizado = str_replace(' ', "", $normalizado);
if (!isset($_POST['numero'])) {
$resposta = '<p>...não escreveu...</p>';
return $resposta;
}
if (isset($_POST['numero'])) {
if ((strlen($normalizado)==$limite) & (ereg(&quot;^[0-9]+$&quot;, $normalizado))) {
$resposta = &quot;<p>ok!</p>&quot;;
return $resposta;
}
else {
$resposta = '<p>...Fez porcaria! Veja o que escreveu: <br /><strong>'.$_POST['numero'].'</strong></p>';
return $resposta;
}
}
return $resposta;
}
Now what i want is that if one submits whitout writing in textbox, the message:$resposta = '<p>...não escreveu...</p>'; works...and it doesnt.
Why?
thanks in advance
 
thanks for answering
i do want the limit fixed, not <=.
anyway the error doesnt come from there.
i improved the function and the php+html like this:
php+html:
<form action = &quot;<?=$_SERVER['PHP_SELF']?>&quot; method = &quot;post&quot;>
<p><input type = &quot;text&quot; maxlength = &quot;4&quot; name = &quot;numero&quot; /></p>
<p><input type = &quot;submit&quot; value = &quot;'bora lá&quot; /></p>

</form>

<?php
include ('f_numero.php');
echo numero(4);
?>

the function:
<?php

function numero($limite)
{
if (isset($_POST['numero']))
{
$normalizado = str_replace(' ', &quot;&quot;, $_POST['numero']);

if ((strlen($normalizado)==$limite) & (ereg(&quot;^[0-9]+$&quot;, $normalizado)))
{
$resposta = &quot;<p>ok! Introduziu: <br /><strong>&quot;.$normalizado.&quot;</strong></p>&quot;;
return $resposta;
}
else
{
$resposta = '<p>...Veja o que introduziu: <br /><strong>'.$normalizado.'</strong></p>';
return $resposta;
}
}
return $resposta;

}

?>

now, when i open the php/html page, i get this message:
Notice: Undefined variable: resposta in C:\Programas\Apache Group\Apache2\htdocs\teste\f_numero.php on line 20

line 20 is last code line in function;
how can i solve it?

note: i know this message, from error_reporting(E_ALL)is due to not having nothing submited yet, but i tried if (!isset...), but cant get it working ok....

 
i guess so
here's the entire php/html page:

<?php
echo('<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>');
error_reporting(E_ALL);
?>

<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;
<html xmlns=&quot; xml:lang=&quot;pt&quot; lang=&quot;pt&quot;>
<head>
<title>normalizado</title>
<meta http-equiv=&quot;Content-type&quot;
content=&quot;text/html; charset=ISO-8859-1&quot; />
<link rel=&quot;Stylesheet&quot; type=&quot;text/css&quot; href=&quot;xml.css&quot; />
</head>
<body>

<p><strong>Parte 0.0:<br /></strong> época</p>
<form action = &quot;<?=$_SERVER['PHP_SELF']?>&quot; method = &quot;post&quot;>
<p><input type = &quot;text&quot; maxlength = &quot;4&quot; name = &quot;numero&quot; /></p>
<p><input type = &quot;submit&quot; value = &quot;'bora l&aacute;&quot; /></p>

</form>

<?php
include ('f_numero.php');
echo numero(4);
?>

</body>
</html>
 
the function is called here:
<?php
include ('f_numero.php');//THE FUNCTION
echo numero(4);//CALL OF THE FUNCTION
?>
 
Yep, that'll do it. The function is getting called every time the script it run -- including the first time it's run, when you only want it to produce the form.

I recommend that you make your entire script an if()...else construct.

If the script gets input, it processes it and returns HTML. If not, it just outputs the form.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
like:
if (form not submited) {
//display the form
)
else {
//then the rest with function
}
right?
 
Correct.

I've also found it easier to debug code when I don't use the style of PHP programming where I switch back and forth between interpreted mode and output mode. I just place one &quot;<?php&quot; tag at the beginning of the file and one &quot;?>&quot; tag at the end. And I use print or echo statements for all output -- both can run more than one line.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
i got it working!
the include 'f_numero.php'

<?php
function numero($limite)
{
if (isset($_POST['numero']))
{
$normalizado = str_replace(' ', &quot;&quot;, $_POST['numero']);
if ((strlen($normalizado)==$limite) & (ereg(&quot;^[0-9]+$&quot;, $normalizado)))
{
$resposta = &quot;<p>ok! Introduziu: <br /><strong>&quot;.$normalizado.&quot;</strong></p>&quot;;
return $resposta;
}
else
{
$resposta = '<p>... Veja o que introduziu: <br /><h4> '.$normalizado.'</h4></p>';
?>
<p><strong>Parte 0.0:<br /></strong> época</p>
<form action = &quot;<?=$_SERVER['PHP_SELF']?>&quot; method = &quot;post&quot;>
<p><input type = &quot;text&quot; maxlength = &quot;4&quot; name = &quot;numero&quot; /></p>
<p><input type = &quot;submit&quot; value = &quot;'bora lá&quot; /></p>
</form>
<?php
return $resposta;
}
}
return $resposta;
}
?>

the page:

<?php
echo('<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>');
error_reporting(E_ALL);
?>

<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;
<html xmlns=&quot; xml:lang=&quot;pt&quot; lang=&quot;pt&quot;>
<head>
<title>normalizado</title>
<meta http-equiv=&quot;Content-type&quot;
content=&quot;text/html; charset=ISO-8859-1&quot; />
<link rel=&quot;Stylesheet&quot; type=&quot;text/css&quot; href=&quot;xml.css&quot; />
</head>
<body>
<?php
if (!isset($_POST['numero']))
{
?>
<p><strong>Parte 0.0:<br /></strong> época</p>
<form action = &quot;<?=$_SERVER['PHP_SELF']?>&quot; method = &quot;post&quot;>
<p><input type = &quot;text&quot; maxlength = &quot;4&quot; name = &quot;numero&quot; /></p>
<p><input type = &quot;submit&quot; value = &quot;'bora lá&quot; /></p>
</form>
<?php
}
else
{
include ('f_numero.php');
echo numero(4);
}
?>
</body>
</html>

one last q:
quote:
&quot;...I switch back and forth between interpreted mode and output mode. I just place one &quot;<?php&quot; tag at the beginning of the file and one &quot;?>&quot; tag at the end. And I use print or echo statements for all output -- both can run more than one line&quot;
dont quite understand
Anyway thank you very much!
 
Anything in a PHP file between &quot;<?php&quot; and &quot;?>&quot; is interpreted by the PHP engine. Everything else is just sent to the browser. You switch back and forth between the two modes in your code.

For example, another way to write some of the code you've posted is:

Code:
<?php
error_reporting(E_ALL);

print '<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>';
print '<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;...;[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;;[/URL] xml:lang=&quot;pt&quot; lang=&quot;pt&quot;>
<head>
<title>normalizado</title>
<meta http-equiv=&quot;Content-type&quot;
content=&quot;text/html; charset=ISO-8859-1&quot; />
<link rel=&quot;Stylesheet&quot; type=&quot;text/css&quot; href=&quot;xml.css&quot; />
</head>
<body>';

if (!isset($_POST['numero']))
{
	print '<p><strong>Parte 0.0:<br /></strong> época</p>
    <form action = &quot;' . $_SERVER['PHP_SELF'] . '&quot; method = &quot;post&quot;>
      <p><input type = &quot;text&quot; maxlength = &quot;4&quot; name = &quot;numero&quot; /></p>
      <p><input type = &quot;submit&quot; value = &quot;'bora lá&quot; /></p>
        </form>';
}
else 
{
	include ('f_numero.php');
	echo numero(4);    
}

print '</body>
</html>';
?>

I find code to be more readable, and thus more maintanable if you don't have all those extra &quot;<?php...?>&quot; or &quot;<?=...?>&quot; tags in your code.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top