10 Replies - 4044 Views - Last Post: 10 August 2011 - 08:31 AM Rate Topic: -----

#1 ozzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 11

Insert multiple checkbox and hidden form into database

Posted 09 August 2011 - 03:14 PM

Hello, first time here.

Using MagpieRSS I manage to display RSS feeds from other websites. What I would like to do is to let users to choose their own links and insert them into MySQL database. Here's the HTML code:
<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<ul>
    <li><input type="checkbox" name="item1[]" value="1" /><a href="http://link.com/one">Link one title</a>
    <input type="hidden" name="item1[]" value="Link one title" />
    <input type="hidden" name="item1[]" value="http://link.com/one" />
    <input type="hidden" name="item1[]" value="Link one description" />
    <input type="hidden" name="item1[]" value="Link one date" />
    </li>
    <li><input type="checkbox" name="item2[]" value="2" /><a href="http://link.com/two">Link two title</a>
    <input type="hidden" name="item2[]" value="Link two title" />
    <input type="hidden" name="item2[]" value="http://link.com/two" />
    <input type="hidden" name="item2[]" value="Link two description" />
    <input type="hidden" name="item2[]" value="Link two date" />
    </li>
    <li><input type="checkbox" name="item3[]" value="3" /><a href="http://link.com/three">Link three title</a>
    <input type="hidden" name="item3[]" value="Link three title" />
    <input type="hidden" name="item3[]" value="http://link.com/three" />
    <input type="hidden" name="item3[]" value="Link three description" />
    <input type="hidden" name="item3[]" value="Link three date" />
    </li>
    <li><input type="checkbox" name="item4[]" value="4" /><a href="http://link.com/four">Link four title</a>
    <input type="hidden" name="item4[]" value="Link four title" />
    <input type="hidden" name="item4[]" value="http://link.com/four" />
    <input type="hidden" name="item4[]" value="Link four description" />
    <input type="hidden" name="item4[]" value="Link four date" />
    </li>
</ul>
<input type="submit" />
</form>

The problem is, I don't know how to insert them (only with the chosen ones) into database. Would someone help me?
Thanks.

This post has been edited by ozzo: 09 August 2011 - 03:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Insert multiple checkbox and hidden form into database

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Insert multiple checkbox and hidden form into database

Posted 09 August 2011 - 03:30 PM

Adding [] at the end of an element name tells PHP that we're dealing with a list. Therefore you'll find that if you iterator over $_POST['item1'], $_POST['item2']...etc you'll get the values of the selected checkboxes.

Visit our PHP Tutorials sections if you don't know how to insert data into your database.
Was This Post Helpful? 1
  • +
  • -

#3 ozzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 11

Re: Insert multiple checkbox and hidden form into database

Posted 09 August 2011 - 04:00 PM

Thank you for your reply.
I do know how to insert the data into database (the SQL commands) what I don't know is how to filter the data I mention above or how to check which links are chosen before I insert them into database.
Was This Post Helpful? 0
  • +
  • -

#4 ozzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 11

Re: Insert multiple checkbox and hidden form into database

Posted 09 August 2011 - 04:24 PM

I have googled and search in this forum but so far I only can found a simple code such as:
<input type="checkbox" name="item[]" value="1"><br>
<input type="checkbox" name="item[]" value="2"><br>
<input type="checkbox" name="item[]" value="3">


which I have done in the past. Or I overdo it?
Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Insert multiple checkbox and hidden form into database

Posted 09 August 2011 - 09:50 PM

As codeprada said, you are dealing with an array. You can use a for loop to traverse $_POST['item_name'] and insert those elements.

$count = count($_POST['item_name']);
for($i = 0; $i < $count; $i++){
    //insert $_POST['item_name'][$i] into the database
}


Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Insert multiple checkbox and hidden form into database

Posted 09 August 2011 - 09:55 PM

The only items that will be in the $_POST['item'] list are the ones that have been selected so you don't have to worry about which ones were or weren't selected.

To determine which link was selected place the URL into the value attribute of each checkbox. That way your $_POST['item'] array will look somewhat like this
$_POST['item'][0] = 'http://link.com/one
$_POST['item'][1] = 'http://link.com/two
...

Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Insert multiple checkbox and hidden form into database

Posted 09 August 2011 - 09:57 PM

Forgot to mention that. Thanks for catching that, codeprada!
Was This Post Helpful? 0
  • +
  • -

#8 ozzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 11

Re: Insert multiple checkbox and hidden form into database

Posted 10 August 2011 - 07:29 AM

I'm sorry if I'm a little bit dense, but I see that you refer array with the same name $_POST['item'], because they're not the same, $_POST['item1'], $_POST['item2'], etc.
How do I catch which one is 'checked'?
Thanks.

If I check link one and link three, using print_r($_POST) I get this array:
Array
(
    [item1] => Array
        (
            [0] => 1
            [1] => Link one title
            [2] => http://link.com/one
            [3] => Link one description
            [4] => Link one date
        )

    [item2] => Array
        (
            [0] => Link two title
            [1] => http://link.com/two
            [2] => Link two description
            [3] => Link two date
        )

    [item3] => Array
        (
            [0] => 3
            [1] => Link three title
            [2] => http://link.com/three
            [3] => Link three description
            [4] => Link three date
        )

    [item4] => Array
        (
            [0] => Link four title
            [1] => http://link.com/four
            [2] => Link four description
            [3] => Link four date
        )
)

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Insert multiple checkbox and hidden form into database

Posted 10 August 2011 - 07:30 AM

If you have checkboxes in your HTML form named item[], and you check the ones with values 1 and 3, the $_POST['item'] array will only have the values 1 and 3 in it. You don't have to make the determination as to which ones weren't checked. You are only provided information regarding the ones that are checked.
Was This Post Helpful? 1
  • +
  • -

#10 ozzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 11

Re: Insert multiple checkbox and hidden form into database

Posted 10 August 2011 - 08:26 AM

View Postmacosxnerd101, on 10 August 2011 - 07:30 AM, said:

If you have checkboxes in your HTML form named item[], and you check the ones with values 1 and 3, the $_POST['item'] array will only have the values 1 and 3 in it. You don't have to make the determination as to which ones weren't checked. You are only provided information regarding the ones that are checked.


I'm not talking about values. The HTML form has different names, item1[], item2[] etc.

But now I think I have the solution, first using preg_match (or any functions to check numeric values) I check if the first index of the array has a numeric value, which is the value of the checked form:
foreach ($_POST as $key=>$value)
{
    if (preg_match('/[0-9]+/', $_POST[$key][0]))
    {
        $query = "INSERT INTO table VALUES (NULL, '$_POST[$key][2]', '$_POST[$key][1]')";
        $db->execute($query);
    }
}


Thanks to macosxnerd101 and codeprada for your patience answers.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Insert multiple checkbox and hidden form into database

Posted 10 August 2011 - 08:31 AM

Glad you've found a solution and glad we could help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1