1
<input type="checkbox" name="vehicle" value="yes">

If the user checks this, the php variable $vehicle_check should hold the value ="RA" otherwise, the value will hold the default "R". Can I do this without javascript?

I tried:

if(isset($_POST['submitquotes'])){

    $check = $_POST['vehicle'];

    if ($check == 'yes'){
        $vehicle_check = 'RA';
    }
    else{
        $vehicle_check = 'R';
    }
}

This doesn't seem to work.

3
  • 1
    Can you give some more light on doesn't seem to work ? What exactly you getting in $check ? Commented Mar 26, 2014 at 12:33
  • What is submitquotes? Show us the HTML. Commented Mar 26, 2014 at 12:34
  • Do you submit it to a php file ? Commented Mar 26, 2014 at 12:34

3 Answers 3

5

The value of the checkbox is not useful - according to the html spec it'd be submitted only if checked, so check that instead. The following code should be enough:

if ( isset($_POST['vehicle']) ) {
    $vehicle_check = 'RA';
} else {
    $vehicle_check = 'R';
]
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! i am a newbie on php and thanks for the isset part. I thought this is only used when submitting forms. great!
2

Try this:

$vehicle_check = isset($_POST["vehicle"]) ? "RA" : "R";

1 Comment

I would have put this
1

Try this:

<input type="hidden" value="R"/> <input type="checkbox" name="vehicle" value="RA"/>

If the user checks the checkbox then the value of checkbox overrides the value of hidden field, since they have the same name, but if the checkbox is not checked, then value of hidden field is taken. Remember if you are giving them id give them two different id

Comments

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.