Forum Moderators: coopster

Message Too Old, No Replies

Subscriber form

checkboxes and empty fields...

         

riverstyx

8:14 pm on May 27, 2005 (gmt 0)

10+ Year Member



Hi, everyone -
I'm new to php and could use some direction. I have an email subscription form and use the following to see if the person has already subscribed:

------------------------
// check if the email is already in the dbase
$check = "select id from $table where email = '".$_POST['email']."';";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows!= 0) {
echo "You are already subscribed!";
exit;
} else {

// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['name']."', '".$_POST['email']."', '".$_POST['gender']."', '".$_POST['age']."')")
or die("Could not insert data because ".mysql_error());
--------------------
I would like to check if the email field is blank and return a warning. I tried this:

if ".$_POST['email']." = NULL
echo "Your email address is missing. Please go back and try again.";

But obviously didn't work. Can someone give me a clue?

Secondly, the subscriber form has 25 checkboxes (optional) -- do I need to create 25 individual fields in the database to capture that data? Obviously, that would leave a lot of empty fields since people don't have to check anything on the form. Suggestions?

TIA,

RS

StupidScript

9:27 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Moving right along ... you have good potential for PHP!

if ".$_POST['email']." = NULL

should be something like:

if ($_POST['email']==NULL)

or

if ($_POST['email']=="")

Note the "comparison operator" (

==
) instead of the "assignment operator" (
=
), and the use of parentheses for the
if
conditional statement.

Secondly, the checkboxes, if traditional, all have the same "name" attribute. While this becomes a pseudo array upon submission, you can force it to be a real array by including array notation in your form names:

instead of:

<input type="checkbox" name="chkbx" value="value1">

use:

<input type="checkbox" name="chkbx[]" value="value1">

When coded like that, you end up passing an array to the subsequent page, which can be aggregated into a single, comma-separated (or whatever-separated) entry for insertion in the db:

On the receiving page:

$thesechkboxes = ""; //initialize storage var

if ($_POST['chkbx']) {

foreach($_POST['chkbx'] as $chkvalue) {

$thesechkboxes .= $chkvalue.","; //append to storage string

}

$thesechkboxes = substr($thesechkboxes,0,-1); //strip off tailing comma

Then you can dump

$thesechkboxes
into a single db cell, no matter how long or empty it is.

Enjoy!

riverstyx

9:59 pm on May 27, 2005 (gmt 0)

10+ Year Member



Thanks so much for the speedy reply. Re: the checkboxes, they each have a different name. Therefore, would I just go with this:

<input type="checkbox" name="chkbx[]" value="value1">
<input type="checkbox" name="chkbx[]" value="value2">
etc.
and skip the rest?

RS

StupidScript

10:12 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Typical checkbox useage is similar to radio button useage with one difference: checkboxes form a group from which the user may select many entries and radio buttons form a group from which the user can only select one entry.

<input type="radio" name="rad" value="yes"> Yes

<input type="radio" name="rad" value="no"> No

sends a single entry (

rad=yes
) with only the one selection. As you know, selecting any other radio button in that group deselects any other selection because that's what radio buttons are for.

<input type="checkbox" name="chk[]" value="red"> Red

<input type="checkbox" name="chk[]" value="blu"> Blue

<input type="checkbox" name="chk[]" value="yel"> Yellow

sends as many entries from that group as are selected (

chk[]=red&chk[]=yel
). As you know, selecting any other entry within that group allows any other selected entry to remain selected.

<input type="checkbox" name="[b]red[][/b]" value="red"> Red

<input type="checkbox" name="[b]blu[][/b]" value="blu"> Blue

<input type="checkbox" name="[b]yel[][/b]" value="yel"> Yellow

results in something like

red[]=red&yel[]=yel
, which is less useful in your situation.

So ... (whew) ... use the same name and different values for all of the checkboxes in any group. You (of course) may have many groups of checkboxes, but their purpose is to form those groups. Having checkboxes with different names is good for some things, but if you want to collect all of the select checkbox values into a single db entry, using the same name (

name="chkbx[]"
) forms the group, and makes it easy to grab the stuff on the "action" page, as shown in my previous post.

riverstyx

10:15 pm on May 27, 2005 (gmt 0)

10+ Year Member



Muchas gracias. I'm pretty new to HTML, too, and got in the habit of using unique names because of that radio button issue. Thanks again...

RS

StupidScript

10:18 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a pleasure! ;)
Quick followup with a "real life" example:

Select which magazines you want to receive:

<input type="checkbox" name="mag[]" value="newsweek">Newsweek

<input type="checkbox" name="mag[]" value="time">Time

<input type="checkbox" name="mag[]" value="pcworld">PC World

Select which newsletters you would like to receive:

<input type="checkbox" name="news[]" value="seo">SEO Weekly

<input type="checkbox" name="news[]" value="web">WebmasterWorld

<input type="checkbox" name="news[]" value="ecom">ECommerce Week

<input type="checkbox" name="listme" value=1 checked>Put me on your mailing list

<input type="submit" value="Subscribe">

or

Select which toys you like:

<input type="checkbox" name="user_info[]" value="toy_doll">Dolls

<input type="checkbox" name="user_info[]" value="toy_teddy">Teddy Bears

<input type="checkbox" name="user_info[]" value="toy_trux">Trucks

Select which flavors you like:

<input type="checkbox" name="user_info[]" value="flav_choc">Chocolate

<input type="checkbox" name="user_info[]" value="flav_van">Vanilla

<input type="checkbox" name="user_info[]" value="flav_straw">Strawberry

<input type="radio" name="gender" value="m">I am a male.

<input type="radio" name="gender" value="f">I am a female.

<input type="submit" value="Send Info">