Jump to content

quick question hopefully. Replace option box with checkboxes


rachae1

Recommended Posts

Hi hopefully this will be easy for somebody to help me with.

 

I have a form that adds products to the database. The form has a drop down box that picks up a category that is already in the database.

 

My form works fine with the option box but now I want to be able to add multiple categories to the product and  I was wondering how I change this code to checkboxes:

 

Name:<input type='text' name='name'><br/>

Title:<input type='text' name='title'><br/>

Category:
<select name='tags'>

<?
$query = mysql_query("SELECT * FROM categorys ORDER BY id ASC") or die(mysql_error());
while($row=mysql_fetch_array($query))
{
echo "<option value='$row[id]'>$row[tags]";
} 
?>

</select>
<input name="submit" type="submit" value="add_product" />
</form>

 

 

I tried to change it to <input type='checkbox' name='tags[]' value='$row[id]'>$row[tags] but it didnt work.

 

 

Hi thanks for the reply,

 

Ok I think I have figured part of it out,  I have tried the implode function but now it is only posting the first value in the checkboxes.

 

This is my back end code:

 

if ($submit == 'add_product') {
       
foreach($_POST['tags'] as $value) { $value="'".$value."'"; }
$tags=implode(",", $_POST['tags']);


        $name = mysql_real_escape_string(strip_tags($_POST['name']));
        $title = mysql_real_escape_string(strip_tags($_POST['title']));
        $category = mysql_real_escape_string(strip_tags($_POST['category']));
        $date = date("m/d/Y");
        $time = time();

        //we begin error checking....
        $error_msg = array();
        if(empty($name))
        {
            $error_msg[] = "Please insert a name!<br />";
        }


        //print the errors, if any
        if(count($error_msg)>0)
        {
            echo "<strong>ERROR:</strong><br>n";
            foreach($error_msg as $err)
                echo "$err";
        }
        //everythings ok, insert it to the DB
        else
        {	
$sql = "INSERT INTO tutorials (submitter,  title, cat_id, date_submitted, time_submitted) 
VALUES ('$name',  '$title', '$tags', '$date', '$time')";
            mysql_query($sql) or die(mysql_error());
            echo "Tutorial added for review!";
        }
    }

 

Do I need to change something in the database at the moment the  cat_id is set to int(25)

this line needs to be change to

foreach($_POST['tags'] as &$value) { $value="'".$value."'"; }

so $value is passed as reference and is changed back in the original array when you change it

and you proberly  want to add a escape the tags value before you put it into your database try adding this to the script

$tags = mysql_real_escape_string(strip_tags($tags));

 

Scott.

Hi,

 

I have just added that code in but I am still only getting one value added under cat_id. :S

 

This is the amended code

 


if ($submit == 'add_product') {
       
foreach($_POST['tags'] as &$value) { $value="'".$value."'"; }


	$tags=implode(",", $_POST['tags']);

	$tags = mysql_real_escape_string(strip_tags($tags));
        $name = mysql_real_escape_string(strip_tags($_POST['name']));
        $title = mysql_real_escape_string(strip_tags($_POST['title']));
        $category = mysql_real_escape_string(strip_tags($_POST['category']));
        $date = date("m/d/Y");
        $time = time();

        //we begin error checking....
        $error_msg = array();
        if(empty($name))
        {
            $error_msg[] = "Please insert a name!<br />";
        }


        //print the errors, if any
        if(count($error_msg)>0)
        {
            echo "<strong>ERROR:</strong><br>n";
            foreach($error_msg as $err)
                echo "$err";
        }
        //everythings ok, insert it to the DB
        else
        {	


            $sql = "INSERT INTO tutorials (submitter,  title, cat_id, date_submitted, time_submitted) 
		VALUES ('$name',  '$title', '$tags', '$date', '$time')";
            mysql_query($sql) or die(mysql_error());
            echo "Tutorial added for review!";
        }
    }



Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.