1

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

1 Answer 1

1
<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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi lafor, thanks for the reply unfortunately it doesn't seem to be working. The code runs ok but how do I incorporate the submit button push into it as I did for the text input. I have tried using the 'isset' query but it doesn't seem to like it.
My apologies lafor, I had got one of my own variables names wrong!! Now it works fine, awesome, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.