user3279136
user3279136

Reputation: 23

Compare selected option value with php variable

i have the option list inside my php code as :

<select class="form-control" name="currentDegree" id="currentDegree">
    <option value="Junior High School">Junior High School</option>
    <option value="Senior High School">Senior High School</option>
    <option value="Diploma">Diploma</option>
    <option value="Bachelor Degree">Bachelor Degree</option>
    <option value="Master">Master</option>
    <option value="Doctor">Doctor</option>
</select>

and then JS tag as

<script>
    var currentDegree = $('#currentDegree').val(); // selected option at listbox
    var minimumDegree = '<php? echo $minimumDegree; ?>'; // it come from the admin backend
    var md; // dinamic variable ( condition depend on minimumDegree )

    function CheckDegree(){                                                                      

        if (minimumDegree == 'Junior High School') {
            md = 0;  
        } else if (minimumDegree == 'Senior High School') {
            md = 1; 
        }
        else if (minimumDegree == 'Diploma') {
            md = 2; 
        }
        else if (minimumDegree == 'Bachelor Degree') {
            md = 3; 
        }
        else if (minimumDegree == 'Master') {
            md = 4; 
        }
        else (minimumDegree == 'Doctor') {
            md = 5; 
        } 
    };
    // Compare the currentDegree variable with External variable
    if (parseint(currentDegree) < md) {
        alert(" Current degree is less than Our minimum requirement !");
        return false;
    }                                                               
</script>  

I use onclick="CheckDegree()" upon submit, But seems it not working properly, has anyone know what type of error in the JS code

Thanks

Upvotes: 0

Views: 202

Answers (3)

cesar andavisa
cesar andavisa

Reputation: 340

Change this :

'<php echo $minimumDegree; ?>'

For this :

<?php echo $minimumDegree; ?>

Upvotes: 0

zzlalani
zzlalani

Reputation: 24354

you need to move last condition if (parseint(currentDegree) < md) { in the function

check the bottom of this code

function CheckDegree(){                                                                      

    if (minimumDegree == 'Junior High School') {
        md = 0;  
    } else if (minimumDegree == 'Senior High School') {
        md = 1; 
    }
    else if (minimumDegree == 'Diploma') {
        md = 2; 
    }
    else if (minimumDegree == 'Bachelor Degree') {
        md = 3; 
    }
    else if (minimumDegree == 'Master') {
        md = 4; 
    }
    else (minimumDegree == 'Doctor') {
        md = 5; 
    } 

    // Compare the currentDegree variable with External variable
    if (parseint(currentDegree) < md) {
        alert(" Current degree is less than Our minimum requirement !");
        return false;
    }
};

Upvotes: 0

Felix
Felix

Reputation: 38102

Try to use:

var minimumDegree =  '<?php echo $minimumDegree; ?>';

instead of:

var minimumDegree = '<php? echo $minimumDegree; ?>';

You need <?php not <php?

Upvotes: 3

Related Questions