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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Best way to use custom function

    I am happy that I have made a custom function to check if the value from seven groups of radio buttons is either 1,2,3,4,5
    I would like some advice on the best way to use the function to check each group of radio buttons and if all correct return true,
    ready to continue to the next part of my script
    currently the code checks the first of the radio button groups $radio_one

    PHP Code:
    $value_chosen = array(12345);

    function 
    isin_array($arrayy$value_chosen) {
        
        return 
    in_array($arrayy$value_chosen);
    }

    $radio_one = empty($_POST['radio-one']) ? $isempty TRUE $_POST['radio-one'];
    $radio_two = empty($_POST['radio-two']) ? $isempty TRUE $_POST['radio-two'];
    $radio_three = empty($_POST['radio-three']) ? $isempty TRUE $_POST['radio-three'];
    $radio_four = empty($_POST['radio-four']) ? $isempty TRUE $_POST['radio-four'];
    $radio_five = empty($_POST['radio-five']) ? $isempty TRUE $_POST['radio-five'];
    $radio_six = empty($_POST['radio-six']) ? $isempty TRUE $_POST['radio-six'];
    $radio_seven = empty($_POST['radio-seven']) ? $isempty TRUE $_POST['radio-seven'];

    if(
    $isempty){       
     echo 
    'something is empty';            
    }else{                 
     
    isin_array($radio_one$value_chosen);   
        } 

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,955
    Thanks
    36
    Thanked 1,064 Times in 1,060 Posts
    A simpler way, 2 sets of check boxes:
    The HTML:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>New document</title>
    <meta name="viewport" content="width= device-width, inital-scale=1">
    <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    
    <body>
    
    <form action="progOne.php" method="post">
    <input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
    <input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
    <input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
    <br><br><br>
    <input type="checkbox" name="check_records[]" value="Green Acres"><label>Green Acres</label><br/>
    <input type="checkbox" name="check_records[]" value="The Mumsters"><label>The Mumsters</label><br/>
    <input type="checkbox" name="check_records[]" value="Last Train to Clarksville"><label>Last Train to Clarksville</label><br/>
    <input type="submit" name="submit" value="Submit"/>
    </form>
    
    </body>
    </html>
    And what you want The PHP:
    Code:
    <?php
    if(isset($_POST['submit'])){//to run PHP script on submit
    	if(!empty($_POST['check_list'])){
    		foreach($_POST['check_list'] as $selected){
    			echo $selected."</br>";
    		}
    	}
    	
    	if(!empty($_POST['check_records'])){
    		foreach($_POST['check_records'] as $selected){
    			echo $selected."</br>";
    		}
    	}
    }
    ?>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,257
    Thanks
    3
    Thanked 553 Times in 538 Posts
    The main point of user written functions/classes is to do something that the language doesn't already do. If your function only contains one line of code that is something the language can already do, there's no point in having a user written function, just use the native line of code.

    Next, when you validate user submitted data, you need to produce a unique and descriptive error message telling the user exactly what is wrong with each piece of submitted data. Don't just output a vague 'something is empty' message.

    Rather than to worry about writing a function that may not have anything to do with the problem you are trying to solve, you should be concentrating on ways of dynamically doing this task so that you are not writing out line after line of code that only differs in the form field name. The seven groups of radio buttons are a set of data. Arrays are for sets of data. You will be processing them all in the same way. The only thing that differs between them is the form field name and that you will store the submitted choice using a value that identifies the group it is for. If you use an array name for the form fields, the data will be submitted as an array and you can use php's array functions to dynamically process the data.

    See the following example code showing how to implement this using a general purpose, dynamic, data driven design, where you have a data structure defined somewhere (array, database table) that controls what the code does -
    PHP Code:
    <?php
    // x (7) radio button groups with 5 choices each

    // define a data structure to control what the code does
    // each entry is an id (identifier) value and a label/name/meaning of the radio button group
    $groups = [1=>'rating one',2=>'rating two',3=>'rating three',4=>'rating four',5=>'rating five',6=>'rating six',7=>'rating seven'];
    $choices range(1,5);

    $errors = []; // define an array to hold validation errors

    // post method form processing code
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        
    // examine the submitted data
        
    echo '<pre>'print_r($_POST); echo '</pre>';

        
    // validate the data
        
    foreach($groups as $id=>$label)
        {
            if(!isset(
    $_POST['radio'][$id]))
            {
                
    // no radio button was selected
                
    $errors["radio_{$id}"] = "Select a rating for: $label.";
            } else {
                
    //a radio button was selected, validate the value
                
    if(!in_array($_POST['radio'][$id],$choices))
                {
                    
    $errors["radio_{$id}"] = "The selected rating for: $label, is an invalid choice."// you should only see this error due to a programming mistake or someone submitting their own data to your site
                
    }
            }
        }
        
        
    // if no errors, use the submitted data/go on to the next step...
        
    if(empty($errors))
        {
            
    // use the submitted data here...
            
        
    }
    }
    ?>

    <?php
    // display any errors
    if(!empty($errors))
    {
        echo 
    implode('<br>',$errors);
        echo 
    '<br>';
    }
    ?>

    <form method='post'>
    <?php
    // dynamically produce the html markup
    foreach($groups as $id=>$label)
    {
        echo 
    "$label: ";
        foreach(
    $choices as $choice)
        {
            
    $chk = isset($_POST['radio'][$id]) && $_POST['radio'][$id] == $choice ' checked' '';
            echo 
    "<input type='radio' name='radio[$id]' value='$choice'$chk$choice ";
        }
        echo 
    '<br>';
    }
    ?>
    <input type='submit'>
    </form>
    You will note that there is not seven lines of repeated code written out, one for each possible radio button group. You can change the amount of radio button groups and their label/name/meaning just by changing the definition of the $groups array. Since the html markup is being produced dynamically, there's only one instance of each element, so that if you need to change what it is doing, such as adding a <label></label> tag, which I purposely left out to save on the free typing being given, you can easily add it because you will only need to do so in one place, not in 35 different places.

    The part of this that is an answer to your question of how to - "if all correct return true, ready to continue to the next part of my script" is the use of the $errors array in the code. This array holds the error messages and also serves as an error flag. If the array is empty, there are no errors. If the array is not empty, there are errors.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.


 

Tags for this Thread

Posting Permissions

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