2

Sorry for my bad title!:) According to this question how can I check and store the appeared field's value With php and mysql;

assume that the form is something like this:

<form method="post" action="#">
    <select name="job" id="job" class="medium">
        <option selected="selected" value="-1">Please Select...</option>
        <option value="1">Job A</option>
        <option value="2">Job B</option>
        <option value="3">Job C</option>
        <option value="4">Job D</option>
        <option value="0">Other</option>
    </select>

    <div id="foo" style="display:none;">
        <input type="text" name="job_other" id="job_other" placeholder="If Other, please specify" value="" />
    </div>
    <div>
        <input type="submit" name="submit" id="submit" value="submit" />
    </div>
</form>

when selecting the "Other" option, "job_other" text field will appear.(handled by javascript) is my procedure true to check and get field in php like this:

<?php 
 $input['job'] = (isset($_POST['job']) && $_POST['job'] != '-1') ? $_POST['job'] : '';

 if ( $input['job'] != '' &&  $input['job'] == '0') {
    $input['job_other'] = (isset($_POST['job_other']) && $_POST['job_other'] != '' && $_POST['job_other'] == '0') ? $_POST['job_other'] : '';
 }

?>

By the way when the field appear, it will be Required. so how can I do this better?

10
  • 1
    if ( $input['job'] != '' && $input['job'] == '0') could be simplified by if($input['job'] == '0') : demo Commented Oct 1, 2013 at 14:06
  • Question title is fine but question contents I couldn't understand, could you please rephrase what you want? Commented Oct 1, 2013 at 14:07
  • thank you! I didn't know that can simplify it like this. Commented Oct 1, 2013 at 14:09
  • I want to get the hidden field just when it appears in the form as a required field by php. Commented Oct 1, 2013 at 14:10
  • 2
    Oops, actually I messed up... You should test like so : if($input['job'] === '0') so it doesn't match false, 0 or FALSE. Demo Commented Oct 1, 2013 at 14:14

1 Answer 1

1

This would be sufficient:

<?php 
 $input['job'] = (isset($_POST['job'])) ? (int) $_POST['job'] : -1;

 $input['job_other'] = ( ($input['job'] == 0) && (isset($_POST['job_other'])) ) ? $_POST['job_other'] : '';
?>

Save the 'job' field as integer and 'job_other' as string / varchar.

Sign up to request clarification or add additional context in comments.

2 Comments

what if user dont select "other" option? again I store the field as an empty string? <?php $input['job'] = (isset($_POST['job'])) ? (int) $_POST['job'] : -1; if ( $input['job'] == 0) { $input['job_other'] = (isset($_POST['job_other'])) ? $_POST['job_other'] : ''; } else {$input['job_other'] = '';} ?>
Yes, or use the revised solution :)

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.