help! how to validate when i press submit

Links for php scripts

Moderators: macek, egami, gesf

darrenclaveria
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Sep 07, 2012 11:11 am

help! how to validate when i press submit

Postby darrenclaveria » Fri Sep 07, 2012 11:16 am

<?php
$val = $_GET['yr'];
if ( $val == "" || $val == null )
{
echo "Pls input numbers";
}
else
{
echo "";
}
?>

<html>
<head><title>Leap Year</title>
</head>
<body>
Leap Year Calculation

<form name="form" method="get" action="">
<table border="0">
<tr><td>Enter Year: <input type="text" maxlength="4" name="yr">
<tr><td><center><input type="submit" value="submit">
</form>
</body>
</html>


pls help. i wanna validate only when i press submit. i dont wanna use JS as possible. what happened to my script above is when i load the main page, it validate already the form. I want only to validate when i press the "submit" button.
THANK YOU IN ADVANCE! :)

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 283
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: help! how to validate when i press submit

Postby freshnet » Sun Sep 09, 2012 4:13 am

If you want to validate only when you submit the form, it's probably better to use $_POST variables, and perform a check on each one.

dering
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Sep 18, 2012 5:59 am

Re: help! how to validate when i press submit

Postby dering » Tue Sep 18, 2012 6:22 am

As a fellow beginner programmer, I have just begun to realize the power of $_POST. It is really great when you can make it work. If you get a true on empty($_POST) then the user has not yet submitted the form. To refer to the post you have to keep in mind it is an array. In your case, I think you will want to deal with the variable $_POST["submit"] for evaluation. Also, I have found it useful to include the command print_r($_POST); at the beginning of my scripts during development to output the available variables.


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest