Jump to content

Recommended Posts

I have data in the table products as follows

,cid,cat,,,
,'1','Furniture',,,
,'2','Toys',,,
,'3','Clothes',,,
,'4','Food',,,

then i created this simple script

include('db.php');
$cxn = mysqli_connect($host,$user,$pwd,$db) or die ("can't connect");
$query = "SELECT DISTINCT cat FROM products ORDER BY cat";
$result = mysqli_query($cxn,$query) or die ("can't connect");
echo "<form action='process.php' method='post'>";
while($row = mysqli_fetch_assoc($result))
{
  extract($row);
  echo "<input type='checkbox' name='category[$cat]' value='$cat' />\n";
  echo "<label for='$cat'>$cat</label>";
}
echo "<input type='submit' value='select category' />";
echo "</form>";

 

the question now is, why is that after i selected a category from the check box,

then onclicked the submit button. the output says

 

"Category = Array"

 

I don't know what am missing in my code, help me configure this thing out Sir/s ???

Link to comment
https://forums.phpfreaks.com/topic/123360-solved-form-checkbox-help/
Share on other sites

Because you set it up to be an array in your form.

 

After the submit button is pressed, do this to see what the array contains

echo '<pre>',print_r($_POST['category']),'</pre>';

 

I already have a 'process.php' that outputs an the $_POST array sir

<?php
echo "<html><head><title>Hello Telephone</title></head>\n";
echo "<body>\n";
echo "<ol>\n";
foreach($_POST as $field => $value)
{
    echo "<li>$field = $value</li>\n";
}
echo "</ol>\n";
echo "<form action='dryrun.php' method='GET'>\n";
echo "<input type='submit' value='back' />\n";
echo "</form>\n";
echo "</body>\n";
echo "</html>\n";
?>

 

i got confused more with your tip :( i don't understand what to do now lol  ;D

Try chaning

foreach($_POST as $field => $value)

 

To

foreach($_POST['category'] as $field => $value)

 

i did just did your tip .. and it ruined the supposed to be output, istead of

 

1. Category = Furniture ...the output is now Furniture = Furniture  :(

 

and also the other blocks that uses my 'process.php' code where ruined

here's my actual practice code

<?php
include('db.php');
echo "<html>\n";
echo "<head><title>Practice Shit</title></head>\n";
echo "<body>\n";
// CUSTOMER DETAIL
echo "<fieldset><b><legend>Customer</legend></b>\n";
$labels = array("fname" => "first name",
                "lname" => "last name",
                "phone" => "phone number");

$submit = "details";
echo "<div align='center'>\n";
echo "<form action='process.php' method='POST'>\n";
foreach($labels as $field => $label)
{
    echo "<label for='$field'>$label</label> \n";
    echo "<input type='text' name='$field' size='15%' maxlength='15'/>\n";
}
echo "<input type='submit' value='$submit'>\n";
echo "</form>\n";
echo "</div>\n";
echo "</fieldset>\n";
// CATEGORIES MENU
$cxn = mysqli_connect($host,$user,$pwd,$db) or die ("can't connect");
$query = "SELECT cat FROM products ORDER BY cat ASC";
$result = mysqli_query($cxn,$query) or die ("can't connect");
echo "<fieldset><b><legend>Categories</legend></b>\n";
echo "<div align='center'>\n";
echo "<form action='process.php' method='POST'>\n";
echo "<select name='Category'>\n";
while($row = mysqli_fetch_assoc($result))
{
  extract($row);
  echo "<option value='$cat'>$cat</option>\n";
}
echo "</select>\n";
echo "<input type='submit' value='select' category'>\n";
echo "</form>\n";
echo "</div>\n";
echo "</fieldset>\n";

//DATE
$months = array("January","February","March","April","May",
                "June","July","August","September","October","November",
                "December");

echo "<fieldset><b><legend>Date</legend></b>\n";
echo "<div align='center'>\n";
echo "<form>\n";
echo "<select name='Month'>\n";
foreach($months as $value)
{
  echo "<option value='$value'";
  if($value == date('F',time()))
  {
    echo " selected ";
  }
  echo "> $value</option>\n";
}
echo "</select>\n";
echo "<select name='Day'>\n";
for($i=1;$i<=31;$i++)
{
    echo "<option value='$i' ";
    if($i == date('d',time()))
    {
      echo " selected ";
    }
    echo "> $i</option>\n";
}
echo "</select>\n";
echo "<select name='Year'>\n";
$year = date('Y');
for($i=$year;$i<=$year+3;$i++)
{
  echo "<option value='$i'";
  if($i == $year)
  {
    echo " selected ";
  }
  echo "> $i </option>\n";
}
echo "</select>\n";
echo "</form>\n";
echo "</div>\n";
echo "</fieldset>\n";
//RADIO BUTTONS

echo "<fieldset><b><legend>Categories</legend></b>\n";
echo "<div align='center'>\n";
echo "<form action='process.php' method='POST'>\n";
$query2 = "SELECT cat FROM products ORDER BY cat";
$result3 = mysqli_query($cxn,$query2) or die ("can't connect");
while($row = mysqli_fetch_assoc($result3))
{
  extract($row);
  echo "<input type='radio' name='Category' value='$cat' />$cat\n";
  echo "<br/>\n";
}
echo "<input type='submit' value='select category' />";
echo "</form>";
echo "</div>\n";
echo "</fieldset>";
echo "<fieldset><b><legend>CheckBox</legend></b>\n";
$query = "SELECT DISTINCT cat FROM products ORDER BY cat";
echo "<form action='process.php' method='post'>\n";
$result4 = mysqli_query($cxn,$query) or die ("can't connect");
while($row = mysqli_fetch_assoc($result4))
{
  extract($row);
  echo "<input type='checkbox' name='category[$cat]' id='$cat' value='$cat' />\n";
  echo "<label for ='$cat'>$cat</label>\n";
}
echo "<input type='submit' value='select category'>\n";
echo "</fieldset>\n";
echo "</body>\n";
echo "</html>\n";
mysqli_close($cxn);
?>

 

what should i do now? all other blocks are using that process.php so when i changed the $_POST attribute ..the code gone crazy :(

Okay, lets try this.

 

Erase this

echo "<ol>\n";
foreach($_POST as $field => $value)
{
    echo "<li>$field = $value</li>\n";
}
echo "</ol>\n";

 

and put in

echo '<pre>',print_r($_POST),'</pre>';

 

Copy and paste what that gives you.

 

Okay, lets try this.

 

Erase this

echo "<ol>\n";
foreach($_POST as $field => $value)
{
    echo "<li>$field = $value</li>\n";
}
echo "</ol>\n";

 

and put in

echo '<pre>',print_r($_POST),'</pre>';

 

Copy and paste what that gives you.

 

 

and now this is the ouput after doing that

 

jtbd5u.jpg:(

So what's the problem with the output, how do you WANT it to be? The output makes sense to me if your only checking one checkbox. Is that what your doing?

 

the out put is supposed to be, let's say I clicked the checkbox with name Furniture,

it should be

 

1.) Category = Furniture

 

that's why the rest of the code are being messed up if I change the $_POST in process.php

I was able to configure the problem myself, first, i created a process2.php  for the checkbox to use

in processing the submit button

$category = array("Clothes","Food","Furnitures","Health","Toys");
echo "<ol>\n";
foreach($_POST as $category)
{
foreach($category as $field => $value)
{
	echo "<li>Category = $value</li>\n";
}
}

 

cheers :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.