I am trying to find a way to retain the condition of a selection of checkboxes and radio-buttons on an html form using php. I have looked at the similar posts that deal with this type of query but I haven't found a solution.

I have used the following php code to retain text information on the same form and have tried to modify it for checkboxes but without success:

<input type="text"name="Date"
 value="<?php echo isset($_POST['Submit'])?htmlspecialchars($_POST['Date']):''?>>

There is a slight complication (especially for a Noob like me) but some of the checkboxes are coded as an array, for example:

<input type="checkbox"name="ANin[]"value="AN1_in">
<input type="checkbox"name="ANin[]"value="AN2_in">
<input type="checkbox"name="ANin[]"value="AN3_in">
<input type="checkbox"name="ANin[]"value="AN4_in">

and so on. The reason for the arrays is that currently the values are being written to Log.txt file when the Submit button is pressed and will eventually be sent across to a Python script so it makes it easier to send a large set of values using a basic for loop.

I have tried replacing the value= with the php script detailed above in the hope that the checkbox would retain its condition but on pressing Submit the checkboxes go back to the original state.

I would really be grateful of any ideas as this has been troubling me for some time. Thanks in advance

有帮助吗?

解决方案

<input type="checkbox" name="ANin[]" value="AN1_in" <?php if(in_array('AN1_in',$_POST['ANin'])) echo 'checked'; ?>>

and so on for AN2_in, AN3_in etc. Rather than repeating this manually for all 4 (or more) checkboxes, you could also write a simple loop to generate all of them with proper values.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top