Solved

Online Form Not Displaying All Checkbox Options

Posted on 2010-09-21
20
286 Views
Last Modified: 2012-05-10
Why does this online form only displaying ONE state in the resulting email after you click on MULTIPLE checkboxes in the state listing.  The problem is related to the row with this statement:

 "We have reps stationed across the country.  In what state, or states, would you like to expand your business?"

Here is the link:

http://woodreps.com/contact.html
http://woodreps.com/woodreps.css
 
0
Comment
Question by:nsitedesigns
  • 11
  • 5
  • 3
  • +1
20 Comments
 
LVL 16

Assisted Solution

by:Chris Harte
Chris Harte earned 40 total points
Comment Utility
Because they all have the same name and id. When you return the value from the page it will pick up NeedRepsHere and return the name and id.

Each <input> needs an individual name and or id
0
 

Expert Comment

by:anjana2100
Comment Utility
This can be happen when you didn't give identical names to your check boxe "names", or giving identical names for "value". For instance, look at the HTML code below, and this is in correct format.

<form>
<input type="checkbox" name="rep" value="Nationwide" />Nationwide<br />
<input type="checkbox" name="rep" value="Alabama" /> Alabama
</form>

Or there may be an error in your processing php file.
Can you attach your php (only the logic) file for further examine.

Regards,
Anjana Silva
0
 

Author Comment

by:nsitedesigns
Comment Utility
So, for sake of keeping it simple, could I just put the state name in the "input name" and "id" sections? How do I identify what it is they are answering so that client knows this is the state that they need reps in and not the state that they live in?  Also, it seems strange that when I filled out the form, I selected 3 states and the email form listed my selection as California. Why did it show California if all I had listed was NeedRepsHere?  I am confused.  It should not have picked up on that state name at all.

old:
<input name="NeedRepsHere" type="checkbox" id="NeedRepsHere" value="California" />California<br />

new:
<input name="California" type="checkbox" id="California" value="California" />California<br />

Thank you
0
 

Author Comment

by:nsitedesigns
Comment Utility
Anjana2100,

Does it matter that my coding is not in the same order as yours?   Would changing it like I have it below under "new" work?

old: <input name="NeedRepsHere" type="checkbox" id="NeedRepsHere" value="California" />California<br />

new:  <input name="Rep" type="checkbox" id="NeedRepsHere" value="California" />California<br />
0
 

Author Comment

by:nsitedesigns
Comment Utility
Would I need to change the id value as well?  They will all say NeedRepsHere
0
 

Assisted Solution

by:anjana2100
anjana2100 earned 115 total points
Comment Utility
Dear nsitedesigns,

Try giving a short name for both id & name. For instance,

<input type="checkbox" name="rep" id="rep" value="Nationwide" />Nationwide<br />

(Actually ID is mostly needed to validate your code from a JavaScript. The "name" is the important part which takes values to your php form. )

After changing your "name" values, you need to slightly change the php file also. For instance,

$var1 = $_POST["rep"];// must be added

If this doesn't work, then there must be a logical error in your php file. You are not correctly taking whatever the value.

Regards,
Anjana
0
 

Expert Comment

by:anjana2100
Comment Utility
For your clarification,

Just think "name" attribute in your check box as the group name. Since you have lot of check boxes in your form, to capture data from a huge (more than 1) set of check boxes, you need to have identical "name" attributes. (This is the easiest method).

The "value" attribute takes the exact value of the check-box. This is different from one check box to another. For instance,

<input type="checkbox" name="rep" value="Nationwide" />
<input type="checkbox" name="rep" value="Alabama" />

Hope you got the point.
Regards,
Anjana
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 345 total points
Comment Utility
nsitedesigns,

When dealing with checkboxes like this it easier to use an array for the name so that you can grab all the values for boxes that were ticked in a foreach loop. Also, don't use the same ID for each checkbox. An ID should only be used once in any document! You can probably get away with dropping the ID altogether (unless you need of for JS purposes)

Also, in your code you have a value of 'checkbox' for the District of Columbia :)







//HTML

<input name="reps[]" type="checkbox" value="nationwide" />Nationwide<br />

<input name="reps[]" type="checkbox" value="Alabama" />Alabama<br />

<input name="reps[]" type="checkbox" value="Arizona" />Arizona<br />

<input name="reps[]" type="checkbox" value="California" />California<br />

<input name="reps[]" type="checkbox" value="Colorado" />Colorado<br />

<input name="reps[]" type="checkbox" value="Connecticut" />Connecticut<br />

<input name="reps[]" type="checkbox" value="Delaware" />Delaware<br />

<input name="reps[]" type="checkbox" value="District of Columbia" />District of Columbia<br />





//PHP

foreach ($_POST['reps'] as $selectedRep) {

	$selectedReps .= $selectedRep . " | ";

}



echo $selectedReps;

Open in new window

0
 

Author Comment

by:nsitedesigns
Comment Utility
ChrisStanyon,

Where do I put the PHP reference you provided?  Does it go in the html or on the php page?  I don't know php so this is all foreign to me.  The php was provided by host and normally I don't have to touch it.


//PHP
foreach ($_POST['reps'] as $selectedRep) {
      $selectedReps .= $selectedRep . " | ";
}

echo $selectedReps;

<?php



if ( !isset($_SERVER['SPI'])) {

        die();

}



if (!isset($_SERVER['DOCUMENT_ROOT'])) {

	echo("CRITICAL: we seem to be running outside of the norm.\n");

	header("Location: http://".$_SERVER["HTTP_HOST"]."/");

	die("CRITICAL: Document root unavailable.\n");

}



$request_method = $_SERVER["REQUEST_METHOD"];

if($request_method == "GET") {

	$query_vars = $_GET;

}

elseif ($request_method == "POST") {

	$query_vars = $_POST;

}



reset($query_vars);

function customsort($a,$b) {

	// $a is array for form vars, $b is comma seperated case sensitive field order

	// this is case sensitive -- good idea to hrc that.

        $data = array();

        if ( strstr($b,',') == FALSE )  {

                $b = $b.",";

        }

        $ordering = split(',',$b);

        foreach ($ordering as $orderitem) {

                if ( ($orderitem != null) && ($orderitem != "") ) {

                        if (isset($a[$orderitem])) {

                                $data[$orderitem] = $a[$orderitem];

                        }

                }

        }

        foreach ($a as $key=>$val) {

                $data[$key] = $a[$key];

        }

        return $data;

}



function xmlentities($string) {

	return str_replace ( array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $string);

}



$t = date("U");



$formhomedir = preg_replace('/.*\/home\/content/','',$_SERVER['DOCUMENT_ROOT']);

$formhomedir = explode('/',$formhomedir);

if (count($formhomedir) <= 4) {

        $formhome="/home/content/".$formhomedir[1]."/".$formhomedir[2]."/data/";

}

else {

        $formhome="/home/content/".$formhomedir[1]."/".$formhomedir[2]."/".$formhomedir[3]."/".$formhomedir[4]."/data/";

}



$file_order = ".default";

$file_format = ".text";

$file_interval = ".15m";

$field_order = "";



if (isset($query_vars['form_order'])) {

	if ($query_vars['form_order'] != "alpha") {

		$field_order=$query_vars['form_order'];

		$file_order=".custom";

		$query_vars = customsort($query_vars,$field_order);

	}

	else {

		switch ($query_vars['form_order']) {

			case "alpha":

				uksort($query_vars,'strnatcasecmp');

				$file_order=".alpha";

			break;

			default:

				$file_order=".default";

			break;

		}

	}

}



if (isset($query_vars['form_format'])) {

	switch ($query_vars['form_format']) {

		case "csv":

			$file_format = ".csv";

		break;

		case "html":

			$file_format = ".html";

		break;

		case "xml":

			$file_format = ".xml";

		break;

		case "text":

		case "default":

		default:

			$file_format = ".text";

		break;

	}

}



if (isset($query_vars['form_delivery'])) {

	switch ($query_vars['form_delivery']) {

		case "hourly":

			$file_interval = ".60m";

		break;

		case "hourly_digest":

			$file_interval = ".60mc";

		break;

		case "daily":

			$file_interval = ".24h";

		break;

		case "daily_digest":

			$file_interval = ".24hc";

		break;

		case "digest":

			$file_interval = ".15mc";

		break;

		case "default":

		default:

			$file_interval = ".15m";

		break;

	}

}



$file = $formhome."form_".$t.$file_order.$file_format.$file_interval;

$fp = fopen($file,"w");



reset($query_vars);

switch ($file_format) {

	case ".csv":

		$csvkeys = "";

		$csvvals= "";

		$firsttime = "";

		while (list ($key, $val) = each ($query_vars)) {

			if ( ($key == "form_order") ||

				($key == "form_format") ||

				($key == "form_delivery") ||

				($key == "redirect") ) {

			}

			else {

				if ($csvkeys != "") {

					$firsttime=",";

				}

				$tmpkey=escapeshellcmd($key);

				$csvkeys = $csvkeys.$firsttime."'".$tmpkey."'";

				$tmpval=escapeshellcmd($val);

				$csvvals = $csvvals.$firsttime."'".$tmpval."'";

			}

		}

		fputs($fp,"$csvkeys\n");

		fputs($fp,"$csvvals\n");

	break;

	case ".html":

		fputs($fp,"<table border=\"1\" cellspacing=\"1\" cellpadding=\"2\">\n");

	break;

	case ".xml":

		fputs($fp,"<form>\n");

	break;

}

		

reset($query_vars);

while (list ($key, $val) = each ($query_vars)) {

	if ($key == "redirect") {

		$landing_page = $val;

	}

	if ( ($key == "form_order") ||

		($key == "form_format") ||

		($key == "form_delivery") ||

		($key == "redirect") ) {



	}

	else {

		switch ($file_format) {

			case ".html":

				fputs($fp,"\t<tr>\n");

				fputs($fp,"\t\t<td><b>$key</b></td>\n");

				fputs($fp,"\t\t<td>$val</td>\n");

				fputs($fp,"\t</tr>\n");

				

			break;

			case ".csv":

				// content is already output

			break;

			case ".xml":

				fputs($fp,"\t<field>\n");

				fputs($fp,"\t\t<fieldname>".xmlentities($key)."</fieldname>\n");

				fputs($fp,"\t\t<fieldvalue>".xmlentities($val)."</fieldvalue>\n");

				fputs($fp,"\t</field>\n");

			break;

			case ".text":

			default:

				fputs($fp,$key.": ".$val."\n");

			break;

		}

	}

}



switch ($file_format) {

	case ".html":

		fputs($fp,"</table>\n");

	break;

	case ".xml":

		fputs($fp,"</form>\n");

	break;

}





fclose($fp);



if ($landing_page != "") {

	header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");

}

else {

	header("Location: http://".$_SERVER["HTTP_HOST"]."/");

}





?>

Open in new window

0
 

Author Comment

by:nsitedesigns
Comment Utility
It was suggested that the following code be added to the php page.  I don't know where to put this.  Can someone please advise.

//PHP
foreach ($_POST['reps'] as $selectedRep) {
      $selectedReps .= $selectedRep . " | ";
}

echo $selectedReps;
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 345 total points
Comment Utility
hi nsitedesigns.

Sorry, been away for a long weekend.

Replace lines 174-178 (in your code above) with the following code.

Bear in mind this will only work if you continue to output your form contents as HTML. If you want to output it as .csv, .xml, or .text, then you will need to adapt the code and add in extra lines in the other 'case' sections.





case ".html":

	if ($key=='reps') {

		$val="";

		foreach ($_POST['reps'] as $selectedRep) {

			$val .= $selectedRep . " | ";

		}

	}



	fputs($fp,"\t<tr>\n");

	fputs($fp,"\t\t<td><b>$key</b></td>\n");

	fputs($fp,"\t\t<td>$val</td>\n");

	fputs($fp,"\t</tr>\n");

Open in new window

0
 

Author Comment

by:nsitedesigns
Comment Utility
I put code into new php page and changed link on html.  Did not work.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
nsitedesigns.

Can you be a little more specific. What didn't work? Did you get any errors? What details are being sent in the email? Does the email include the field information for reps?

Also, not sure what you mean by 'changed link on html'

It may help if you post the full PHP code, and provide a link to the HTML form so I can have a look.




0
 

Author Comment

by:nsitedesigns
Comment Utility
I took the code you provided and added it to the php as was directed.  I had to rename the php file because host would not let me change the original php file that they provided.  I changed the file name on the html to point to new php file.  I uploaded php and html pages and tested form.  You still cannot get more than one state to display.  I tried selecting the top 3 states on the contact form (We have reps stationed across the country. In what state, or states, would you like to expand your business?) and only got the last state to display in email (see screenshot below)

http://woodreps.com/contact.html
webformmailer2.php (new one)
http://screencast.com/t/YWYwOThhND
<?php



if ( !isset($_SERVER['SPI'])) {

        die();

}



if (!isset($_SERVER['DOCUMENT_ROOT'])) {

	echo("CRITICAL: we seem to be running outside of the norm.\n");

	header("Location: http://".$_SERVER["HTTP_HOST"]."/");

	die("CRITICAL: Document root unavailable.\n");

}



$request_method = $_SERVER["REQUEST_METHOD"];

if($request_method == "GET") {

	$query_vars = $_GET;

}

elseif ($request_method == "POST") {

	$query_vars = $_POST;

}



reset($query_vars);

function customsort($a,$b) {

	// $a is array for form vars, $b is comma seperated case sensitive field order

	// this is case sensitive -- good idea to hrc that.

        $data = array();

        if ( strstr($b,',') == FALSE )  {

                $b = $b.",";

        }

        $ordering = split(',',$b);

        foreach ($ordering as $orderitem) {

                if ( ($orderitem != null) && ($orderitem != "") ) {

                        if (isset($a[$orderitem])) {

                                $data[$orderitem] = $a[$orderitem];

                        }

                }

        }

        foreach ($a as $key=>$val) {

                $data[$key] = $a[$key];

        }

        return $data;

}



function xmlentities($string) {

	return str_replace ( array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $string);

}



$t = date("U");



$formhomedir = preg_replace('/.*\/home\/content/','',$_SERVER['DOCUMENT_ROOT']);

$formhomedir = explode('/',$formhomedir);

if (count($formhomedir) <= 4) {

        $formhome="/home/content/".$formhomedir[1]."/".$formhomedir[2]."/data/";

}

else {

        $formhome="/home/content/".$formhomedir[1]."/".$formhomedir[2]."/".$formhomedir[3]."/".$formhomedir[4]."/data/";

}



$file_order = ".default";

$file_format = ".text";

$file_interval = ".15m";

$field_order = "";



if (isset($query_vars['form_order'])) {

	if ($query_vars['form_order'] != "alpha") {

		$field_order=$query_vars['form_order'];

		$file_order=".custom";

		$query_vars = customsort($query_vars,$field_order);

	}

	else {

		switch ($query_vars['form_order']) {

			case "alpha":

				uksort($query_vars,'strnatcasecmp');

				$file_order=".alpha";

			break;

			default:

				$file_order=".default";

			break;

		}

	}

}



if (isset($query_vars['form_format'])) {

	switch ($query_vars['form_format']) {

		case "csv":

			$file_format = ".csv";

		break;

		case "html":

			$file_format = ".html";

		break;

		case "xml":

			$file_format = ".xml";

		break;

		case "text":

		case "default":

		default:

			$file_format = ".text";

		break;

	}

}



if (isset($query_vars['form_delivery'])) {

	switch ($query_vars['form_delivery']) {

		case "hourly":

			$file_interval = ".60m";

		break;

		case "hourly_digest":

			$file_interval = ".60mc";

		break;

		case "daily":

			$file_interval = ".24h";

		break;

		case "daily_digest":

			$file_interval = ".24hc";

		break;

		case "digest":

			$file_interval = ".15mc";

		break;

		case "default":

		default:

			$file_interval = ".15m";

		break;

	}

}



$file = $formhome."form_".$t.$file_order.$file_format.$file_interval;

$fp = fopen($file,"w");



reset($query_vars);

switch ($file_format) {

	case ".csv":

		$csvkeys = "";

		$csvvals= "";

		$firsttime = "";

		while (list ($key, $val) = each ($query_vars)) {

			if ( ($key == "form_order") ||

				($key == "form_format") ||

				($key == "form_delivery") ||

				($key == "redirect") ) {

			}

			else {

				if ($csvkeys != "") {

					$firsttime=",";

				}

				$tmpkey=escapeshellcmd($key);

				$csvkeys = $csvkeys.$firsttime."'".$tmpkey."'";

				$tmpval=escapeshellcmd($val);

				$csvvals = $csvvals.$firsttime."'".$tmpval."'";

			}

		}

		fputs($fp,"$csvkeys\n");

		fputs($fp,"$csvvals\n");

	break;

	case ".html":

		fputs($fp,"<table border=\"1\" cellspacing=\"1\" cellpadding=\"2\">\n");

	break;

	case ".xml":

		fputs($fp,"<form>\n");

	break;

}

		

reset($query_vars);

while (list ($key, $val) = each ($query_vars)) {

	if ($key == "redirect") {

		$landing_page = $val;

	}

	if ( ($key == "form_order") ||

		($key == "form_format") ||

		($key == "form_delivery") ||

		($key == "redirect") ) {



	}

	else {

		switch ($file_format) {

	case ".html":

	if ($key=='reps') {

		$val="";

		foreach ($_POST['reps'] as $selectedRep) {

			$val .= $selectedRep . " | ";

		}

	}



	fputs($fp,"\t<tr>\n");

	fputs($fp,"\t\t<td><b>$key</b></td>\n");

	fputs($fp,"\t\t<td>$val</td>\n");

	fputs($fp,"\t</tr>\n");

				

			break;

			case ".csv":

				// content is already output

			break;

			case ".xml":

				fputs($fp,"\t<field>\n");

				fputs($fp,"\t\t<fieldname>".xmlentities($key)."</fieldname>\n");

				fputs($fp,"\t\t<fieldvalue>".xmlentities($val)."</fieldvalue>\n");

				fputs($fp,"\t</field>\n");

			break;

			case ".text":

			default:

				fputs($fp,$key.": ".$val."\n");

			break;

		}

	}

}



switch ($file_format) {

	case ".html":

		fputs($fp,"</table>\n");

	break;

	case ".xml":

		fputs($fp,"</form>\n");

	break;

}





fclose($fp);



if ($landing_page != "") {

	header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");

}

else {

	header("Location: http://".$_SERVER["HTTP_HOST"]."/");

}





?>

Open in new window

0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 345 total points
Comment Utility
OK,

In the PHP file we have added code to check for a form field called reps and when it finds it, loop through it and build a string containing all the selected reps.

That code isn't being fired because your form doesn't contain any field called reps. In your HTML page they are still called NeedRepsHere.

Have a look at my comment above (33726909) and you'll see how you need to set up your HTML form. Instead of all your checkboxes having a name of NeedRepsHere, you will need to rename them all to reps[] in order for the code I provided to work.




0
 

Author Comment

by:nsitedesigns
Comment Utility
reps[]  (with brackets?)
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Yeah - square brackets. That way, all the selected checkboxes will be sent through to the PHP script as an array, allowing you to loop through them, and build the string you need.




0
 

Author Comment

by:nsitedesigns
Comment Utility
Sorry, I wasn't sure what those brackets meant.  I made the change to the form and ran a test.  Waiting for resulting email.  It can take 1-2 hours so I will keep you posted.  Thanks
0
 

Author Comment

by:nsitedesigns
Comment Utility
WAHOO!  It worked!
0
 

Author Closing Comment

by:nsitedesigns
Comment Utility
thanks all!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Read about why website design really matters in today's demanding market.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now