Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Passing multiple variables from a dropdown menu

    I have a couple of drop down menus where the user can select more than one option. As of now it is only passing a single variable. Any help will be greatly appreciated.

    Code:
    <td>
    		<label>Where did you hear about us?(hold Ctrl button to select all that apply)</label></br>
    	
    			<select name="hereabout" multiple="multiple" onchange="v=options[selectedIndex].value;if(/\.php$/i.test(v))self.location=v">
    				<option value="internet">Internet</option>
    				<option value="booth">Fair Booth</option>
    				<option value="news">Newspaper/Advertising</option>
    				<option value="word">Word of Mouth</option>
    				<option value="other">Other</option>
    			</select>
    		</td></br>
    		<td>
    			<label>What products are you interested in?( hold Ctrl button to select all that apply)</label></br>
    			<select name="product" multiple="multiple" onchange="v=options[selectedIndex].value;if(/\.php$/i.test(v))self.location=v">
    				<option value="a4">Asronaut A4</option>
    				<option value="cosmix">Cosmix</option>
    				<option value="juno">Juno</option>
    				<option value="calm">Calm</option>
    				<option value="grazeway">Grazeway</option>
    				<option value="discovery">Discovery</option>
    				<option value="luna">Luna Brush</option>
    			</select>
    		</td>

    PHP Code:
    <?php
    $name 
    check_input($_POST['yourname']);
    $address check_input($_POST['youraddress']);
    $city   check_input($_POST['yourcity']);
    $state check_input($_POST['yourstate']);
    $zip check_input($_POST['yourzip']);
    $phone check_input($_POST['yournumber']);
    $email check_input($_POST['youremail']);
    $hereabout check_input($_POST['hereabout']);
    $product check_input($_POST['product']);

    ?>
    <html>
    <body>
    Your name is: <?php echo $name?><br />
    Your address is: <?php echo $address." ".$city." ".$state." ".$zip?><br />
    Your phone number is: <?php echo $phone?><br/> 
    Your e-mail: <?php echo $email?><br />
    Where did you here about us: <?php echo $hereabout;  ?><br />
    What products are you interested in? <?php echo $product?>


    </body>
    </html>

    <?php
    function check_input($data)
    {
        
    $data trim($data);
        
    $data stripslashes($data);
        
    $data htmlspecialchars($data);
        return 
    $data;
    }
    ?>

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    change its name to
    PHP Code:
    name="hereabout[]" 

    and your sanitize section to

    PHP Code:
    foreach ($_GET['hereabout'] As $k){
        
    $hereabout .= check_input($k).' ';

    and keep this the same

    PHP Code:
    Where did you here about us: <?php echo $hereabout;  ?><br />
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    jeff1c1 (04-16-2013)

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,205
    Thanks
    23
    Thanked 605 Times in 604 Posts
    @jeff1c1, shouldn't that be
    Where did you hear about us:
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    jeff1c1 (04-16-2013)

  • #4
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    change its name to
    PHP Code:
    name="hereabout[]" 

    and your sanitize section to

    PHP Code:
    foreach ($_GET['hereabout'] As $k){
        
    $hereabout .= check_input($k).' ';

    and keep this the same

    PHP Code:
    Where did you here about us: <?php echo $hereabout;  ?><br />
    Awesome! Thank you very much. It is working sort of. Thank you sunfighter, that little spelling mistake could have been embarrassing. I have made the changes suggested (including "hearabout") and it does pass multiple variables but with the following errors.

    Notice: Undefined variable: hearabout in C:\xampp\htdocs\info\myform.php on line 11

    Notice: Undefined variable: product in C:\xampp\htdocs\info\myform.php on line 15

    Any suggestions?
    Code:
    <td>
    		<label>Where did you hear about us?(hold Ctrl button to select all that apply)</label></br>
    	
    			<select name="hearabout[]" multiple="multiple">
    				
    				<option value="internet">Internet</option>
    				<option value="booth">Fair Booth</option>
    				<option value="news">Newspaper/Advertising</option>
    				<option value="word">Word of Mouth</option>
    				<option value="other">Other</option>
    			</select>
    		</td></br>
    		<td>
    			<label>What products are you interested in?( hold Ctrl button to select all that apply)</label></br>
    			<select name="product[]" multiple="multiple">
    				<option value="a4">Asronaut A4</option>
    				<option value="cosmix">Cosmix</option>
    				<option value="juno">Juno</option>
    				<option value="calm">Calm</option>
    				<option value="grazeway">Grazeway</option>
    				<option value="discovery">Discovery</option>
    				<option value="luna">Luna Brush</option>
    			</select>
    		</td>
    PHP Code:
    ?php
    $name = check_input($_POST['yourname']);
    $address = check_input($_POST['youraddress']);
    $city   = check_input($_POST['yourcity']);
    $state = check_input($_POST['yourstate']);
    $zip = check_input($_POST['yourzip']);
    $phone = check_input($_POST['yournumber']);
    $email = check_input($_POST['youremail']);

    foreach ($_POST['hearabout'] As $k){
        $hearabout.= check_input($k).' ';
    }  

    foreach ($_POST['product'] As $k){
        $product.= check_input($k).' ';
    }


    ?>
    <html>
    <body>
    Your name is: <?php echo $name?><br />
    Your address is: <?php echo $address." ".$city." ".$state." ".$zip?><br />
    Your phone number is: <?php echo $phone?><br/> 
    Your e-mail: <?php echo $email?><br />
    Where did you hear about us: <?php echo $hearabout;  ?><br />
    What products are you interested in? <?php echo $product?>


    </body>
    </html>

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    yes you'll need to define them as blank first as you have something that requires it set in your php.ini

    just add these at the top

    PHP Code:

    <?php
    //these 2 lines
    $hearabout '';
    $product '';


    $name check_input($_POST['yourname']);
    $address check_input($_POST['youraddress']);
    $city   check_input($_POST['yourcity']);
    $state check_input($_POST['yourstate']);
    $zip check_input($_POST['yourzip']);
    $phone check_input($_POST['yournumber']);
    $email check_input($_POST['youremail']);

    foreach (
    $_POST['hearabout'] As $k){
        
    $hearabout.= check_input($k).' ';
    }  

    foreach (
    $_POST['product'] As $k){
        
    $product.= check_input($k).' ';
    }


    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    jeff1c1 (04-16-2013)

  • #6
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    15
    Thanked 0 Times in 0 Posts
    That's what I am looking for. Thank you so much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •