Long Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mur
Forum Newbie
Posts: 21
Joined: Wed Aug 24, 2005 4:01 pm

Long Form

Post by mur »

Hi, I understand the mail() command, but I am making a series of long email forms, and I was wondering if it was possible to write a while loop to automate the proccess of filling the $message varaible. The way I would hope to do it is by telling it to add the info in form object 1 then 2 and so on.

Can anyone help me?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not entirely sure what your question really is.. yes, you can use a loop to make multiple emails... but I'm not so sure of what you are actually after..
mur
Forum Newbie
Posts: 21
Joined: Wed Aug 24, 2005 4:01 pm

Post by mur »

what i mean is...

i have a very long html form with many text fields

is there anyway to automate the input of theses fields into the $message string so i do not have to make a bunch of different variables to combine into one

instead i am hoping to make a while loop that will basically tell it to take the first text field and add it to $message then the second then the third ect
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

One way would be to create an array of all the names of the text fields and then do a foreach loop over that array. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

http://ca3.php.net/implode

$message = implode('-',$_POST);
mur
Forum Newbie
Posts: 21
Joined: Wed Aug 24, 2005 4:01 pm

Progress

Post by mur »

I like the idea of using the array, I don't know why I didn't think of it before. But I'm still having problems.

I have an html doc with the text fields first, last and mi, and I am trying to get this script to run it so it will put all the input into one varaible ($message) although the implode method would work here, it is the comcept of cycling the names through to get their input that I am trying to get to work here.

Code: Select all

<script language="php">
$arr = array("first","last","mi");
$message = "";
foreach ($arr as $name) {
	$message = $message+$_POST[$name];
}
echo $message;


</script>
When I run it, I get "0"

Why?
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

First you should change the <script> tags to the proper php tags ( <?php . . . ?>).

Second, in PHP the concatenation operator is "." not "+". So the code should look like this:

Code: Select all

<?php
$arr = array("first","last","mi");
$message = "";
foreach ($arr as $name) {
    $message = $message . $_POST[$name]; 
    // alternatively:  $message .= $_POST[$name];
}
echo $message;


?>
mur
Forum Newbie
Posts: 21
Joined: Wed Aug 24, 2005 4:01 pm

Post by mur »

Well, that explains the numeric value assigned to the varaible, but the script still does not work i think that there is a problem with the line:

Code: Select all

$message = $message . $_POST[$name];
can you use $name to call the name of a text field using the post method?
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

Code: Select all

<html>
	<head>
		<title>Test</title>
	</head>
	<body>
		<form action="test.php" method="POST">
			<input type="text" name="first" value="John">
			<input type="text" name="mi" value="Q">
			<input type="text" name="last" value="Doe">
			<input type="submit">
		</form>
	</body>
</html>

<?php
	$arr = array("first","last","mi");
	$message = "";
	foreach ($arr as $name) {
		$message = $message . $_POST[$name];
	}
	echo $message;
?>
That code works and outputs "JohnDoeQ".

There isn't any problem using the $name variable to index the $_POST array. :)
mur
Forum Newbie
Posts: 21
Joined: Wed Aug 24, 2005 4:01 pm

Ahhh.

Post by mur »

Got it! Thanks guys. I just left out the method="POST" line. So now that I've got it, is this the best way to be doing this? Also, is there a way to have the PHP code automatically "import" the names of all of the form objects into an array so I could in theory use this on as many forms as I needed to?

Thanks alot!
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

All the form data is stored in the $_POST or $_GET array. If you really want to do something to all the elements then you can do something like:

Code: Select all

foreach($_POST as $name=>$value) {
   . . .
}
implode as suggested before would work in some situation, depending on what exactly what you want to do. :)
mur
Forum Newbie
Posts: 21
Joined: Wed Aug 24, 2005 4:01 pm

Post by mur »

That is almost the perfect command. My script reads

Code: Select all

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form action="a.php" name="form1" id="form1" method = "POST">
  <input type="text" name="first" value="A">
  <input type="text" name="mi" value="B">
  <input type="text" name="last" value="C">
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


<?php
$message = "";
foreach ($_POST as $name) {
	$message = $message . $name;
}
echo $message;


?>
my output reads "ABCSubmit"

what I am hoping to get it to read is

firstamiblastc
and eventually:
First: A MI: B Last: C

is there any way to get the name of the text field from the $_POST, and I'm sure I'll be able to take out the Submit at the end.
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

Code: Select all

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form action="a.php" name="form1" id="form1" method = "POST">
  <input type="text" name="First" value="A">
  <input type="text" name="MI" value="B">
  <input type="text" name="Last" value="C">
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


<?php
$message = "";
foreach ($_POST as $name=>$value) {
    if( $name != 'Submit') {
	   $message .= "$name: $value ";
    }
}
echo $message;


?>

That should give you the exact output you want. :)
mur
Forum Newbie
Posts: 21
Joined: Wed Aug 24, 2005 4:01 pm

Post by mur »

Perfect! Thank you so much. Now only if I could understand it. How does the => operator pull another value from $_POST and how many other things are stored in this array?
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

The => operator used this way in a foreach statement allows you to store both the index and the value.

In a standard array the array is indexed starting from position 0 up to the last position (n-1). If you used the => operator in the foreach statement for this kind of array the variable you put before the => would hold the index number (it would be 0 the first time, 1 the next time, and so on). The variable you put after the => holds the value of the array at that index (so it would be equal to $array[$index], that is $array[0] the first time, $array[1] the next time, etc).

$_POST is what is called an associative array. This means instead of having an integer (number) index, the index is a string. In the $_POST array the indexes are the names of the form element and the value stored is the value input in the form element. Using the => operator just lets you store the index string (in this case the name of the form element) in addition to its value (in this case what was input by the form element). That is all that is stored in the $_POST array. :)
Post Reply