LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2012, 10:00 AM   #1
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Rep: Reputation: 1
I need to list the results from a form in php


Hi Guys...
I have written a list of around 150 items in a form.
Only some of the items will have a tick.
I want to email the list of items that are ticked.
So I need to run down the form and then list each item that is ticked, and ONLY the ticked items, and email it.

I have seen this done in Perl, a long time ago, but I need to do it in php.

Can anybody suggest some code that will achieve the desired result.

Thanks,

John C
 
Old 05-31-2012, 10:27 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by countrydj View Post
I have written a list of around 150 items in a form.
Only some of the items will have a tick.
I want to email the list of items that are ticked.
The form is HTML, I suppose, and you're submitting that form to your PHP script which you still have to write?
Then it's easy: When an HTML form is submitted, unchecked checkbox elements aren't included in the request. Browsers automatically drop them before sending the rest. This means your script will only receive the items that were checked.

Does that help you? - If not, where's your lack of understanding?

[X] Doc CPU
 
Old 05-31-2012, 01:34 PM   #3
809areacode
LQ Newbie
 
Registered: Aug 2011
Location: Godthåb, Grønland
Distribution: Debian
Posts: 28

Rep: Reputation: Disabled
Just a rough guess, but how about something like:

PHP Code:
<?php
   
for ($i=0i<(count($_RESULT)); $i++) {
      
$email $email "Some formatting stuff" $_RESULT[$i];
   }
I'm not certain, but off the top of my head, I don't think unchecked fields are even submitted. Sorry for being quick.

Last edited by 809areacode; 05-31-2012 at 01:35 PM. Reason: Formatting
 
Old 06-01-2012, 10:50 AM   #4
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Original Poster
Rep: Reputation: 1
Hi Guys...

Thank you very much for responding to my query.

I don't think that I explained myself very clearly, so here goes again.

I am testing a script that displayes a form with 8 items. The finished script will have around 150 - 200 items.
Each Item has 4 elements to it:
Description; Pack Qty; Price; Quantity.
The items are called from a database, except for the quantity which is user input.

I do 2 things with the input from the form.
1. I send a response page to confirm what the user has input.
2. I send an email to myself to show what the user has input.

When I test the form input, I fill in all 8 quantities.
The resultant response only shows the LAST item in both response form and email sent to me.

What I need is to show ALL the items where a quantity is filled in to show on the response page, and the email that is sent to me.

Here is my response script:
Code:
$template = join('',file('response_template.tpl'));

$total = $trade*$quantity;
$total = number_format($total ,  2  );
	$content = "<table align=center width=600 cellpadding=0 cellspacing=0 border=0>
<tr><td colspan=2>";
	$content  .= "<center><br /><br /><font size=2 ><b>Hi $name.....</b><br /><br /></center>\n";
	$content  .= "<center><font size=3 ><b>Thank you for Order.</b><br /><br /></center>\n";
	$content .= "<font size=2><b>Your order (detailed below) has been sent to Hughes Family Bakers. </b></font><hr />\n";
	$content .= "<font size=2 color=#ff0000><B>Here is what you ordered:-</b></font><br />\n";

	$content .= "<center><table width=100% border=0 align=CENTER cellspacing=0 cellpadding=10><tr><td>";

		$content .= "<table align=center width=99% cellpadding=0 cellspacing=0 border=0>";

		$content .= "<tr height='30'>";
 		$content .= "<td width='100'><font size=2 color='#ff0000'><b>Quantity</b></font> </td>\n";
 		$content .= "<td><font size=2 color='#ff0000'><b>Product</b></font> </td>\n";
		$content .= "<td width='50'><font size=2 color='#ff0000'>Price</td>";
		$content .= "<td width='100'><font size=2 color='#ff0000'>Total</td>";
		$content .= "</td></tr>";

		$content .= "<tr>";
 		$content .= "<td><font size=2><b>$quantity</b></font> </td>\n";
 		$content .= "<td><font size=2><b>$product</b></font> </td>\n";
		$content .= "<td><font size=2><b>$trade</b></font></td>";
		$content .= "<td><font size=2><b>&pound$total</b></font></td>";
		$content .= "</tr></table><br>";
		$content .= "</td></tr></table></center>";
 		$content .= "<font size=2><b>Your order is valuable to us. We will contact you as soon as we can. </b></font><br />";
		$content .= "</td></tr></table></center><br>";

	$template = str_replace("[content]",$content,$template);
	print $template;
Any advice on how to get all the items showing will be appreciated.

Thanks...
 
Old 06-01-2012, 11:46 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Without seeing the code for your input form or how you are reading the post data it is a bit difficult to determine where you you are having problems. Are you using arrays?
 
Old 06-01-2012, 12:32 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Main thing with PHP is that you need to add [] after the variable name if it can take multiple values, so the PHP interpreter generates an array of those values. Otherwise the PHP interpreter will only use the last value specified in the form.

Assume you have this in the HTML code:
Code:
<form method="POST" action="URL-to-PHP-script" accept-encoding="UTF-8">

 List of things available:
 <select name="thing[]" multiple="multiple" size="5">
  <option value="" selected="selected">Select one or more of the following:
  <option value="foo">Foo thing
  <option value="bar">Bar thing
  <option value="baz">Baz thing
 </select>

 <br>

 Options to include:
 <br> <input type="checkbox" name="option[]" value="box1">Box 1
 <br> <input type="checkbox" name="option[]" value="box2">Box 2
 <br> <input type="checkbox" name="option[]" value="box3">Box 3 
 <br> <input type="checkbox" name="option[]" value="boxŃ">Box N

 <br>

 <input type="submit" value="Send">
</form>
In your own code, you can generate nicely formatted lists as strings using e.g.
PHP Code:
$things "";
if (
is_array(@$_POST['thing']))
    foreach (@
$_POST['thing'] as $item) {
        
$item trim(preg_replace('/[\x00-\x20\x7F]+/'" "$item));
        if (
strlen($item) > 0)
            
$things .= "\t" $item "\n";
    }

$options = @implode("\t", @$_POST['option']);
if (
strlen($options) > 0)
    
$options "\t" $options
which both do the same thing, but the first one more carefully: the string will contain a list of selected items, one per line, with a tab before each item. The first one explicitly converts all consecutive whitespace and control characters to a single space, and removes leading and trailing ones.

Both $_POST['thing'] and $_POST['option'] should be arrays, but since errors may occur (and script kiddies may send you totally bogus forms), it's a good idea to be prepared. The @ is used to suppress any errors; it basically means the code is aware that the right side may fail occasionally, and is prepared to handle the result even in those cases. PHP will not issue any errors or warnings in any case for @-marked expressions.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
validated form >> redirect to php form esteeven Programming 1 10-21-2009 01:18 PM
how to show results of a php query from a form on the main form texmansru47 Programming 2 06-27-2008 01:26 PM
PHP: build query from form entry, then display results in the same form tonedeaf1969 Programming 4 06-22-2007 07:55 AM
How do list the results from my search form? Alexander.s Programming 4 05-09-2005 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration