0

I want to submit the form and go to the action url only if one checkbox is selected.For that I used javascript as well as php. But it's not working properly.Once I select only one checkbox and preceed after that if I go back to the same page with the given code and go for the edit option again, it is not keeping a check if more than 1 or no checkbox is checked. I'm not using checkboxes because for the same form i've a delete button which can delete multiple enteries at one time

<form name="form1" method="post" action="">
<input type="submit" name="Edit"  value="Edit" />

JavaScript

<script language=Javascript>    
function changeaction()
{document.form1.action='4a2edit.php';}
</script>

Php

<?php
if(isset($_POST['Edit'])){
$checkbox = $_POST['checkbox'];
if(count($_POST['checkbox'])>1)
{
echo"<br> <Font color=red> ***More than 1 checkbox selected.Make sure that only one checkbox must be selected.***</Font>";
}
else if (count($_POST['checkbox'])==0)
{
echo"<br><Font color=red> ***No checkbox is selected.***</Font>";
}   
else
{
echo "<script language=Javascript>  
changeaction();
</script>";
}}
?>

Sorry, the missing code is :

<form name="form1" method="post" action="">
<?php
//Some php code
while($row = mysql_fetch_array($result))
{
    echo "<input name=\"checkbox[]\" type=\"checkbox\" value=".$row['ID']."> ".$row['ID'];
}
?>

<input type="submit" name="Edit"  value="Edit" />
Karma
  • 11
  • 2
  • 1
    where is checkbox in html? You should validate using javascript – AnkiiG Nov 20 '15 at 11:42
  • sorry, i feel uneasy. anyway, to clarify your question we need your html code for your checkboxes. As won't send the information as array. But will send the entire checked checkboxes values. It's explained well in [this question](http://stackoverflow.com/questions/14026361/php-multiple-checkbox-array). As for validation, i believe you should keep server-side validatation, you won't know when *user* will throw input not from your form. – Bagus Tesa Nov 20 '15 at 12:20
  • If you want someone to select exactly one of a set why are you using checkboxes in the first place? Checkboxes are designed for the state zero or more. Radio buttons are designed for the Exactly 1 state. – Quentin Nov 20 '15 at 12:34
  • But Quentin I'm also using a delete button on the same page which can delete multiple enteries at a time. – Karma Nov 20 '15 at 12:36

0 Answers0