HTML FILE IS:

<html>

<body  bgcolor="yellow" text="red" >
<form method="get" action="cookie6.php">
<p>Which car you want to buy?<P>
<TABLE border=2>
<TR><TD><input type="checkbox" value ="15"  name="text[]"><TD>Ferrari<TD bgcolor="blue" width="15px"> 15million</td></TR>
<TR><TD><input type="checkbox" value ="35"  name="text[]"><TD>Mercedes<TD bgcolor="red"width="15px"> 35million</td></TR>
<TR><TD><input type="checkbox" value ="50"  name="text[]"><TD>Bugatti<TD bgcolor="yellow"width="15px">50million</td></TR>
</TABLE>

<input type="hidden" value="1" name="check" >
<p>
<input type="submit" value="refresh" >
</form>
</body>
</html>

PHP FILE IS :

<?php
// echo "you want to buy<P>";
// if(isset($_GET['check']))
// {$car=array();
// if(isset($_GET['text1']))
// {echo $_GET['text1']."	ferrari<br>";}
// else {}
// if(isset($_GET['text2']))
// {echo $_GET['text2']."	merc<br>";}
 // else {}
 // if(isset ($_GET['text3']))
// {echo $_GET['text3']."	bugatti<br>";}
 // else {}
// }
if(isset($_GET['check']))
{	
	$sel_cars="";
	$count=count($_GET['text']);
	for($i=0;$i<$count;$i++)
	{	
		$sel_cars=$sel_cars.$text[$i]." ";
	}
}


?>

This is a basic code for finding which cars user selected.My program works if the checkboxes in the HTML file have different names but not if they have the same name.
Please I have tried it for more than 2 hours .....
What is the solution if checkboxes have the same name?

Dani AI

Generated

, when multiple checkboxes share the same name with the [] suffix, PHP collects their values as an array. The reason your first attempt failed is that you were reading from an undefined variable $text instead of the $_GET['text'] array. pointed you in the right direction by highlighting the superglobal. Two quick guardrails: do not rely on the hidden check field to decide if anything was selected; and never call count() on a variable that might be null (modern PHP warns on that).

A simple, robust pattern is to retrieve the array safely, normalize types, and then map IDs to labels so you can show friendly names instead of raw numbers:

<?php
// cookie6.php
$ids = filter_input(INPUT_GET, 'text', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$ids = is_array($ids) ? array_map('intval', $ids) : [];

$catalog = [
    15 => 'Ferrari',
    35 => 'Mercedes',
    50 => 'Bugatti',
];

$selected = [];
foreach ($ids as $id) {
    if (isset($catalog[$id])) {
        $selected[] = $catalog[$id] . " ({$id} million)";
    }
}

echo $selected
    ? 'You selected: ' . htmlspecialchars(implode(', ', $selected), ENT_QUOTES, 'UTF-8')
    : 'No cars selected.';

Tips you can apply right away:

  • Prefer checking isset($_GET['text']) or using filter_input(..., FILTER_REQUIRE_ARRAY) instead of a hidden check flag.
  • If this is more than a demo, switch the form to method="post" to avoid exposing choices in the URL.
  • Always validate and cast incoming values (e.g., intval) before using them, and escape any output with htmlspecialchars to prevent XSS.

This is related to register globals*, you were using $text[$i] , if you don't define variable $text, then you get an error:

<?php
if(isset($_GET['check']))
{	
	$sel_cars="";
	$count=count($_GET['text']);
	for($i=0;$i<$count;$i++)
	{
		echo $_GET['text'][$i]." ";
	}
}
?>

Or use:

$text = $_GET['text'];
echo $text[$i]." ";

Bye :)

* http://php.net/manual/en/language.variables.predefined.php

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.