Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2004, 08:34 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default Options values into a string

I am using many <INPUT type="checkbox"> values in an HTML from.
I am getting these all $_POST into an array and imploding it into a string delimited by commas.

User can select only few of checkboxes.
So i am getting pattern like: val1,,,val5,,,val9,,,,,val15,
(say) for 15 checkboxes.
It should be: val1, val5, val9, val15

Some good use of PHP string functions can do so ?
Please HELP..
__________________
`~@#\^%&*/\.<.\/-|+|_!:;..=?>
PHP, SEO | anshul shrivastava | mediasworks.org | FB
 
Old November 4th, 2004, 09:07 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

This isn't too difficult to overcome. Assuming that you have an array of checkboxes that looks similar to the following:

<input type='checkbox' name='field[0]' value='value0' />
<input type='checkbox' name='field[1]' value='value1' />
<input type='checkbox' name='field[2]' value='value2' />

You can access this in PHP by doing the following. You should only have POST entries for the checkboxes that were checked in the form. Those that were not checked will not be set on the server.

if (isset($_POST['field']) && is_array($_POST['field']))
{
    $_POST['field'] = implode(',', $_POST['field']);
    // Now $_POST['field'] is a string and no longer an array.
}

The checkbox fields must be constructed with array syntax for this to work though.

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
 
Old November 6th, 2004, 06:59 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

i've tried this for
<input type='text' name='field_xyz[0]' etc.
it is not working

but if(is_array($_POST['field']) evaluates to true
 
Old November 6th, 2004, 09:42 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

How about seeing the HTML source of the form and the PHP code you're using to access it.

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
 
Old November 7th, 2004, 03:31 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

i m using
<input type="text" name="keyword[0]" size="15">
..
..
<input type="text" name="keyword[11]" size="15">

so there are 12 textfields.
i want to combine these inputs into a single string delimited by commas and dump it into db.

if (isset($_POST['field']) && is_array($_POST['field']))
   $_string = implode(',', $_POST['field']);
did well for checkboxes

i even used:
if ($_POST['keyword']!="" && $_POST['keyword']!=NULL && $_POST['keyword']!=" " && (strlen($_POST['keyword'])!=0) && $_POST['keyword']!=0 && isset($_POST['keyword']) && is_array($_POST['keyword'])) {
    $f_keywords = implode(', ', $_POST['keyword']);
    }
to filter out empty values for <input type="text"
but can get it.

what else can be done?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Place string values into ListBox semilemon C# 2005 2 April 13th, 2007 06:36 PM
getting a count of values from within a string cole SQL Server 2000 6 September 5th, 2006 11:20 AM
How to hide the values in query string Ramakrishna.G General .NET 8 May 8th, 2006 07:29 PM
Return ASCII values for a given string sal C# 1 May 19th, 2005 03:59 PM
store multiple values in a string bamboat_3 General .NET 3 May 23rd, 2004 07:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.