0

I have a form such as the one below:

 <input type="checkbox" name="type" value="wash"><label>Wash</label><br>
 <input type="checkbox" name="type" value="no wash"><label>No Wash</label><br>
        <label>Other (Specify)</label><br>
        <input name="type"><br>

If you notice for all three i am using "type" as the input name. The point being that the user will be given two options, if none of the two options apply to them they should enter a value in other. Now in the database i have the field type, so if they selected the first two and entered a value in the field or if they only wrote a value in the field i still want it to to be part of the type field. So how can i make it so that if they select the input field it should also insert in "type".

2 Answers 2

1

Do you mean something like this?

HTML:

<input type="checkbox" name="type[]" value="wash"/><label>Wash</label>
<input type="checkbox" name="type[]" value="no_wash"/><label>No wash</label>
Other type:
<input type="text" name="other_type"/>

PHP:

if (!empty($_REQUEST['other_type']))
    $_REQUEST['type'][] = $_REQUEST['other_type'];
var_dump($_REQUEST['type']);
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you should better use radio buttons instead of checkboxes. Then you could do the following

<input type="radio" name="type" value="wash"/><label>Wash</label>
<input type="radio" name="type" value="no_wash"/><label>No wash</label>
<input type="radio" name="type" value="other"/><label>Other</label>
<input type="text" name="other_type"/>

Your PHP would then look like this:

if ($_REQUEST["type"] == "wash"){
    echo "Wash me please";
}else if ($_REQUEST["type"] == "no_wash"){
    echo "no wash";
}else if ($_REQUEST["type"] == "other"){
    echo "you want to ".$_REQUEST["other_type"];
}

If you use JS you could even disable the textbox unless the user selects the third option.


Edit: If I got your comment right it would be the easiest like this:

<input type="checkbox" name="wash_me"/><label>Wash your car?</label>
<input type="text" name="other"/><label>What else can we do for you?</label>

PHP

if (isset($_REQUEST["wash_me"]){
    echo "wash my car please";
}
if (strlen($_REQUEST["other"]) != 0){
    echo "and do the following: ".$_REQUEST["other"];
}

6 Comments

Beat me to it :) Would only add that you might want to enable/disable the input based on the value of the other radio.
I need checkbox to enable selection of multiple fields.
Just change it back to a checkbox then. It shouldn't change anything.
Sorry, i didn't get it right, is the wash/no wash just an example?
So you want that the user chooses wether he wants his car washed and then what else he wants to have done, right?
|

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.