Send value checkbox in mail

Hallo,
i have problem with sending mail from my form checkboxes

It’s my code:

<input type="checkbox" name="wiztype[]" value="value1">
<input type="checkbox" name="wiztype[]" value="value2">
<input type="checkbox" name="wiztype[]" value="value3">
<input type="checkbox" name="wiztype[]" value="value4">
<input type="checkbox" name="wiztype[]" value="value5">

I use javascript to validate form, there i have

var wiztype = $("input[name='wiztype[]']:checked").serializeArray();

PHP sender code:

$wiztype = $_POST["wiztype"];

if(!empty($_POST['wiztype'])) {
foreach($_POST['wiztype'] as $wiztypelist) {
        $wiztypelist;
}
}

Mail is sending, but instead values from boxes i have:

[object Object],[object Object] (x number of selected boxes)

I would like to get values from checked boxes, but i’m not very good in php
Please help me :slight_smile:

That’s not a PHP problem. It looks like you pass the JS wiztype variable directly into jQuery AJAX, which doesn’t work (serialize() would be the correct one to use).

With (serialize() still doesn’t work, i show all code php:

$wiztype = $_POST["wiztype"];

if(!empty($_POST['wiztype'])) {
foreach($_POST['wiztype'] as $wiztypelist) {
	$wiztypelist = $wiztypelist;
}
}

...

$Body .= "Type: ";
$Body .= $wiztypelist;
$Body .= "\n";

Again, this is a JS problem, not a PHP one.

Ok, soryy, tis is my JS code:

function submitForm(){
    // Initiate Variables With Form Content
	var profile = $("input[name=profile]:checked").val();
	var profil2 = $("input[name=profil2]").val();
	var wiztype = $("input[name='wiztype']:checked").serialize();
    var scale = $("input[name=scale]").val();


    $.ajax({
        type: "POST",
        url: "assets/php/form-process.php",
		data: "profile=" + profile + "&profil2=" + profil2 + "&wiztype=" + wiztype + "&scale=" + scale,
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}

but I do not know completely what’s wrong

You try to pass an array where a string should go (hence the weird output). I’d rather re-work the form data collection than trying to fix that somewhat fragile script.

see also:

Ok i will try it.
Thank you

for specifically selecting I’d use a common class on the inputs, so data collection becomes as simple as:

var data = $('input.some-class').serialize()

Ok so i wrote:

var wiztype = $("input.wiztype:checked").serialize();
	console.log(wiztype.toString());

and now in mail i got “Array” text

Now you’re printing an array in PHP …

hmy… my foreach doesn’t work, I will try do something but i completly don’t know what i should to do. Many thanks

Since I don’t know your actual PHP & JS code, I don’t know either.

Thank You for help,

Now i have some like this:

HTML

<input type="checkbox" name="wiztype" value="1" class="wiztype">
<input type="checkbox" name="wiztype" value="2" class="wiztype">
<input type="checkbox" name="wiztype" value="3" class="wiztype">

JS


function submitForm(){
    // Initiate Variables With Form Content
	var profile = $("input[name=profile]").val();
	var wiztype = $("input.wiztype:checked").serialize();
	console.log(wiztype.toString());
	


    $.ajax({
        type: "POST",
        url: "assets/php/form-process.php",
		data: "profile=" + profile + "&wiztype=" + wiztype,
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}

PHP

...

$arr = array($_POST['wiztype']);
foreach ($arr as &$wiztypevalue) {
    $wiztypevalue = $value;
}
...

$Body .= "Type: ";
$Body .= $wiztypevalue;
$Body .= "\n";

I know, it’s terrible but i’m completely amateur

No wonder this isn’t working …

In JS you made a mix of old & new, which was bound to fail (just have a look at the submitted request in the browser’s dev tools). I meant it when I said “a common class for all inputs”.

To make things worse you changed the input names so PHP won’t recognise them as array.

And the PHP code is beyond help. If you want to make an array into a string, one of the first functions to consider is implode() (if the array is not nested).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.