Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
form validation - if selected, make required
Old 01-30-2009, 02:13 PM form validation - if selected, make required
Junior Talker

Posts: 3
Name: bleeblee
Trades: 0
hi,

i'm looking to create a form that makes other fields required based on certain dropdown selections.

example

if YES is selected on this dropdown, than make the next 3 fields required.

anyone have any tips on how to go about this?

thanks so much!!
dbjn is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-30-2009, 02:30 PM Re: form validation - if selected, make required
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
test if the checkbox exists in the POST or GET variable list and check the fields, if it doesn't. ....

... don't check them.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-30-2009, 02:46 PM Re: form validation - if selected, make required
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Like what Chris said... here's an example, only with a drop-down menu
PHP Code:
<?php 
$option1 
$_REQUEST['1'] ; 
$option2 $_REQUEST['2'] ; 
$option3 $_REQUEST['3'] ;
$validate $_REQUEST['validate'] ;
                 
if(
$_POST['submit']){
    if(
$validate == 'yes') {
        if(
$option1 == '') {
            echo 
"Fill out option 1";
        } else { 
        if(
$option2 == '') {
            echo 
"Fill out option 2";
        } else { 
        if(
$option3 == '') {
            echo 
"Fill out option 3";
        } else { 
            echo 
"Everything was Validated!  You pass!";
        } 
        }
        }
    } else {
        echo 
"Nothing Validated, but you pass anyway.";
    }
}
?> 

<form method="post">
<select name="validate">
<option value="no">Don't Validate</option>
<option value="yes" selected="selected">Validate Form</option>
</select><br />
Option 1: <br />
<input type="text" name="1" />
<br />
Option 2:<br />
<input type="text" name="2" />
<br />
Option 3:<br />
<input type="text" name="3" />
<br />
<input type="submit" value="Send" class="custombutton" name="submit"/>
</form>
- Steve

Last edited by stevej; 01-30-2009 at 02:48 PM..
stevej is offline
Reply With Quote
View Public Profile
 
Old 01-30-2009, 04:06 PM Re: form validation - if selected, make required
Junior Talker

Posts: 3
Name: bleeblee
Trades: 0
awesome - thanks so much!!!!!!
dbjn is offline
Reply With Quote
View Public Profile
 
Old 01-30-2009, 10:47 PM Re: form validation - if selected, make required
gravedig2's Avatar
Junior Talker

Posts: 3
Name: Sean Richard
Trades: 0
I believe that that could also be done with Javascript, correct me if I am wrong.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
gravedig2 is offline
Reply With Quote
View Public Profile
 
Old 01-30-2009, 10:57 PM Re: form validation - if selected, make required
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Quote:
Originally Posted by gravedig2 View Post
I believe that that could also be done with Javascript, correct me if I am wrong.
I believe that is correct. However, I tend to lean towards php because it is server-side code, which is very reliable, and not client-side like JavaScript, which tends to be rather unreliable (because either a browser does not support it, or the user has it turned off).

- Steve
stevej is offline
Reply With Quote
View Public Profile
 
Old 01-31-2009, 03:44 AM Re: form validation - if selected, make required
jason_alan's Avatar
Super Talker

Posts: 100
Name: Jason
Location: Seattle, WA
Trades: 0
I always validate server-side no matter what. I get that figured out first, and usually add some javascript validation to let the user know immediately what is wrong with their input to cut down on http requests.

At the very least, validate server-side, because anyone can post anything to your server.
jason_alan is offline
Reply With Quote
View Public Profile
 
Old 02-11-2009, 09:55 AM Re: form validation - if selected, make required
Junior Talker

Posts: 3
Name: bleeblee
Trades: 0
Ok, my form sends data to a processor php file. @stevej - can I implement your code into the processor file or does it have to be present on the same page as the form? Here's the code from my processor.php. Thanks again!

PHP Code:
<?php
if(isset($_POST['submit'])) {

$name $_POST['name'];
$account $_POST['account'];
$return $_POST['return'];
$hw_a $_POST['hw_a'];
$hwq_a $_POST['hwq_a'];
$hw_b $_POST['hw_b'];
$hwq_b $_POST['hwq_b'];
$hw_c $_POST['hw_c'];
$hwq_c $_POST['hwq_c'];
$cc $_POST['cc'];
$cn $_POST['cn'];
$ac $_POST['ac'];
$comments $_POST['comments'];

$to "my email addresses";
$subject "my subject";



$body "

here's my html body

"
;

$formattedtext wordwrap($body100"\n"true);


// To send HTML mail, the Content-type header must be set
$headers 'MIME-Version: 1.0' "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";

mail($to$subject$formattedtext$headers);

include(
"thanks.php");

} else {

echo 
"blarg!";

}
?>
dbjn is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to form validation - if selected, make required
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.84611 seconds with 12 queries