Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-17-2005, 06:44 AM
WebProWorld Pro
 

Join Date: May 2004
Location: Sydney
Posts: 146
ozchris RepRank 0
Default Form validation: PHP or Javascript?

Hi.

I'm still at beginner mode for PHP and Javascript, working out of books.

If I have a form on a page, and I want to validate the 2 input fields, and display the result in a third field, is it best to use Javascript or PHP?

The rest of the site is already using PHP for menu includes.

With PHP, how do you make the result stay on the same page? So far I've only practised pages that go to a new page when you hit submit.

What percentage of people/browsers would have Javascript disabled? - is it generally worth excluding Javascript function if there is an alternative work around?

ta Chris
Reply With Quote
  #2 (permalink)  
Old 04-17-2005, 01:00 PM
mushroom's Avatar
WebProWorld Veteran
 

Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 351
mushroom RepRank 0
Default Re: Form validation: PHP or Javascript?

Quote:
Originally Posted by ozchris
If I have a form on a page, and I want to validate the 2 input fields, and display the result in a third field, is it best to use Javascript or PHP?

With PHP, how do you make the result stay on the same page? So far I've only practised pages that go to a new page when you hit submit.

What percentage of people/browsers would have Javascript disabled?
1. Use php and keep it server side.
2. refer page back to itself and use if (isset($var1) and isset($var2)) {to change output:}
3. http://www.w3schools.com/browsers/browsers_stats.asp
__________________
Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.

Both Security and SuSe start with "S". www.oldslides.com
Reply With Quote
  #3 (permalink)  
Old 04-17-2005, 08:52 PM
WebProWorld Veteran
 

Join Date: Nov 2003
Location: Castle Rock, CO
Posts: 494
Corey Bryant RepRank 0
Default

I agree - the more server side coding you can use - the less chances you have something worrying about wiht the way the browser will (not) handle your coding.
Reply With Quote
  #4 (permalink)  
Old 04-18-2005, 12:20 AM
WebProWorld Pro
 

Join Date: May 2004
Location: Sydney
Posts: 146
ozchris RepRank 0
Default

Thanks mushroom.

I found onlamp.com, which had a great example on what you suggest.

I have put my first version at
http://womens-health-and-fitness.com...mass-index.php

Problems so far:
1. It seems to reset the input values to blanks when it redisplays the screen.
2. When you try to refresh the screen, you get the message 'cannot refresh, retry or cancel'. Is that normal with forms calling the same page?
3. I couldnt test this offline. I have e-novative's WAMP environment on my PC to test PHP, and this exact code would NOT process the isset command offline. I had echos coming out of my seat trying to see why it wouldn't go into my if statement. After 2 hours I uploaded it and it worked. Is this normal for offline PHP environments?

I put the php code immediately above the form, as follows:

<?

$bminum = "";
$bmitext = "";

if (isset($metric_bmi_entered)) {
$height = $_POST["bheight"];
$weight = $_POST["bweight"];

$squareheight = $height * $height;
$bmi = $weight / $squareheight;

$bminum = sprintf ("%1.2f", "$bmi");

if ($bminum < 18.5) {
$bmitext = "underweight"; }
elseif ($bminum < 25) {
$bmitext = "normal" ; }
elseif ($bmitext < 30) {
$bmitext = "overweight"; }
else { $bmitext = "obese";}
}
?>

I spent time looking at the php switch command, but couldn't see how to put a range of values, or >< in the case statement. Definite beginner.

ta Chris
Reply With Quote
  #5 (permalink)  
Old 04-18-2005, 02:52 AM
ADAM Web Design's Avatar
WebProWorld 1,000+ Club
 

Join Date: Dec 2003
Location: Toronto, Ontario, Canada
Posts: 2,217
ADAM Web Design RepRank 0
Default

I would suggest "both".

This would give you the benefit of reducing calls to your server for form validation when it isn't necessary (i.e. when the user has Javascript enabled), while providing a backup plan in case someone has it disabled.
Reply With Quote
  #6 (permalink)  
Old 04-18-2005, 09:17 AM
paulhiles's Avatar
WebProWorld 1,000+ Club
 

Join Date: Jul 2003
Location: UK
Posts: 2,803
paulhiles RepRank 0
Default Combine both client-side and server side programming

I would agree with Adam here, client-side Javascript is both quick and extremely effective for the vast majority of site visitors.

However, if a visitor chooses not to enable Javascript for whatever reason, then there should be server-side error-handling to catch these users too!

Paul
Reply With Quote
  #7 (permalink)  
Old 04-21-2005, 01:22 PM
WebProWorld Member
 

Join Date: Nov 2004
Posts: 32
imported__sam_ RepRank 0
Default

Quote:
Originally Posted by ozchris
3. I couldnt test this offline. I have e-novative's WAMP environment on my PC to test PHP, and this exact code would NOT process the isset command offline. I had echos coming out of my seat trying to see why it wouldn't go into my if statement. After 2 hours I uploaded it and it worked. Is this normal for offline PHP environments?
Hello Chris,

The reason for that is most likely a PHP setting called REGISTER_GLOBALS, see http://at2.php.net/register_globals

In order not to rely on this setting, use $_POST['metric_bmi_entered'] instead of $metric_bmi_entered.

Quote:
Originally Posted by ozchris
I spent time looking at the php switch command, but couldn't see how to put a range of values, or >< in the case statement.
That's not possible in PHP. You can do this, but that's not really the intended use of switch:

Code:
switch (TRUE) {
  case ($bminum < 18.5):
  ...
}
best regards

- Sam
__________________
Software-Pointers.com - Directory of Software Development Sites - Free submission.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0