Do not use on any
shared computer
August 29, 2008 08:10pm pdt
 
[x]
Attachment Details

Dynamic required radio button corresponding with text field

I have an email text field and a phone text field in an html form, I also have a radio button that says "How would you prefer to be contacted?".

How can I code this so that when the radio button (either phone or email) is marked - the corresponding text field (either phone or email) will be required. But ONLY the one field corresponding to the selected radio button, the other would be optional.

I am using a basic PHP email form and all the code is attached.

Thank you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
<?php
$solution = (isset($_POST['solution']) && !empty($_POST['solution'])) ? mysql_escape_string(strip_tags($_POST['solution'])) : "";
$name = (isset($_POST['name']) && !empty($_POST['name'])) ? mysql_escape_string(strip_tags($_POST['name'])) : "";
$email = (isset($_POST['email']) && !empty($_POST['email'])) ? mysql_escape_string(strip_tags($_POST['email'])) : "";
$phone = (isset($_POST['phone']) && !empty($_POST['phone'])) ? mysql_escape_string(strip_tags($_POST['phone'])) : "";
$contact_method = (isset($_POST['contact_method']) && !empty($_POST['contact_method'])) ? mysql_escape_string(strip_tags($_POST['contact_method'])) : "";
 
$allOK=true;
 
 
if("Submit" == strval($_POST["submit"]))
{
	if( ""==$solution )
	{
		$allOK=false;
	}
	if( ""==$name )
	{
		$allOK=false;
	}
}
 
if($allOK && "Submit"==strval($_POST["submit"]) )
{
$todayis = date("l, F j Y, g:i a") ;
 
$subject = "Purchase Page Form";
 
$message = "
 
A new message has been received from the Purchase Page.
 
Contact information:
Solution:    $solution
Name:    $lname
Email:    $email
Company:    $phone
Contact Method:    $contact_method
";
 
 
$from = "From: $email\r\n";
 
 
mail("BLOCKED_EMAIL_ADDRESS", $subject, $message, $from);
 
}
 
?>
	
    <!--START Top Banner-->
	<div id="header">
        <?php include 'header_spotlight.php'; ?></div>  
    <!--END Top Banner-->
    
    <!--START Menu-->
    <!--Height must be 30px more then Body height-->
    <div style="height:810px;" id="menu">
        <?php include 'includes/left_nav.php'; ?>
    </div>
    <!--END Header-->
    
    <!--START Body-->
            <p align="center" class="red_title">
            <?php
				if(!$allOK)
				{
				  echo("<div class='red_title' align='center'>Please provide all the required fields.</div>");
				}
				else
				{
				  echo("<div class='red_title' align='center'>Thank you, your message has been sent.</div>");
				}
			?>
            </p>
            <div style="background-color:#CCCCCC; width:300px; padding-top:20px; margin-left:auto; margin-right:auto; height:400px;">
            <p align="center" class="title bold">Want someone to contact you?</p>
            	<form id="contact" name="contact" method="post" action="purchase_send.php">
            	<table align="center" class="body" width="200">
            	  <!--DWLayoutTable-->
                    <tr>
                      <td colspan="2"><input type="radio" name="solution" value="auto_truck" id="solution_0" />
                      Auto & Light Truck</td>
                    </tr>
                    <tr>
                      <td colspan="2"><input type="radio" name="solution" value="owner_operator" id="solution_1" />
                      Owner / Operator</td>
                    </tr>
                    <tr>
                      <td colspan="2"><input type="radio" name="solution" value="truck_fleet" id="solution_2" />
                      Truck Fleet</td>
                    </tr>
                    <tr>
                      <td colspan="2"><input type="radio" name="solution" value="heavy_equipment" id="solution_3" />
                      Heavy Equipment</td>
                    </tr>
                    <tr>
                      <td colspan="2"><p>
                          <input type="radio" name="solution" value="other" id="solution_3" />
                      Other</p><br />                        </td>
                    </tr>
                    <tr>
                      <td colspan="2" align="right"><div align="left">Name:&nbsp;
                          <input type="text" name="name" id="name" />
                      </div></td>
                    </tr>
                    <tr>
                      <td colspan="2" align="right"><div align="left">Phone:
                        <input type="text" name="phone" id="phone" />
                      </div></td>
                    </tr>
                    <tr>
                      <td colspan="2" align="right"><div align="left">Email:&nbsp;
                        <input type="text" name="email" id="email" />
                      </div></td>
                    </tr>
                    <tr>
                      <td colspan="2" align="right"><div align="left" class="small_body"><br />
                        <p>How would you prefer to be contacted?</p>
                      </div>                      </td>
                    </tr>                      
                    <tr>
                      <td width="63" height="23" valign="top"><label>
                        <input type="radio" name="contact_method" value="contact_by_phone" id="contact_method_0" />
                      Phone</label></td>
                    <td width="121" rowspan="2" align="right" valign="bottom"><input type="submit" name="button" id="button" value="Submit" /></td>
                    </tr>
                    <tr>
                      <td height="23" valign="top"><label>
                        <input type="radio" name="contact_method" value="contact_by_email" id="contact_method_1" />
                      Email</label></td>
                    </tr>
                </table>
                </form>
         </div>
    <!--END Body-->
<?php include 'includes/footer.php'; ?>
Start your free trial to view this solution
Question Stats
Zone: Web Development
Question Asked By: glopicon
Question Asked On: 06.03.2008
Participating Experts: 2
Points: 500
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by glcummins

Rank: Sage

Expert Comment by glcummins:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by mrdany
Expert Comment by mrdany:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by mrdany
Expert Comment by mrdany:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628