SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    158
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    radio button issue in a form processing script

    hello,

    I found a tutorial script that works for me but it doesn't seem to work well with radio buttons. I'm hoping someone could have a look at that script and help me get it working with the radiobutton.

    the selection carrie over to the process.php script but cant seem to process the selection.
    need 2 things:
    1 process the selection.
    2. in the event user enters incorrect information somewhere else i like to be able to go back to the form with the users original radio button selection. eg. Name: <input type="text" name="name" value="<?php echo $name; ?>" > <br />

    any help you can give me would be greatly appreciated.



    index.php
    PHP Code:
    <body>
    <h3>Form testing</h3>
    <?php echo $errorString?>
    <br />
    <form name="formprocess" method="POST" action="process.php">
        Name: <input type="text" name="name" value="<?php echo $name?>" > <br />
        eMail: <input type="text" name="email" value="<?php echo $email?>" > <br />
        Birthday:<input type="text" name="birthday" value="<?php echo $birthday?>" /><br />
        <br />
        <p> <input type="radio" name="radiobutton" value="1"> OptionOne </p>
        <p> <input type="radio" name="radiobutton" value="2"> OptionTwo </p>
        <p> <input type="radio" name="radiobutton" value="3"> OptionThree</p>
        
        <input type="submit" name="formSubmit" value="Submit">
    </form>
    </body>


    process.php - form processing script
    PHP Code:
    <?php 
    $allowedFields 
    = array(
        
    'name',
        
    'email',
        
    'birthday',
        
    'radiobutton',
    );

    $requiredFields = array(
        
    'name',
        
    'email',
        
    'radiobutton',
    );
    echo 
    $_POST['radiobutton'];
    echo 
    '<br />';
    print_r($_POST);
    //loop through the POST array
    $fieldErrors = array();
    foreach(
    $_POST as $key=>$value)
    {    
    // allowed fields
        
    if(in_array($key$allowedFields))
        {    $
    $key $value;
            
    // required field?
            
    if(in_array($key$requiredFields) && $value == '')
            {    
    $fieldErrors[] = "The field $key is required."
            }
        }    
    }

    // Any errors
    if(count($fieldErrors) > 0)
    {    
    $errorString '<p>There was an error processing the form.</p>';
        
    $errorString 'ul>';
        foreach(
    $fieldErrors as $errorVal)
        {    
    $errorString .= "<li>$errorVal</li>"; }
        
    $errorString .= '</ul>';
        include 
    'form.php';
    }
    else
    {    echo 
    "<br /> append to  database, goes here";  }
    ?>

  2. #2
    SitePoint Member
    Join Date
    Jan 2011
    Location
    Karachi, Pakistan.
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi robin,

    this will do what you are trying to achieve


    PHP Code:
    <body> 
    <h3>Form testing</h3> 
    <?php echo $errorString?> 
    <br /> 
    <form name="formprocess" method="POST" action="process.php"> 
        Name: <input type="text" name="name" value="<?php echo $name?>" > <br /> 
        eMail: <input type="text" name="email" value="<?php echo $email?>" > <br /> 
        Birthday:<input type="text" name="birthday" value="<?php echo $birthday?>" /><br /> 
        <br /> 
        <p> <input type="radio" name="radiobutton" value="1" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 1) ?  'checked':''?>> OptionOne </p> 
        <p> <input type="radio" name="radiobutton" value="2" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 2) ?  'checked':''?>> OptionTwo </p> 
        <p> <input type="radio" name="radiobutton" value="3" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 3) ?  'checked':''?>> OptionThree</p> 
         
        <input type="submit" name="formSubmit" value="Submit"> 
    </form> 
    </body>

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    158
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fathaheem View Post
    hi robin,

    this will do what you are trying to achieve


    PHP Code:
    <body> 
    <h3>Form testing</h3> 
    <?php echo $errorString?> 
    <br /> 
    <form name="formprocess" method="POST" action="process.php"> 
        Name: <input type="text" name="name" value="<?php echo $name?>" > <br /> 
        eMail: <input type="text" name="email" value="<?php echo $email?>" > <br /> 
        Birthday:<input type="text" name="birthday" value="<?php echo $birthday?>" /><br /> 
        <br /> 
        <p> <input type="radio" name="radiobutton" value="1" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 1) ?  'checked':''?>> OptionOne </p> 
        <p> <input type="radio" name="radiobutton" value="2" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 2) ?  'checked':''?>> OptionTwo </p> 
        <p> <input type="radio" name="radiobutton" value="3" <?php echo (isset($_POST['radiobutton']) && $_POST['radiobutton'] == 3) ?  'checked':''?>> OptionThree</p> 
         
        <input type="submit" name="formSubmit" value="Submit"> 
    </form> 
    </body>
    Thank you for the help. The script you provided works but in the process.php script the variable is passed but if the radio button isnt selected it bypasses the mandatory check.

    If the radiobutton isnt selected the array is empty, the key value doesnt even appear any way to fix that?

  4. #4
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by robin01 View Post
    Thank you for the help. The script you provided works but in the process.php script the variable is passed but if the radio button isnt selected it bypasses the mandatory check.

    If the radiobutton isnt selected the array is empty, the key value doesnt even appear any way to fix that?
    f the radioButton isn't set, set a default value before the code snippet you provided runs. That way one of the buttons will be checked.


Bookmarks

Posting Permissions

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