shaddf Posted February 25, 2017 Share Posted February 25, 2017 i have a set of selects in html form and would like to validate them based on : if the first select is not null and the value is not equal to the selection of the second select,and the remaing selects are not empty or null,then pass else fail. how can i do that? here is my html <tr> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" name="home[0]"> <option value="">Select team</option> <option value="6">Seb FC</option> </select> </label> </td> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" name="away[0]"> <option value="">Select team</option> <option value="6">Sebo FC</option> </select> </label> </td> <td valign="middle" align="left"> <select class="t1-select" name="starthour[0]" style="width:55px"> <option value="00">00</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option></select></td> <td valign="middle" align="left"> <select class="t1-select" name="startminute[0]" style="width:55px"> <option value="00">00</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option><option value="32">32</option><option value="33">33</option><option value="34">34</option><option value="35">35</option><option value="36">36</option><option value="37">37</option><option value="38">38</option><option value="39">39</option><option value="40">40</option><option value="41">41</option><option value="42">42</option><option value="43">43</option><option value="44">44</option><option value="45">45</option><option value="46">46</option><option value="47">47</option><option value="48">48</option><option value="49">49</option><option value="50">50</option><option value="51">51</option><option value="52">52</option><option value="53">53</option><option value="54">54</option><option value="55">55</option><option value="56">56</option><option value="57">57</option><option value="58">58</option><option value="59">59</option><option value="60">60</option></select> </td> <!--<td valign="middle" align="left"> <select class="t1-select"name="endhour[0]"style="width:55px"> <option value="00">00</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option></select> </td> <td valign="middle" align="left"> <select class="t1-select"name="endminute[0]"style="width:55px"> <option value="00">00</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option><option value="32">32</option><option value="33">33</option><option value="34">34</option><option value="35">35</option><option value="36">36</option><option value="37">37</option><option value="38">38</option><option value="39">39</option><option value="40">40</option><option value="41">41</option><option value="42">42</option><option value="43">43</option><option value="44">44</option><option value="45">45</option><option value="46">46</option><option value="47">47</option><option value="48">48</option><option value="49">49</option><option value="50">50</option><option value="51">51</option><option value="52">52</option><option value="53">53</option><option value="54">54</option><option value="55">55</option><option value="56">56</option><option value="57">57</option><option value="58">58</option><option value="59">59</option><option value="60">60</option></select> </td>--> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" id="user_venue" name="venue[0]" style="width:55px"> <option value="">Select Venue</option> <option value="H">(H)HOME</option><option value="A">(A)AWAY</option><option value="N">(N)NEUTRAL</option> </select> </label> </td> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" id="user_stad" name="stadium[0]"> <option value="">Select Stadium</option> <option value="1">Nambo Stadium</option> </select> </label> </td> </tr> <tr> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" name="home[1]"> <option value="">Select team</option> <option value="6">Seb FC</option> </select> </label> </td> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" name="away[1]"> <option value="">Select team</option> <option value="6">Sebo FC</option> </select> </label> </td> <td valign="middle" align="left"> <select class="t1-select" name="starthour[1]" style="width:55px"> <option value="00">00</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option></select></td> <td valign="middle" align="left"> <select class="t1-select" name="startminute[1]" style="width:55px"> <option value="00">00</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option><option value="32">32</option><option value="33">33</option><option value="34">34</option><option value="35">35</option><option value="36">36</option><option value="37">37</option><option value="38">38</option><option value="39">39</option><option value="40">40</option><option value="41">41</option><option value="42">42</option><option value="43">43</option><option value="44">44</option><option value="45">45</option><option value="46">46</option><option value="47">47</option><option value="48">48</option><option value="49">49</option><option value="50">50</option><option value="51">51</option><option value="52">52</option><option value="53">53</option><option value="54">54</option><option value="55">55</option><option value="56">56</option><option value="57">57</option><option value="58">58</option><option value="59">59</option><option value="60">60</option></select> </td> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" id="user_venue" name="venue[1]" style="width:55px"> <option value="">Select Venue</option> <option value="H">(H)HOME</option><option value="A">(A)AWAY</option><option value="N">(N)NEUTRAL</option> </select> </label> </td> <td valign="middle" align="left"> <label for="tags"> <select class="t1-select" id="user_stad" name="stadium[1]"> <option value="">Select Stadium</option> <option value="1">Nambo Stadium</option> </select> </label> </td> </tr $hteam = $_POST['home']; $ateam = $_POST['away']; $starthour = $_POST['starthour']; $startminute = $_POST['startminute']; $venue = $_POST['venue']; $stadium = $_POST['stadium']; foreach($hteam as $key => $d ) { if((!empty($d)&& !empty($ateam[$key])&& ($ateam[$key]!= $d))/*&& empty($venue[$key])&& empty($stadium[$key])*/){ $mErrorMessage = 'Set full match details'; break;} } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 25, 2017 Share Posted February 25, 2017 (edited) you should validate each input separately and set up a specific error message for each input so that the user doesn't need to guess which value(s) was(were) not selected. you would validate any empty values first, before testing if the first two values are not the same. if you use an array of arrays to hold the error messages, with the main array index being the key/section value (0, 1, 2, ...) and the secondary array index being the select name, you can either output all the errors as a group at the start of each section or you can output each individual error at the select menu it corresponds to. you would also want to validate all the sections of data at once (no break; in the loop) so that you could display all the errors at once. by breaking out of the loop upon the first error, the user will have to fix the error(s) with one section before seeing if there are more errors later in the submitted data. if you set up an array that defines the form fields and the label for each, you can use this to produce your table heading and to dynamically validate the data and dynamically set up the error messages. i also hope you are dynamically producing the select menus, which would allow you to select any existing option choices should there be validation errors so that the user doesn't have to re-select everything when there is a validation error. Edited February 25, 2017 by mac_gyver Quote Link to comment Share on other sites More sharing options...
shaddf Posted February 25, 2017 Author Share Posted February 25, 2017 you should validate each input separately and set up a specific error message for each input so that the user doesn't need to guess which value(s) was(were) not selected. you would validate any empty values first, before testing if the first two values are not the same. if you use an array of arrays to hold the error messages, with the main array index being the key/section value (0, 1, 2, ...) and the secondary array index being the select name, you can either output all the errors as a group at the start of each section or you can output each individual error at the select menu it corresponds to. you would also want to validate all the sections of data at once (no break; in the loop) so that you could display all the errors at once. by breaking out of the loop upon the first error, the user will have to fix the error(s) with one section before seeing if there are more errors later in the submitted data. if you set up an array that defines the form fields and the label for each, you can use this to produce your table heading and to dynamically validate the data and dynamically set up the error messages. i also hope you are dynamically producing the select menus, which would allow you to select any existing option choices should there be validation errors so that the user doesn't have to re-select everything when there is a validation error. could you tweak the code to this end . Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.