1

I am having problems writing a function in javascript on a PHP page that points to a select box. This if statement is to point to the select box

if (!(document.getElementById("add_product").value)==="Choose here")

Here's the code:

                     function promptEnrollmentMonth() {
                         if (!(document.getElementById("add_product").value)==="Choose here") {
                             var month = prompt("Is this the correct month for this course/product?", "<?php echo "$EnrollmentMonth"; ?>");
                             if (month !=null) {

                             }
                         }
                     }

 <button type="submit"
 onclick="promptEnrollmentMonth()">Update</button>

 <div>Add course/product:<select name='add_product'>
 <option selected>Choose here</option> 
 <option>Other options...</option>
 </select></div>
1
  • 1
    if (document.getElementById("add_product").value !=="Choose here") Commented Jun 21, 2017 at 3:45

2 Answers 2

3

First issue is with "<?php echo "$EnrollmentMonth"; ?>"

it should be like :

var month = prompt("Is this the correct month for this course/product?", "<?php echo $EnrollmentMonth; ?>");

Second <option selected>Choose here</option> you haven't provide value attribute to option tag :

<option value='Choose here' selected>Choose here</option> 
Sign up to request clarification or add additional context in comments.

Comments

1

you have a typo in the if statement

if (!(document.getElementById("add_product").value)==="Choose here")

should be

if (document.getElementById("add_product").value !=="Choose here")

you need to set the value to choose here for the initial option. Or

if (document.getElementById("add_product").value !=="")

this case would apply if you don't want to set a value to the initial select option.

!(document.getElementById("add_product").value)

would just translate to true or false, which would never equal "Choose here"

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.