Note that there are some explanatory texts on larger screens.

plurals
  1. POvalidate drop down list echoed from php to html with javascript?
    text
    copied!<p>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:</p> <pre><code>$var = array(0 =&gt; '&lt;input type=....&gt;', 1=&gt; '&lt;input type=....&gt;'); </code></pre> <p>and i created a while loop to loop through the array to show them in a table, something like this:</p> <pre><code>echo '&lt;table&gt;'; while($row = mysql_fetch_array($result)){ $num = $row['fieldId']; $field = $num.' '.$row['field']; echo '&lt;tr&gt;&lt;td&gt;'.$field.'&lt;/td&gt;'; echo '&lt;td&gt;'.$var[$q].'&lt;/td&gt;&lt;/tr&gt;'; echo '&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;'; $q++; }; echo '&lt;/table&gt;'; </code></pre> <p>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 <code>&lt;select&gt;</code> 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 <code>&lt;option value="SOMEVALUE"&gt;</code> . 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 <strong>CAN</strong> validate with javascript.</p> <p><strong>more info:</strong> the html file that includes the php file has this:</p> <pre><code>&lt;html&gt;.... &lt;form name="mainForm"&gt; &lt;?php include 'myPhpFile.php'; ?&gt; &lt;input type="button" name="btnNext" onclick="functionToValidate();"&gt; &lt;/form&gt; ...&lt;/html&gt; </code></pre> <p>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 <code>&lt;select&gt;</code> input, i can validate radio buttons, text fields and textareas but i can't validate those <code>&lt;select&gt;</code></p> <p>this is the javascript function i am using:</p> <pre><code>function myFunctionNameHere(){ if(!document.mainForm.p1_1[0].checked &amp;&amp; !document.mainForm.p1_1[1].checked) alert("1 - some warning here"); else if(!document.mainForm.p1_2[0].checked &amp;&amp; !document.mainForm.p1_2[1].checked) alert("2 - some warning here"); else if(!document.mainForm.p1_3[0].checked &amp;&amp; !document.mainForm.p1_3[1].checked) alert("3 - some warning here"); else if(!document.mainForm.p1_4[0].checked &amp;&amp; !document.mainForm.p1_4[1].checked) alert("4 - some warning here"); else if(!document.mainForm.p1_5[0].checked &amp;&amp; !document.mainForm.p1_5[1].checked) alert("5 - some warning here"); else if(!document.mainForm.p1_6[0].checked &amp;&amp; !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"; } </code></pre> <p>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.)</p> <p><strong>Sorry i actually got this solved, there were some errors in the html code from my php array. Now it actually works. Thanks.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload