I have been trying to create php mail form that has input fields, radio buttons and check boxes. Currently stuck with collecting info from check boxes… I tried multiple ways but either getting only one checkbox title or ‘Array’
<form id="customer_form" action="mail_handler.php" method="post" class="form-horizontal" style="background-color: rgba(255,255,255,0.1)" autocomplete="off" onsubmit="alert('Danke für die Information. Ich werde bald antworten.');";>
First thing, stop creating a bunch of variables for nothing. Secondly, this code is vulnerable to an email header injection attack. The From should be from the server this resides on, not from random spammer using this. I also do not see a proper form here.
I am only beginner and learning through the php, so not sure what you are referring to as “bunch of variables” (can only assume it related to php check boxes code?)
I also don’t know how this code would be vulnerable without further explanation.
Does it mean I can just use direct value such as $_POST[‘Q1’]?
I have read quick article about the injection attack - it was on my list to implement some sort of character validation, but right now, it does not do what I want to start with…
As you are already using the variable $q6 in your email message you should define it so there are no errors when q6 checkboxes are not checked. You can then loop through them as Benanamen described and concatenate them together with the line breaks you wish to have. Something like this.
Thanks, this seems to work fine. I have modified the output lines as I prefer to have in line rather than list.
Since I have two questions with checkboxes, I wrote second code based on first one, but I am not getting any results (for q8). I made assumption that $k was local variable and for second question changed to $g - is this correct?
In this case, it does happen contain the array key / index of each element of $_POST['Q6'], within the scope of the foreach() loop. In that sense it’s a local variable, try to use it outside the foreach() and you won’t be able to. But the name could be anything, and because you have two separate foreach() loops, it wouldn’t matter if you used the same variable names for the loop in both of them. If they were nested within each other, using the same names would cause a problem.
That’s not exactly true. While the array would not be available outside the loop as coded, the last element of the key and the value would be. You could unset them but I have never seen a need to do that.