LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 August 26 2010

Kassem
Member

Arrays Inside The $_POST Array

Hey everyone,

I'm currently working on some website which has a huge "Quotation Form" that requires the user to input a lot of information. I finished re-writing the HTML part of the form, but now I need to fix the PHP code. I have two questions:

1. What's the best (shortest yet efficient) way to get the posted values which will then be included inside the body of an email which will be sent to the site owner?
2. There's a bunch of input tags of type "text" that the user is supposed to fill in with number of items. I'd like to have the values of these tags posted to the server inside an array other than the $_POST array or maybe an array inside it. Is that even possible? (I know that's possible for checkboxes but not sure about textfields)

Offline

#2 August 26 2010

arithma
Member

Re: Arrays Inside The $_POST Array

You should just try the same technique that works for checkboxes. If it doesn't work, you should parse the names and collect them into an array..

Offline

#3 August 26 2010

Kassem
Member

Re: Arrays Inside The $_POST Array

arithma wrote:

You should just try the same technique that works for checkboxes. If it doesn't work, you should parse the names and collect them into an array..

Yeh I will give that a shot... I was wondering, is it possible to check for a class on a tag in PHP? Like for example check for the required fields by adding class="required" to the tag, and then reference those tags from PHP...

Offline

#4 August 27 2010

Kassem
Member

Re: Arrays Inside The $_POST Array

Ok here's something I worked out so far:

if(!empty($_POST)) {
	$msg = '';
	foreach($_POST as $key => $value) {
		if(stripos($key, 'contact_') > -1)
		{
			$key = str_replace('contact_', '', $key);
			if(stripos($key, '_')) {
				$key = str_replace('_', ' ', $key);
			}
			$msg .= $key.': '.$value."\n";
		}
	}
	echo $msg;
}

I decided to break down the contact form into blocks, each will start with a prefix (such as 'contact_'), and then I'll parse the posted data accordingly. Maybe I should make that into a function which takes the prefix as a param... But for some reason the line break doesn't work... Why is that?

EDIT: I find a solution for the line break issue... using the nl2br() function solved the problem.

P.S: I would still like to get an answer to my previous question in the post above...

Last edited by Kassem (August 27 2010)

Offline

#5 August 27 2010

arithma
Member

Re: Arrays Inside The $_POST Array

Kassem wrote:
arithma wrote:

You should just try the same technique that works for checkboxes. If it doesn't work, you should parse the names and collect them into an array..

Yeh I will give that a shot... I was wondering, is it possible to check for a class on a tag in PHP? Like for example check for the required fields by adding class="required" to the tag, and then reference those tags from PHP...

The closest thing you can do is have hidden fields referencing other fields and marking their validation.
However, marking validation requirements on the client-side is bad.
You should think of the form you generate and the script that will handle the posted data (ideally at least) as totally separate things. This will help you with posting from web services, ajax and so on.

Where does validation belong to?
Its correct place is in the place where the model is affected (the script handling the post).

Where does error reporting belong to?
Its correct place is in the view that is responsible for showing the next step after the post is made. In HTML, this is usually just a few divs explaining what went wrong. In XML, this is a failure message, an error number and possibly an error message.

I am not sure what else I have missed processing of your questions. Did your script work well? Did the checkbox like trick work? Do you need something else?

Offline

#6 August 27 2010

samer
Admin

Re: Arrays Inside The $_POST Array

Like arithma mentioned, the form validation should be done on the server side, since it can easily be bypassed on the client side. You can always use jQuery or any other framework to have some additional (and instant) validation. Also, it's a good idea to never trust the user and sanitize all his inputs (even though in your case, we're not storing stuff in a database). You will need to use functions like htmlspecialchars() or even go hardcore and use regex to strip out anything but A-Z a-z 0-9.

As for your implementation, I don't get why you're using the contact_ prefix in the first place.

Offline

#7 August 27 2010

rolf
Member

Re: Arrays Inside The $_POST Array

Multi-dimentinal POST in PHP demonstration script (1/2 of the code is formatting and presentation...):

<?php
echo '<h4>Server Side $_POST:</h4>';
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<html>
	<head>
		<style>
			input, h4, pre {
				display:block;	
				padding:3px;
				margin:3px;
			}
			h4 {text-decoration:underline;}
			pre {	border:1px dotted black;}
		</style>
	</head>
	<body>
		<form method="POST">
			<h4>input1:</h4>
			<input type="text" name="input1" value="a">
			
			<h4>sub_array inputs:</h4>
			<input type="text" name="sub_array[one]" value="b">
			<input type="text" name="sub_array[two]" value="c">
			<input type="text" name="sub_array[three]" value="d">
			
			<input type="submit" value="Try it">	
		</form>
	<body>
</html>

All you have to do is, in your input tag, set the name to arrayName[key], example "choices[one]", "choices[two]" and you will get an array in POST name "choices" with all the key that you defined. You can go multiple levels deep with that.

You owe me a beer :)

Last edited by rolf (August 27 2010)

Offline

#8 August 27 2010

rolf
Member

Re: Arrays Inside The $_POST Array

Kassem wrote:
arithma wrote:

You should just try the same technique that works for checkboxes. If it doesn't work, you should parse the names and collect them into an array..

Yeh I will give that a shot... I was wondering, is it possible to check for a class on a tag in PHP? Like for example check for the required fields by adding class="required" to the tag, and then reference those tags from PHP...

Not really, it's not a limitation in PHP, but from the browser, because when you submit a form, the browser only sends the POST data, and not the whole page HTML.
But you could do something like that:

<input type="hidden" name="is_required[inputName]" value="1">

But, really, as someone pointed out, if these fields are really required, then they should already be defined server-side, otherwise someone who is knowledgeable can falsify the POST and bypass that validation. And if the fields are not really required, then you shouldnt be annoying the visitor with them, or you could at best have a nag or warning message in jQuery.

Now I may have jumped to conlcusions. Do you want to parse your outgoing HTML for the required attribute?? That should be possible, using output buffering ( ob_start() and ob_end_flush() ), but the tricky part is parsing that HTML and detecting that attribute, and also if that attribute is generated dynamically then you'll have an additional problem...

Edit: arithma beat me on this one...

Last edited by rolf (August 27 2010)

Offline

#9 August 27 2010

Kassem
Member

Re: Arrays Inside The $_POST Array

@ arithma: Actually, I'm going to perform validation on the client and server sides. I will try to sanitize the inputs but I will not take it to the extreme because I'm not sure it's needed in the first place (since I won't be storing anything inside a database).

arithma wrote:

Did your script work well? Did the checkbox like trick work? Do you need something else?

Yup actually the script worked and the checkbox like trick worked as well. But now I need some way tell the value refers to which text field because the site owner needs to know what items are available (and their numbers which will be input by the user). I'm thinking maybe I should create an array on the PHP side, fill it in with all the item names, and then I will simply reference it with the for loop index, hopefully that's going to work, I'll give it a try. Otherwise, I'll just use the prefix thingy which should work fine as well, hopefully.

samer wrote:

As for your implementation, I don't get why you're using the contact_ prefix in the first place.

Well that's because the form is HUGE! There are many input fields of different types. I really do not want to parse the posted data one field by one, because that would be writing bunches of code lines to do one of the simplest tasks in PHP. Hence I'm trying to find smarter solutions :)

rolf wrote:

All you have to do is, in your input tag, set the name to arrayName[key], example "choices[one]", "choices[two]" and you will get an array in POST name "choices" with all the key that you defined. You can go multiple levels deep with that.

You owe me a beer :)

As I've mentioned above, I already tried it and it worked, but I still owe you a beer anyway (la 3younak :) ) and thanks for the example. Using the keys like you did in your example could be helpful.

rolf wrote:

But, really, as someone pointed out, if these fields are really required, then they should already be defined server-side, otherwise someone who is knowledgeable can falsify the POST and bypass that validation. And if the fields are not really required, then you shouldnt be annoying the visitor with them, or you could at best have a nag or warning message in jQuery.

Yup, that's what I'm going to do. I do not want to complicate things too much. A jQuery warning message would do the trick.

Thanks for all the help guys, appreciated :)

Offline

Board footer