I want to pass the checkbox values from my form to another PHP file upon clicking the next hyperlink. But I am unable to do so. I have javascript in my form which checks that only one checkbox is selected. To use this javascript, I need to name all my checkboxes the same. but I want to be still able to post the values to the php file for further processing.
Here is my code

Form code

<html>
<head>
<script type="text/javascript">
<!-- Begin
function checkBoxValidate(cb) {
for (j = 0; j < 8; j++) {
if (eval("document.myform.ckbox[" + j + "].checked") == true) {
document.myform.ckbox[j].checked = false;
if (j == cb) {
document.myform.ckbox[j].checked = true;
         }
      }
   }
}
//  End -->
</script>
</head>
<body>

<br>
<br><br>
<form name=myform>

<p>Please choose a variable to incrementally create your form  
<div style " 
	margin-top:25px;
	margin-left:50px;
	margin-right:50px;
	margin-bottom:25px;
">

<div style = "width:500px;height:275px;border:2px solid blue;
	padding-top:25px;
	padding-left:100px;
	padding-right:100px;
	padding-bottom:5px;
">

<table border = "1">

<tr>
<th>Choose One</th>
<th>Element Name </th>
<th>Element</th>
<tr>
<td><input type = "checkbox" name = "ckbox" value = "txt_box"
onClick="javascript:checkBoxValidate(0)"></td>
<td>Textbox</td>
<td><input type = "textbox" name = "text_box"></td>

</tr>

<tr>
<td><input type = "checkbox" name = "ckbox" value = "list_box"
onClick="javascript:checkBoxValidate(1)"></td>
<td>List Box</td>
<td><select>
<option value="ListItem1"></option>
  <option value="ListItem1">ListItem1</option>
  <option value="ListItem2">ListItem2</option>
  <option value="ListItem3">ListItem3</option>
  <option value="ListItem4">ListItem4</option>
</select></td>

</tr>

<tr>
<td><input type = "checkbox" name = "ckbox" value = "chk_box"
onClick="javascript:checkBoxValidate(2)"></td>
<td>Checkbox</td>
<td><input type = "checkbox" name = "checkbox"></td>

</tr>

<tr>
<td><input type = "checkbox" name = "ckbox" value = "radio"
onClick="javascript:checkBoxValidate(3)"></td>
<td>RadioButton</td>
<td><input type = "Radio" name = "Radio"></td>

</tr>
</form>
</table>

</div>

<a href = "AppForm1.html" >Back</a>&nbsp&nbsp&nbsp
 
<a href = "checkboxSubmit.php" >Next >></a>

php code

<?php
   

    echo $_POST['ckbox'];
    
   
?>

</body>
</html>

Recommended Answers

All 6 Replies

Use radio buttons. That's what they are for: selection of one of multiple values.

Use radio buttons. That's what they are for: selection of one of multiple values.

The constraint of my application is that they need to be a checkboxes.

In the processing script, walk through the $_POST array and check which item of ckbox has a value assigned. Test it:

<form method='post'>
  <input type='checkbox' name='cb[]' value='a'>
  <input type='checkbox' name='cb[]' value='b'>
  <input type='submit' value='submit'>
</form>

<?php
  print_r( $_POST );
?>

In the processing script, walk through the $_POST array and check which item of ckbox has a value assigned. Test it:

<form method='post'>
  <input type='checkbox' name='cb[]' value='a'>
  <input type='checkbox' name='cb[]' value='b'>
  <input type='submit' value='submit'>
</form>

<?php
  print_r( $_POST );
?>

Thanks,but the problem is a little different. I cannot have a submit button, instead I need to have a hyperlink to Next>> text. When the user clicks next, the form has to do a POST to the php file. Also, I think I might not be able to have an array as the javascript to enable only 1 checkbox needs the name attribute to be a non array.

Use the id attribute in Javascript and the name attribute in the PHP form processing.
For processing it is irrelevant if you have a submit button or a javascript function calling form.submit().

Use the id attribute in Javascript and the name attribute in the PHP form processing.
For processing it is irrelevant if you have a submit button or a javascript function calling form.submit().

Thanks very much for your suggestion. For some reason, my Javascript form.submit() did not work. I negotiated this entire problem by adding a submit button with a value ="Next>>". That solved my issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.