Forum Moderators: coopster

Message Too Old, No Replies

if field has a value, another field must have a value.

         

tonynoriega

11:27 pm on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



to continue my quest for a proper working registration form...

i am trying to figure out how to accomplish this...

i.e.

If a user selects from a drop down list menu, that a contact is a "real estate agent" then they must enter a value for "real estate agent name"....if they dont, an alert pops up, "you selected Real Estate Agent Lead Type, you must enter a Real Estate Agent Name"

PHP_Chimp

11:50 pm on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want pop-up's then that is javascript not php, as this is a client side thing. However if you want them to submit the page then get warnings back saying that they havent filled in the form correctly then you could use something along the lines of -

if (isset($_POST['real_estate_agent']) && isset($_POST['agent_name'])) {
// form completed properly
}
else {
echo 'its not difficult...do it properly :)';
}

If you want to combine these then you need ajax, so you can have both the client and server side working together.

tonynoriega

4:40 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The PHP code you posted looks like both fields have to have a value either input or selected...

that is not my objective. i will try to clarify

The drop down menu list will have values of:
<select name"contact_type">
<option value"n/a" selected>N/A</option>
<option value"agent">Agent</option>
<option value"buyer">Buyer</option>
<option value"browsing">Just Browsing</option>
</select>

now, if they leave it as N/A, Buyer, or Just Browsing the form will process correctly.

IF they select Agent, i want to have the Agent Name field to have to be populated with a value

so im thinking it would have to be something like:
(this is not correct by any means, becuase im very new at PHP)
if{
field.contact_type == "agent"

then field.agent_name (must have a value)
}

Habtom

4:44 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You missed the '=' in there
<select name="contact_type">
<option value="n/a" selected>N/A</option>
<option value="agent">Agent</option>
<option value="buyer">Buyer</option>
<option value="browsing">Just Browsing</option>
</select>

This could be a solution:

<?php
if ($_POST['contact_type'] <> 'n/a' && $_POST['agent_type'] == '') {
echo 'Agent type can't be empty';
}