raman Posted March 13, 2009 Share Posted March 13, 2009 I have a php script called query.php in which I am dynamically generating a table by retrieving data from Mysql. Against every entry retrieved from MYsql, there is a checkbox column, and there are 3 buttons on this page. All these 3 buttons lead to another php script called download.php. I want that the information should be passed to download.php only if there is atleast 1 checkbox selected. Otherwise it gives error saying invalid argument. I am trying a function called submitcheck written in a javascript file selectall.jsp which has many other functions including submitcheck but this function is not working. query.php $con= mysql_connect("localhost.localdomain","root","mypassword-blablabla"); if(!$con) { die('Could not connect:'.mysql_error()); } else { echo"<head><script type='text/javascript' src='selctall.jsp'> </script> </head>"; mysql_select_db("Protvirdb",$con); echo "<table border='1'><form id='field' action='downclus.php' method='post' onsubmit='return submitcheck('field')'>" echo"<input type='submit' value='Download as excel file.' name='excel' > <input type='submit' name='fasta' value='Download as fasta file.' > <input type='submit' name='clust' value='ClustalW alignment'>"; while($row=mysql_fetch_array($ret)) { $ort=$row['Organism']; $tre=$row['Category']; $key=$row['SNo']; echo "<tr>"; echo"<td><input type='checkbox' name='pro[]' value=\"$key\"></td>"; echo"<td><i>".$row['Organism']."</i></td>"; echo"<td>".$row['Category']."</td>"; echo"<td><a href=\"seq2.php?sn=$key\">".$row['Name']."</a></td>"; echo"<td>".$row['Brief_Description']."</td>"; echo "</tr>"; } } echo"</form></table><br>"; mysql_close($con); ?> selctall.jsp function submitcheck(field) { var comfList = document.forms[field].getElementsByName['pro[]']; if (comfList.length) { var selected = false; for (var i=0; i < comfList.length; i++) { if (comfList.checked) { selected = true; // is anyone selected/checked. break; } } if (!selected) { alert('Please select an entry to download.'); return false; } else { document.forms[field].submit(); return true; } } } Quote Link to comment Share on other sites More sharing options...
dgoosens Posted March 13, 2009 Share Posted March 13, 2009 I would not use a JS for this... I think it would be much easier to start your download.php script by checking if at least one checkbox was checked (a simple count() should do) if nothing was checked, you redirect back to query.php (with a message?) otherwise you process Quote Link to comment Share on other sites More sharing options...
raman Posted March 13, 2009 Author Share Posted March 13, 2009 I would not use a JS for this... I think it would be much easier to start your download.php script by checking if at least one checkbox was checked (a simple count() should do) if nothing was checked, you redirect back to query.php (with a message?) otherwise you process Yes that can be done, but the problem is that query.php itself has been generated by retrieving data from mysql and I don't have those variables in download.php to regenerate the same page query.php That's why I needed Javascript. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 13, 2009 Share Posted March 13, 2009 Yes that can be done, but the problem is that query.php itself has been generated by retrieving data from mysql and I don't have those variables in download.php to regenerate the same page query.php That's why I needed Javascript. Did you miss this part if nothing was checked, you redirect back to query.php (with a message?) otherwise you process You definitely need that validation server-side in case the user has JS diabled. BUt, adding JS validation is definitely a nice feature. It would have been helpful if you posted just sample HTML code since this is a JS issue. Use this function function submitcheck(formObj) { //Only validate if there are fields to validate if (checkOptions = formObj.elements['pro[]']) { if(checkOptions.length) { //chekOptions is an array (i.e. two or more options) for(var i=0; i<checkOptions.length; i++) { if (checkOptions[i].checked) { return true; } } //No options were selected return false; } else { //chekOptions is not an array (i.e. only one option) return checkOptions.checked; } } //There were no options on the form return true; } Change the onsibmit function to this echo "<table border='1'><form id='field' action='downclus.php' method='post' onsubmit='return submitcheck(this)'>"; 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.