Jump to content

Help with radio check box in a form


stanoliver
 Share

Recommended Posts

For reference... Input type radio

Regardless of how many options you make available to the user, each one has a unique value. Since the value of the selected radio button is submitted with the form, the only sanitation you need is to check whether the submitted option is in, for example an array of valid values.

32 minutes ago, stanoliver said:

form example with radio checkbox

Just to clarify terminology, a radio button and a check box are two different types of inputs. A radio button is usually presented as a set of options in which only one can be selected, eg, Select Size: ()small, ()medium, ()large.

A check box on the other hand is displayed as a single on/off indicator for a specific option, eg, Display User Name [yes|no] <- checked or unchecked.

Link to comment
Share on other sites

Hi @rick yes you are right and thanks for your clarification. It's not a big deal for me to use radio buttons or checkboxes in the frontend, especially there are a lot of frameworks for it. Could you just help me to provide some php code which validates a minimal checkbox/radio button form from the serverside with wiremail? I just do not know how to validate from serverside (sanitize the inputs of the form with php). 

Link to comment
Share on other sites

Hi @stanoliver,

What are you wanting to do with wiremail?

In general,

Using the example from the MDN link I posted, this form contains three options (radio buttons). You will notice that each radio button has the same name (contact), effectively creating an array, or group. Remember that only one option can be selected (or no option).

// I've added a few things to the example to help clarify my explanation.
// I've added the action and method to the example.
// I've added the required attribute to the input element.

<form action="" method="post">
  <p>Please select your preferred contact method:</p>
  <div>
    <input type="radio" id="contactChoice1" name="contact" value="email" required>
    <label for="contactChoice1">Email</label>
    
    <input type="radio" id="contactChoice2" name="contact" value="phone" required>
    <label for="contactChoice2">Phone</label>
    
    <input type="radio" id="contactChoice3" name="contact" value="mail" required>
    <label for="contactChoice3">Mail</label>
    
  </div>
  <div>
    <button type="submit">Submit</button>
  </div>
</form>

When this form is submitted, only a selected radio button is submitted. For example, if a user selects "Phone", then the server receives $_POST["contact"]="phone". Since I specified each option is required, then the user *must select one. If the options were not required, then if no option was selected, there would not be a contact parameter.

So in your server side script (the php file associated with the template) you would do something like this:

$contact = $input->post('contact');
// $contact now contains the selected value, either "email", "phone", or "mail"

if( $contact == "email" ) {
	// do some email stuff.

} elseif ( $contact == "phone" ) {
	// do some phone stuff.

} elseif( $contact == "mail" ) {
	// do some mail stuff.
}

Is this what you are looking for? If not, please give me more information to go on.

Link to comment
Share on other sites

Yes, @rick your example is just fine. A simple form is all I need. I already got to work simple forms with text fields, attachment, "answer" mails ... to work. Just with radio buttons and check boxes I had problems, because I did not know how to sanitize the input through php or the processwire way. If I look at your example I only would still like to know how to sanitize your form with php/the processwire way.

Thanks, for your help already.

Link to comment
Share on other sites

22 hours ago, rick said:

Check this reference ...

santizer

 

Hi @rick that reference I also have found but as far as I understand there are just hints how to sanitize normal text/textarea fields. I do not now how that reference could help me with radio boxes or checkboxes. Can anyone provide an full example?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...