douqiu1604 2013-07-07 12:47
浏览 64
已采纳

根据下拉列表中的选择执行功能

I am trying to learn php today and I on the part of experimenting. I encountered problem using the drop down list, if and else and the function.

It's seems that its not working. I have no idea how to debug it. What I'm trying to do is that when the user selects "employed", it will simply return a "Do THIS!" text. but if the user selects any of the 3 (self employed, Voluntary member & OFW), it will display "DO THAT!".

It's really simple but i can't get it work, i just started php 6 hours ago. :)

Please help!

<form method="POST">

Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event)"><br>

Membership Type:
<select name="membershiptype">
        <option value="employed">Employed</option>
        <option value="SE">Self Employed</option>
        <option value="VM">Voluntary Member</option>
        <option value="OFW">OFW</option>
</select>

<br/>

<input type="submit" />


</form>


<?php
$a = (isset($_POST['salary'])) ? $_POST['salary'] : '';
$b = (isset($_POST['membershiptype'])) ? $_POST['membershiptype'] : '';

function employed () {
        if (empty ($a)) {echo "";}
        elseif ($a<10000) {$a * 2.0}
        elseif ($a<20000) {$a * 2.3}
        elseif ($a<30000) {$a * 2.7}
        elseif ($a>30000) {$a * 3.0}
}


function sevmofw() {
        if (empty ($a)) {echo "";}
        elseif ($a<10000) { $a * 1.3}
        elseif ($a<20000) { $a * 1.5}
        elseif ($a<30000) { $a * 1.8}
        elseif ($a>30000) { $a * 2.0}
}



if ( $_POST['membershiptype'] == 'employed' ){employed();
} elseif ( $_POST['membershiptype'] == 'SE' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'VM' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'OFW' ){sevmofw();
}

?>

Here's a flowchart of what i'm trying to do.

enter image description here

  • 写回答

3条回答 默认 最新

  • doulan9287 2013-07-07 12:51
    关注
    <select name="membershiptype" method="POST">
            <option value="Employed" name="employed">Employed</option>
            <option value="SE" name="sevmofw">Self Employed</option>
            <option value="VM" name="sevmofw">Voluntary Member</option>
            <option value="OFW" name="sevmofw">OFW</option>
    </select>
    

    should be

    <select name="membershiptype">
            <option value="employed">Employed</option>
            <option value="SE">Self Employed</option>
            <option value="VM">Voluntary Member</option>
            <option value="OFW">OFW</option>
    </select>
    

    and your code should be

    if (isset($_POST['membershiptype'])) {
        if ( $_POST['membershiptype'] == 'employed' ){
                employed();
        } elseif ( $_POST['membershiptype'] == 'SE' ){
                sevmofv();
        }
    }
    

    ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?