Page 1 of 1
How to built-in validation engine?
Posted: Thu Aug 10, 2006 7:22 am
by tetsuO2
patrikG | Please use Code: Select all
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I just want to built-in validation engine such as making an INPUT required,
or ensuring the value is an Integer or Date.
I coded below but it doesnot work.
Code: Select all
[thispage.php]
--------------------------------
<html>
<body>
<form action=<?php if($_POST["name"]==null||$_POST["address"]==null){echo "thispage.php";}
else{echo "nextpage.php";} ?> method='post'>
Name:<input type='text' name='name'><br>
<?php if($_POST["name"]==null){echo "You have to type your name!!";} ?>
Address:<input type='text' name='address'><br>
<?php if($_POST["address"]==null){echo "Where do you live? tell me!!";} ?>
<input type='submit'>
</form>
</body>
</html>
--------------------------------
*What i want to do is if the forms in 'thispage.php' is filled, the page goes to next page,
but if not, the page cannot go to next page and the message such as 'you have to type your name'
has to be showed up on the browser.
If you have time, please copy and paste the code and test it.
You will have some problems......
Thank you in advance.
patrikG | Please use Code: Select all
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Aug 10, 2006 8:00 am
by volka
Re: How to built-in validation engine?
Posted: Thu Aug 10, 2006 8:07 am
by blackbeard
tetsuO2 wrote:patrikG | Please use Code: Select all
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I just want to built-in validation engine such as making an INPUT required,
or ensuring the value is an Integer or Date.
I coded below but it doesnot work.
Code: Select all
[thispage.php]
--------------------------------
<html>
<body>
<form action=<?php if($_POST["name"]==null||$_POST["address"]==null){echo "thispage.php";}
else{echo "nextpage.php";} ?> method='post'>
Name:<input type='text' name='name'><br>
<?php if($_POST["name"]==null){echo "You have to type your name!!";} ?>
Address:<input type='text' name='address'><br>
<?php if($_POST["address"]==null){echo "Where do you live? tell me!!";} ?>
<input type='submit'>
</form>
</body>
</html>
--------------------------------
*What i want to do is if the forms in 'thispage.php' is filled, the page goes to next page,
but if not, the page cannot go to next page and the message such as 'you have to type your name'
has to be showed up on the browser.
If you have time, please copy and paste the code and test it.
You will have some problems......
Thank you in advance.
patrikG | Please use Code: Select all
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color][/quote]
I think the problem is that you in the action= part of the form, you need to change the string that is echo'd out to make that the string contains quotation marks.
Try this:
Code: Select all
[thispage.php]
--------------------------------
<html>
<body>
<form action=<?php if($_POST["name"]==null||$_POST["address"]==null){echo ""thispage.php"";}
else{echo ""nextpage.php"";} ?> method='post'>
Name:<input type='text' name='name'><br>
<?php if($_POST["name"]==null){echo "You have to type your name!!";} ?>
Address:<input type='text' name='address'><br>
<?php if($_POST["address"]==null){echo "Where do you live? tell me!!";} ?>
<input type='submit'>
</form>
</body>
</html>
--------------------------------
Posted: Thu Aug 10, 2006 11:18 am
by tetsuO2
volka,
thank you for quick replay.
I read the tutorial but for me it is difficult to undersand because i have
never learned php with CLASS. I have to study that but for now it is quite difficult.
Do you know any other tutorial about the basic validation? if you know, please let me know.
blackbread,
Thank you for quick reply.
i tried your code but the result was the same thing....
if you have any other solutiona about this, please let me know.
The problem is;
1. when i open 'thispage.php', the error message 'you have to type your name!!' and 'where do you live' are
already showed up on the browser. cos when i open 'thispage.php', the value'$_POST["name"]' and '$_POST["address"]'
is NULL. that is why i got this problems.
2. Even though i do not fill the forms, the page goes to 'nextpage.php'. the page is supposed to be the same page if i donot fill in the form.
Re: How to built-in validation engine?
Posted: Thu Aug 10, 2006 11:25 am
by blackbeard
Ok, maybe this:
Code: Select all
[thispage.php]
--------------------------------
<html>
<body>
<form action=<?php if(trim($_POST["name"]) == "" || trim($_POST["address"]) == "" || is_null($_POST["name"]) || is_null($_POST["address"])){echo "\"thispage.php\"";}
else{echo "\"nextpage.php\"";} ?> method='post'>
Name:<input type='text' name='name'><br>
<?php if($_POST["name"]==null){echo "You have to type your name!!";} ?>
Address:<input type='text' name='address'><br>
<?php if($_POST["address"]==null){echo "Where do you live? tell me!!";} ?>
<input type='submit'>
</form>
</body>
</html>
--------------------------------
Posted: Thu Aug 10, 2006 4:28 pm
by tetsuO2
Blackbeard,
Thank you over and over again.
but your code still does not work....
At first when i donot type anything and then click submit button, the page does not go
the next page. that is fine.
But even when i type name and address and then click submit button,
the page does not go the nextpage.php. that is a problem.
if i type name and address and then click submit button, the page should go to the nextpage.php.
BUT!!!!
After that, When i click submit button, the page goes to the next page even though i did not
type anything. Do you get what i mean? If possible, I' d like you to try your code and then
you realize what i want to say.
Sorry my English is not good so i am afraid that people cannot get what i want to say.
Thank you.
Posted: Thu Aug 10, 2006 7:04 pm
by blackbeard
Duh, it just hit me with what's happening.
When the page is first loaded, your $_POST variables are null, which means the action= attribute is going to be "thispage.php". When the user hit's submit the first time, it going to go to thispage.php.
Now, when the page is loaded the second time with the $_POST variables set, if they are not set to an empty string, then the action= will be set to "nextpage.php". This is why it works on the second time around.
To honest, you'd be better off using Javascript to do the form validation. If you want, I can show you.
Posted: Thu Aug 10, 2006 7:20 pm
by Ollie Saunders
To honest, you'd be better off using Javascript to do the form validation. If you want, I can show you.
Yep and just tell me what the URL to the site is so I can hack it to pieces
I'm sorry, that might have sounded rude. What I am trying to say is that JavaScript is easily bypassed, in fact you can completely bypass the form entirely and send any data to the server, any n000b script kiddy can and will do this. Always validate your data server. If you want client side validation you should do this in addition to server side validation.
Posted: Thu Aug 10, 2006 7:22 pm
by tetsuO2
Thank you , blackbeard.
I got what you mean.
Actually Javascript seems to be easier to make this kind of validation and
i have some Javascript documents about this so probably i can make it by javascipt.
but I just want to know how i should code for that by PHP.
so All i can do is to wait until somebody figure out this problem or try to figure out by myself.
If you figure out, please let me know. and if i figure out, i post here.
again Thank you, blackbeart!!
Posted: Thu Aug 10, 2006 7:47 pm
by blackbeard
To code it for php, you'd need to reference the two pages back to each other.
For the first page:
Code: Select all
<html>
<body>
<form action="nextpage.php" method="POST">
Name:<input type='text' name='name'><br>
Address:<input type='text' name='address'><br>
<input type='submit'>
</form>
</body>
</html>
For the second page:
Code: Select all
if (isset($_POST['name']) && trim($_POST['name']) != "" && isset($_POST['address']) && trim($_POST['address'] != "")) {
/* Process the form */
}
else {
header = ("Location: ./thispage.php");
exit;
}
You'll have to make sure I got the if statement right with the parenthesis *sp.
Good luck with it.
Posted: Thu Aug 10, 2006 8:12 pm
by Ollie Saunders
Sorry to upstage you blackbeard but there are just too many things wrong with the code you posted.
Code: Select all
<?php
if (!empty($_POST)) {
// if you wanna trim everything, I usually do.
$_POST = array_map(create_function('$v', 'return trim($v);'), $_POST);
}
?>
<! -- ensure you use a doctype -->
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="post">
<label for="txtName">Name:</label>
<input type="text" name="txtName" id="txtName" /><br />
<label for="txtAddress">Address</label>
<input type="text" name="txtAddress" id="txtAddress" /><br />
<button type="submit" name="submitButton" id="submitButton">Submit</button>
</form>
</body>
</html>
Posted: Fri Aug 11, 2006 5:58 am
by JayBird
blackbeard wrote:To honest, you'd be better off using Javascript to do the form validation. If you want, I can show you.
For the love of God, WHATEVER you do, DO NOT do this in Javascript

Posted: Fri Aug 11, 2006 6:28 am
by volka
Pimptastic wrote:For the love of God, WHATEVER you do, DO NOT do this in Javascript

agreed. The decisive validation must be server side.
You might add a client side (javascript) validation for the user's convenience but it must never be authoritative.