Multiple Choice Form Values

This is a discussion on "Multiple Choice Form Values" within the PHP Forum section. This forum, and the thread "Multiple Choice Form Values are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Aug 19th, 2005, 14:12
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
Multiple Choice Form Values

One of the fileds in my Form has multiple Tick boxes. Each tick box is labeled 'service'

I expected the values to be emailed to me in order,

eg:

service1, service2, service4

But only one was coming through. . . To get around this I labeled each tick box differently and manualy added a ',' commer at the end of each to separate them up:
Code: Select all
$message .= "Service: $Service, ";
$message .= "$Service2, ";
$message .= "$Service3, ";
$message .= "$Service4, ";
etc
etc
But this results in them being emailed with all the unused fields displayed like this:

service1, , service3, , , service6, service7, , , service11,

Any recommendations as to a better way to capture multiple answers in one field please?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Aug 20th, 2005, 20:38
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
How's this?

Code: Select all
<?php
if ($_REQUEST[completed]) {
    $report = "";
    for ($k=1; $k<6; $k++) {
        if ($_REQUEST["service".$k]) {
            $report .= $report ? ", " : "Service: ";
            $report .= "service".$k;
        }
    }
} else {
    $report = "Your results will appear here";
}
?>
<html>
<head><title>Demo to answer Forum question</title></head>
<body>
<h1>Select from a series of radio boxes</h1>
<?= $report ?><hr>
<form><input type=hidden name=completed value=1>
<input name=service1 type=radio> 05:52 to Swindon

<input name=service2 type=radio> 07:45 to Swindon

<input name=service3 type=radio> 13:35 to Swindon

<input name=service4 type=radio> 17:02 to Swindon

<input name=service5 type=radio> 21:33 to Swindon

<input type=submit></form>
<hr>
By Graham Ellis, graham@wellho.net
</body>
</html>
Complete running code

Note - I dislike repeated code in my PHP (such as checking a series of variables with a pattern of names) as it makes the code hard to extend if more repetitions are added later, so I've provided a slightly complex loop that could easily be expanded if the service increased from 5 to 50 a day!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Aug 23rd, 2005, 10:58
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
That is fantastic thank you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Aug 24th, 2005, 20:25
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,
I have been using the code you suggested but from the POST php page after my form is submitted and I have come across a problem.

First off I changed the
Code: Select all
if ($_REQUEST[completed]) {
to
Code: Select all
if ($_POST[myform]) {
But this returned the ELSE statment "No Services selected" every time, so I just deleted it.

Here is the code:

Code: Select all
    $message .= "SERVICE: "; 
    for ($k=1; $k<18; $k++) { 
        if ("$Service".$k) { 
            $message .= $message ? ", " : "Service: "; 
            $message .= "$Service".$k."\n"; 
} else { 
    $message .= "No Services selected\n"; 
	}
}
Instead of adding the Service values it just lists numbers 1 to 17 (shown below) If I alter the $k< value it changes the numbers what is sent.


Quote:
SERVICE: , 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
, 10
, 11
, 12
, 13
, 14
, 15
, 16
, 17
Any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Aug 24th, 2005, 21:22
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Any ideas?
Yes.

You can use a $service.$k type construct as an array subscript which is what I did in my example, but you can't use it to make up a variable name which is what you're attempting.

What version of PHP are you using? If you're using 4.1 or later, you really should be using code like my original suggestion .... but it looks like you may be using 4.0.something or even older if that didn't work. In the older versions ... as I recall .. there's an array called $HTTP_POST_VARS which should be used in place of the $_REQUEST of my original code (and you do need to add a method="POST" to the form tag)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Aug 25th, 2005, 08:51
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
Hi

I am using php 4.3.11, but here I tried $HTTP_POST_VARS anyway

I have amended to code like you suggested (below). The form is named form1 and has POST method already. The fields are named Service1 to Service17

I removed the $ so it is not using a variable name. But I dont understand how the function will know to recieve info from the Service fileds? I understood by writing $Service I was calling the field info?

The result is still 'No Services Selected' every time

Code: Select all
if ($HTTP_POST_VARS[form1]) { 
    $message .= "SERVICE: "; 
    for ($k=1; $k<18; $k++) { 
        if ("service".$k) { 
            $message .= $message ? ", " : "Service: "; 
            $message .= "service".$k;
			} 
		}
} else { 
    $message .= "No Services selected\n"; 
	}

The reason I changed the $report to $message is because it is within an email message and there are other $message .= "info info" above and below it refering to other fileds on the form.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Aug 26th, 2005, 05:40
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
The code

Code: Select all
if ("service".$k) {
is never going to work - it will always give you a true value ... sorry, but as per previous comments you cannot make up a variable name like and should be using it as an array element where you CAN make it up as a subscript.

If you're at 4.3.11, then you should be using $_GET if you're submitting the form via the GET method, $_POST if you're using the POST method, or $_REQUEST if you want the code to work whichever method is used for the submission.

I note that you're using this as part of a bigger script (yes, I would have expected that). If my comments above don't lead you to a solution, can I suggest that you cut out the majority of that script and reduce the problem to a piece of code about the length of my earlier example and post it ... I'll probably be able to spot the problem and give a specific fix quite quickly!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Aug 26th, 2005, 11:58
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
I realised the line you rmentioned was missing the $_POST so I changed it to this:

Code: Select all
       if ($_POST["Service".$k]) {
I am not sure to use "Service".$k or "$Service".$k and I dont understand what you mean by an array subscript?

As far as I can see it is the same as your orginal script only using POST

Code: Select all
if ($_POST[form1]) { 
    $message .= ""; 
    for ($k=1; $k<18; $k++) { 
       if ($_POST["Service".$k]) { 
            $message .= $message ? ", " : "Service: "; 
            $message .= "Service".$k;
			} 
		}
} else { 
    $message .= "No Services selected\n"; 
	}
No Services selected. Here is the full script, it is only a simple form. I dont blame you if you want to give up though

Code: Select all
<?php

$senderemail = $_POST['Email'];
$recipient = "me@address.com";
$subject = "Enquiry Form";
$mailheader = "From: $Email\n";
$mailheader .= "Reply-To: $Email\n";
$mailheader .= "MIME-Version: 1.0\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\n"; 

$message = "Organisation: $Organisation\n";
$message .= "Client_Name: $Client_Name\n";
$message .= "Address: $Address1\n";
$message .= "Email: $Email\n\n";

if ($_POST[form1]) { 
    $message .= ""; 
    for ($k=1; $k<18; $k++) { 
       if ($_POST["Service".$k]) { 
            $message .= $message ? ", " : "Service: "; 
            $message .= "Service".$k;
			} 
		}
} else { 
    $message .= "No Services selected\n"; 
	}

$message .= "Service Other: $Serv_Other\n\n";

mail($recipient, $subject, $message, $mailheader) or die ("Failure");
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Aug 26th, 2005, 17:29
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Your PHP worked for me using the form:

Code: Select all
Please input ...
<form action=wfz.php method=POST>
<input type=hidden name=form1 value=12>
email: <input name=Email>

<input type=checkbox name=Service1> Solid

<input type=checkbox name=Service2> Liquid

<input type=checkbox name=Service3> Gas

<input type=submit>
</form>
I suspect that you haven't got the hidden field in your form ....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Aug 30th, 2005, 12:01
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
I added the Hidden Field and the Services come through fine now Thank you.

Out of interest - If I wanted to add Checked Values to the check boxes on the form, could this script be amended somehow to accomodate them?

AK
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
multiple, choice, form, values

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
My javascript for multiple choice questions (COMPLETED) RohanShenoy Webforumz Cafe 0 Dec 4th, 2007 07:52
JS for multiple choice questions (i worte it myself) RohanShenoy JavaScript Forum 20 Nov 8th, 2007 18:40
Setting Form Values to Previously Entered Values masonbarge PHP Forum 6 Oct 17th, 2006 17:36
form not updating values djanim8 Classic ASP 0 Dec 10th, 2005 19:05
Passing values from Datagrid to another form caryma ASP.NET Forum 4 Aug 17th, 2005 08:06


All times are GMT. The time now is 07:14.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42