Hi all

I have a checkboxes form. How can I get the results displayed (in a new window lets say) when submiting it?

Thank you

P.S. I care about getting results displayed part.

Dani AI

Generated

As noted, only checked boxes are submitted. The reason saw the value "on" is that an <input type="checkbox"> without a value attribute submits the literal string "on" by HTML spec. To display readable names (home, home2, etc.) either give each checkbox an explicit value or collect the checked items into an array and loop them on the processing page. See the HTML spec summary at MDN: input type checkbox.

A simple, robust pattern (show results in a new window and print the selected names):

<form method="post" action="process.php" target="_blank">
  <input type="checkbox" name="places[]" value="home"> Home
  <input type="checkbox" name="places[]" value="home2"> Home2
  <input type="submit" name="submit" value="Show">
</form>
// process.php
if (!empty($_POST['places'])) {
  foreach ($_POST['places'] as $p) {
    echo htmlspecialchars($p, ENT_QUOTES, 'UTF-8') . "<br>\n";
  }
}

If checkboxes were given unique names (home, home2, ...) use isset() to decide which to print. To re-render the form with boxes still checked, output the HTML checked attribute based on the submitted data (the attribute controls rendering only; it is not the posted value).

Quick troubleshooting tips: inspect the POST payload with browser DevTools or use a debug dump (var_export/print_r) on $_POST to see exactly what was sent (as suggested). Always set explicit value attributes if specific text is needed, and escape output with htmlspecialchars to avoid XSS when showing submitted values.

Recommended Answers

All 8 Replies

Well, when you submit the form, only the selected checkboxes will be posted to the next page. You can see what values are being posted by using print_r($_POST['checkboxname'] You can loop through $_POST (since that will be an array of checked values) and display the data however you want !

This is asample of my code:

$home='unchecked';
$home2='unchecked';

if (isset($_POST['submit'])) {
if (isset($_POST['home'])) {
$home = $_POST['home'];
if ($home == 'home') {$home = 'checked';}}

I add

echo "$home";
echo"home2";

But the echo I get if i check only 'home' is: on unchecked

What sould I change to display only the checked checkboxes end with their name (home, home2, etc.).

I first tried what you said but I got nothing.

[The same as I get in my db... (but here is no problem)]

Can you post your latest code ?

Is too long cause i don't have only 3 boxes.

Is just like I show above including my db connection.

STARTS LIKE I JUST POSTED [$home='unchecked';] etc
AND ENDS WITH MY ECHO.

Nothing else.

I don't get it. Here is a simple example.

<?php
if(isset($_POST['submit'])) {
	print_r($_POST['check']);
}
?>
<html>
<body>
<form method="post" action="checkbox.php">
<input type="checkbox" name="check[]" value="1"> 1 <br />
<input type="checkbox" name="check[]" value="2"> 2 <br />
<input type="checkbox" name="check[]" value="3"> 3 <br />
<input type="checkbox" name="check[]" value="4"> 4 <br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Oh.. was playing with procces.php and not the form.... I will give it a try

my next idea was

to replace

$home='unchecked';
with
$home='';

and

$home = 'checked';
with
$home = 'home';

Still no resaults with your way..

with mine replacing $home='unchecked';
with $home='';

I don't get desplayed anything when not checked. so i am ok here.

but still when is checked i get the word on..

hat do I have to change in order to get the word home or whatever i wand to get????

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.