Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

ok so, the title of this question might be a little confusing, and i will try to explain it to you: i have a php file with an array containing multiple form fields like this:

$var = array(0 => '<input type=....>', 1=> '<input type=....>');

and i created a while loop to loop through the array to show them in a table, something like this:

echo '<table>';
  while($row = mysql_fetch_array($result)){
    $num = $row['fieldId'];
    $field = $num.' '.$row['field'];
    echo '<tr><td>'.$field.'</td>';
    echo '<td>'.$var[$q].'</td></tr>';
    echo '<tr><td>&nbsp;</td></tr>';
    $q++;
  }; echo '</table>';

i included this php file in a html file so i can get a table with two columns, the first one with $field from the database and the second with $var[$q] which is some input field taken from the $var array. Believe it or not that actually works, now the problem is that inside that $var array i have a <select> field with multiple options, i want to validate that field with some javascript, but for some reason i can't get the values assigned in <option value="SOMEVALUE"> . i don't know why but i can't, one thing i found weird is that in that $var array i have some radio buttons which i CAN validate with javascript.

more info: the html file that includes the php file has this:

<html>....
<form name="mainForm">
  <?php
    include 'myPhpFile.php';
  ?>
  <input type="button" name="btnNext" onclick="functionToValidate();">
</form>
...</html>

so basically when i include my php file it creates a table and adds fields to it filling them with data from a mysql database and some input fields using the $var array shown above all that works, but the problem is when i click the next button it calls a javascript function to validate the fields, everything works until it gets to the <select> input, i can validate radio buttons, text fields and textareas but i can't validate those <select>

this is the javascript function i am using:

function myFunctionNameHere(){
if(!document.mainForm.p1_1[0].checked && !document.mainForm.p1_1[1].checked)
      alert("1 - some warning here");
    else if(!document.mainForm.p1_2[0].checked && !document.mainForm.p1_2[1].checked)
      alert("2 - some warning here");
    else if(!document.mainForm.p1_3[0].checked && !document.mainForm.p1_3[1].checked)
      alert("3 - some warning here");
    else if(!document.mainForm.p1_4[0].checked && !document.mainForm.p1_4[1].checked)
      alert("4 - some warning here");
    else if(!document.mainForm.p1_5[0].checked && !document.mainForm.p1_5[1].checked)
      alert("5 - some warning here");
    else if(!document.mainForm.p1_6[0].checked && !document.mainForm.p1_6[1].checked)
      alert("6 - some warning here");
    else if(document.mainForm.p1_7.value == "")
      alert("7 - some warning here");
    // this condition does not work:
    else if(document.mainForm.p1_8.selectedIndex == 0)
      alert("8 - some warning here);
   // can't get the selectd field from the html select field
    else
      window.location = "OTHERPAGE.html";
}

that's pretty much what the function does, all conditions work except for that one i put the comments on. (i'm still learning so don't laugh at my code... please.)

Sorry i actually got this solved, there were some errors in the html code from my php array. Now it actually works. Thanks.

share|improve this question
Could you post the parsed code of the problematic select element? – Steve Adams Oct 25 '11 at 19:13
You are getting select tag by id or using some jquery magic ? (if you post some code, would be helpful) – Kamil Lach Oct 25 '11 at 19:18
If the javascript validation is the problem, you might want to post the code of that. – rodneyrehm Oct 25 '11 at 20:24
This has nothing to do with PHP. Post the full page somewhere and let us look at it. – Brad Oct 25 '11 at 21:00

1 Answer

up vote 0 down vote accepted

this is how you access select values in javascript:

<form name="mainform">
    <select name="dropbox_select">
        <option value="value0">option0</option>
        <option value="value1">option1</option>
        <option value="value2">option2</option>
    </select>
</form>

document.forms.mainform.dropbox_select.options[0].value
document.forms.mainform.dropbox_select.options[1].value
document.forms.mainform.dropbox_select.options[2].value
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.