Jump to content

Recommended Posts

Hi,

 

I have been trying to learn checkboxes and arrays, and I've finally figured out how to add the checkboxes to the database.

 

I currently have two forms  addlinks.php and modifylinks.php and I have a dolinks.php that submits my data to the database.

 

What I would like to do now is on my modifylinks page show a tick in the boxes to show that they have already been added.

 

At the moment if I dont tick any boxes on modifylinks.php it deletes my previous data, and I have to retick them everytime I do an update.

 

heres some code:

 

addlinks.php

<input type="checkbox" name="category[]" value="apple">apple
<input type="checkbox" name="category[]" value="oranges">oranges

 

dolinks.php

 

if ($submit == 'Add') {
if ($linkName == ""){
	echo forwardScript($href);
	exit;
} else {

        $category = '*'.implode('*',$_POST['category']).'*';

	$links_query  = "INSERT INTO link ";
	$links_query .= "(category, name, description) ";
	$links_query .= "VALUES ("; 
	$links_query .= "'" . $category . "',";
	$links_query .= "'" . $name . "',";
	$links_query .= "'" . $description . "'";
                $links_query .= ");";

$link_query_result = domysql($links_query, 'Adding a link' . $_SERVER['PHP_SELF']); 
}
}


if ($_POST['submit'] == 'Modify') {
if ($linkName == ""){
	echo forwardScript($href);
	exit;
} else {

$category = '*'.implode('*',$_POST['category']).'*';

	$links_query  = "UPDATE link SET ";
	$links_query .= "category ='" . $category . "',";
	$links_query .= "name ='" . $name . "',";
	$links_query .= "description ='" . $description . "'";
                $links_query .= "WHERE id LIKE '" . $linkid . "';";
	$link_query_result = domysql($links_query, 'Modifying a link' . $_SERVER['PHP_SELF']);
}
}


 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/69352-checkboxes/
Share on other sites

Here is a function on how I do it... although I may start using the method phat_hip_prog showed. It's pretty cool. But I digress. Here is my code

 

<?php
	connectdb_com(); // make connection
	$query="SELECT * FROM ammenities ORDER BY ammenity ASC"; //setup query
	$result=mysql_query($query);//run query
	while($row=mysql_fetch_object($result)) //start loop to get results
	{
		if(in_array($row->ammenityid,$_ammenities))
		{ 
// $_ammenities comes from another query. I store multiple id's in 1 field separating them with a space
//then I use explode on that string to create my $_ammenities array, I then use this if statement to 
// determine if the id in my query above is in the $_ammenities array 
//if it is, then I echo a checked="checked" in there, if it is not, then I don't.

			echo '<label><input type="checkbox" name="ammenities[]" value="'.$row->ammenityid.'" id="ammenities[]" checked="checked">'.$row->ammenity.'</label><br>';
		}
		else
		{
			echo '<label><input type="checkbox" name="ammenities[]" value="'.$row->ammenityid.'" id="ammenities[]">'.$row->ammenity.'</label><br>';
		} 

	}
?>	

 

Not a real complex piece of code, so I hope it helps ya.

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348462
Share on other sites

I have a wrapper for virtually every form element, and again for fitting into my standard table! LOL

 

I would be interested in seeing how you do that. I am working on redoing my companies .com site and we got some pretty lengthy forms e.g. Employment App, Franchise App, Contact forms etc.

 

These freaking things get pretty tedious especially when the forms are over 20 fields.

 

Hell, I was happy that I finally realized there is an easier way to assign friendly names to the field $_POST vars

 

foreach($_POST as $k=>$v)
{
	echo '$'.$k.'=trim($_POST[\''.$k.'\']);<br>';
}

 

For a field called fname it will return

 

$fname=trim($_POST['fname']);

 

Makes it a lot easier to get 30-40 fields all named properly and such. Good stuff though, I may be trying to come up with some things like that to make development easier.

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348479
Share on other sites

I develop on about 3 different pc's. work desktop, work laptop, home desktop.... I believe in at least one of em I got the injection piece in there. The ones I am working with now are not inserted in the DB, they are just emailed so I am not concerned about injection.

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/69352-checkboxes/#findComment-348483
Share on other sites

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.