0

Before anyone yells Repost, there aren't many cases like mine that I've seen.
I have a contact form that has the possibility to have a main Select option, a sub-Select option and then a sub-Select option of that one. Overall, There are 27 options to choose from and I think I could make the php give me all 27 of those and then do a "yes they selected this one but not that one" type of report, but I digress.
How do I have the php script for my contact form send only what is selected from a possible three-tiered select menu? EDIT: I'm totally willing to use something other than PHP to help facilitate this, I just need help with the PHP script as well.
Edit: To be clear, I'm try to create and populate a php script, not valdiate the form/check that all required sections are filled out.
Code

<form  action="sending.php" method="POST" enctype="multipart/form-data"> 
	<input type="hidden" name="action" value="submit"> 
	Name:<br> 
	<input name="name" type="text" value="" size="30"/><br> 
	Email:<br> 
	<input name="email" type="text" value="" size="30"/><br> 
	Service:<br> 
	<select name="service" id="service" class="service">
		<option>Select a Service</option>	
		<option value="screen" data-target="devices" id="screen">Screen Replacement</option>	   
		<option value="comp" data-target="comp" id="comp">Computer Work</option> 
		<option value="misc" data-target="misc" id="misc">Miscellaneous</option>
	</select>
	<div style="display:none" id="service-devices">
		<select name="devices" id="devices" class="devices">
			<option>Select a Device</option>	   
			<option value="iphone" data-target="iphones" id="iphone">iPhone</option>	   
			<option value="ipad"  data-target="ipads" id="ipad">iPad</option>
			<!--<option value="watch" id="watch">Apple Watch</option> -->
			<option value="android" id="android">Android</option>
		</select>
		<div style="display:none" id="devices-iphones">
			<select name="iphone" id="iphone" class="iphone">
				<!--<option value="iphone6s" id="iphone6s">iPhone 6S</option> -->
				<!--<option value="iphone6splus" id="iphone6splus">iPhone 6S Plus</option>-->
				<option>Select a Model</option>
				<option value="iphone6" id="iphone6">iPhone 6</option>
				<option value="iphone6plus" id="iphone6plus">iPhone 6 Plus</option>
				<option value="iphone5s" id="iphone5s">iPhone 5S</option>
				<option value="iphone5c" id="iphone5c">iPhone 5C</option>
				<option value="iphone5" id="iphone5">iPhone 5</option>
				<option value="iphone4s" id="iphone4s">iPhone 4S</option>
				<option value="iphone4" id="iphone4">iPhone 4</option>
			</select>
		</div>
		<div style="display:none" id="devices-ipads">
			<select name="ipad" id="ipad" class="ipad">
				<option>Select a Model</option>
				<option value="ipadmini3" id="ipadmini3">iPad Mini 3</option>
				<option value="ipadmini2" id="ipadmini2">iPad Mini 2</option>
				<option value="ipadair" id="ipadair">iPad Air</option>
				<option value="ipad4" id="ipad4">iPad 4</option>
				<option value="ipad3" id="ipad3">iPad 3</option>
				<option value="ipadmini" id="ipadmini">iPad Mini</option>
				<option value="ipad2" id="ipad2">iPad 2</option>
			</select>
		</div>
	</div>
	<div style="display:none" id="service-comp">
		<select name="compwork" id="compwork" class="compwork">
			<option value="desktopcreation" id="desktopcreation">Desktop Creation</option>
			<option value="desktopbuild" id="desktopbuild">Desktop Build</option>
			<option value="hardwareupgrades" id="hardwareupgrades">Hardware Upgrades</option>
			<option value="datarecovery" id="datarecovery">Data Recovery/Transfer</option>
			<option value="spywareremoval" id="spywareremoval">Spyware/Adware Removal</option>
			<option value="virusremoval" id="virusremoval">Virus Removal</option>
		</select>
	</div>
	<div style="display:none" id="service-misc">
		<select name="miscellaneous" id="miscellaneous" class="miscellaneous">   
		<option value="networksecurity" id="networksecurity">Network Security</option> 
		<!--<option value="webdesign" id="webdesign">Website Design</option>-->
		</select>
	</div><br>
	Message:<br> 
	<textarea name="message" rows="7" cols="30"></textarea><br> 
	<input type="submit" value="Submit Request"/> 
</form>

2
  • Straight PHP, you can't. If you were interested in mixing in some javascript / jQuery, you could then set the "disabled" attribute of the input(s) that should not be submitted, and un-disable them as they should be completed / submitted. Commented Mar 29, 2016 at 21:52
  • I wouldn't mind throwing in some JS/JQuery at all, but the problem is I want every option to be selectable and it sounds like, with your method, that wouldn't be possible. Correct me if I'm wrong, I'm not terribly familiar with either language, just enough to edit, not create haha Commented Mar 29, 2016 at 21:55

1 Answer 1

0

I assume that you do have jquery to display the relevant select boxes upon selection of devices etc... I have made a few changes in your html form, like adding id="form" in your form tag. Also added value="" for options like "Please select Service".. Here is the code. Hope this helps.. Refer to this as well.. Jquery form submit to check empty fields

<form id="form"  action="sending.php" method="POST" enctype="multipart/form-data"> 
<input type="hidden" name="action" value="submit"> 
Name:<br> 
<input name="name" type="text" value="" size="30"/><br> 
Email:<br> 
<input name="email" type="text" value="" size="30"/><br> 
Service:<br> 
<select name="service" id="service" class="service">
    <option value="">Select a Service</option>  
    <option value="screen" data-target="devices" id="screen">Screen Replacement</option>       
    <option value="comp" data-target="comp" id="comp">Computer Work</option> 
    <option value="misc" data-target="misc" id="misc">Miscellaneous</option>
</select>
<div style="display:none" id="service-devices">
    <select name="devices" id="devices" class="devices">
        <option value="">Select a Device</option>      
        <option value="iphone" data-target="iphones" id="iphone">iPhone</option>       
        <option value="ipad"  data-target="ipads" id="ipad">iPad</option>
        <!--<option value="watch" id="watch">Apple Watch</option> -->
        <option value="android" id="android">Android</option>
    </select>
    <div style="display:none" id="devices-iphones">
        <select name="iphone" id="iphone" class="iphone">
            <!--<option value="iphone6s" id="iphone6s">iPhone 6S</option> -->
            <!--<option value="iphone6splus" id="iphone6splus">iPhone 6S Plus</option>-->
                            <option value="">Select a Model</option>
            <option value="iphone6" id="iphone6">iPhone 6</option>
            <option value="iphone6plus" id="iphone6plus">iPhone 6 Plus</option>
            <option value="iphone5s" id="iphone5s">iPhone 5S</option>
            <option value="iphone5c" id="iphone5c">iPhone 5C</option>
            <option value="iphone5" id="iphone5">iPhone 5</option>
            <option value="iphone4s" id="iphone4s">iPhone 4S</option>
            <option value="iphone4" id="iphone4">iPhone 4</option>
        </select>
    </div>
    <div style="display:none" id="devices-ipads">
        <select name="ipad" id="ipad" class="ipad">
                        <option value="">Select a Model</option>
            <option value="ipadmini3" id="ipadmini3">iPad Mini 3</option>
            <option value="ipadmini2" id="ipadmini2">iPad Mini 2</option>
            <option value="ipadair" id="ipadair">iPad Air</option>
            <option value="ipad4" id="ipad4">iPad 4</option>
            <option value="ipad3" id="ipad3">iPad 3</option>
            <option value="ipadmini" id="ipadmini">iPad Mini</option>
            <option value="ipad2" id="ipad2">iPad 2</option>
        </select>
    </div>
</div>
<div style="display:none" id="service-comp">
    <select name="compwork" id="compwork" class="compwork">
        <option value="desktopcreation" id="desktopcreation">Desktop Creation</option>
        <option value="desktopbuild" id="desktopbuild">Desktop Build</option>
        <option value="hardwareupgrades" id="hardwareupgrades">Hardware Upgrades</option>
        <option value="datarecovery" id="datarecovery">Data Recovery/Transfer</option>
        <option value="spywareremoval" id="spywareremoval">Spyware/Adware Removal</option>
        <option value="virusremoval" id="virusremoval">Virus Removal</option>
    </select>
</div>
<div style="display:none" id="service-misc">
    <select name="miscellaneous" id="miscellaneous" class="miscellaneous">   
    <option value="networksecurity" id="networksecurity">Network Security</option> 
    <!--<option value="webdesign" id="webdesign">Website Design</option>-->
    </select>
</div><br>
Message:<br> 
<textarea name="message" rows="7" cols="30"></textarea><br> 
<input type="submit" value="Submit Request"/> 

    <script src="js/jquery-1.10.2.min.js"></script>
    <script>
    $(document).ready(function() {
    $('select').change(function(){
     var boxid = $(this).children('option:selected').attr('data-target');
     alert(boxid);
     if(boxid == "devices") {
       var divId = "service-" + boxid;   
     }
     document.getElementById(divId).style.display = "block";

    });

    $("form").submit(function(){
       // $(':input[value=""]').attr('disabled', true);

        $("input[type='text'],select,input[type='password'],textarea",this).each(function() {
            if($(this).val().trim() == "") {
               $(this).attr('disabled', true);
            }
        })


    });


    });    
    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, that's not really what I was trying to do; I'm trying to create and populate a php contact form, not validate that everything is filled out (though important, not the issue right now).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.