Page 1 of 2
What would be the best way? Array?
Posted: Tue Jun 27, 2006 4:36 pm
by bordman
Hi,
I'm new to PHP and I'm not sure of the best way to do this.
Here's what I need to do. I need a quote form (name,state, phone, email) that will use php to send an email.
The form will start with a question: "Number of people?" with text field for the user to enter a number. I will probably limit it to 10.
So, for example if the user enters 3 people and clicks submit, I would basically like to produce this code:
Code: Select all
<form name="form1" method="post" action="">
<p><hr><strong>Person 1</strong><br>
Name:
<input name="name" type="text" id="name">
<br>
<br>
State:
<input name="address" type="text" id="address">
<br>
<br>
Phone:
<input name="phone" type="text" id="phone">
<br>
<br>
Email:
<input name="email" type="text" id="email">
<hr>
</p>
<strong>Person 2</strong><br>
Name:
<input name="name2" type="text" id="name2">
<br>
<br>
State:
<input name="address2" type="text" id="address2">
<br>
<br>
Phone:
<input name="phone2" type="text" id="phone2">
<br>
<br>
Email:
<input name="email2" type="text" id="email2">
<hr>
</p>
<strong>Person 3</strong><br>
Name:
<input name="name3" type="text" id="name3">
<br>
<br>
State:
<input name="address3" type="text" id="address3">
<br>
<br>
Phone:
<input name="phone3" type="text" id="phone3">
<br>
<br>
Email:
<input name="email3" type="text" id="email3">
<hr>
</p>
<input type="submit" name="Submit" value="Submit">
<p> </p>
</form>
And put all 3 people's info in the email as well.(or how ever many the user enters)
Any ideas on the best way to handle this?
Thanks for any Help!!
Posted: Tue Jun 27, 2006 4:54 pm
by Robert Plank
Array.
Have the HTML look like this
"name" will show up in $_POST as an array and so on.
Posted: Tue Jun 27, 2006 5:26 pm
by RobertGonzalez
To lighten the load of the code later on, you may want to pass the number specified by the user to the form as a hidden field. Then you can use that value instead of counting the resulting post array.
Posted: Tue Jun 27, 2006 5:29 pm
by Robert Plank
Everah wrote:To lighten the load of the code later on, you may want to pass the number specified by the user to the form as a hidden field. Then you can use that value instead of counting the resulting post array.
What's there to "lighten the load" ... just foreach
Posted: Tue Jun 27, 2006 5:56 pm
by RobertGonzalez
EDIT | My last answer was in the wrong thread. I amazed at how stupid I can be sometimes...
What I meant by lightening the load of the code is that since the form will be drawn using a know number passed by the user, it makes sense to pass that number to the form so when the form is posted, it has an idea of how many of each form field was posted. It just made sense to me when it came out of my fingertips. If it is confusing, please disregard.
Posted: Tue Jun 27, 2006 6:07 pm
by Robert Plank
edited
Posted: Tue Jun 27, 2006 6:13 pm
by RobertGonzalez
HAH! You're right. I choked on that one. Sorry. I'll edit mine if you edit yours.
Posted: Tue Jun 27, 2006 6:27 pm
by Robert Plank
Everah wrote:EDIT | My last answer was in the wrong thread. I amazed at how stupid I can be sometimes...
What I meant by lightening the load of the code is that since the form will be drawn using a know number passed by the user, it makes sense to pass that number to the form so when the form is posted, it has an idea of how many of each form field was posted. It just made sense to me when it came out of my fingertips. If it is confusing, please disregard.
I still don't understand the lighten the load on the code metaphor... are you trying to say for is faster than foreach?
Posted: Wed Jun 28, 2006 12:02 am
by RobertGonzalez
No. What I am saying is that instead of writing code to guess the number of form elements sent, you tell the form. It reduces the amount of code need to process the sent data because you are actually giving the script the same fixed number of fields the user told the script to generate, instead of having the script try to determine the number of posted elements. This is a trivial amount of code savings anyway, but if I can find a savings somewhere, I'll at least at it.
Posted: Wed Jun 28, 2006 12:28 am
by technofreak
I have an idea, if not a great one or perhaps a very simple one.
1. Let the user enter the number of people in a form.
2. Use a for loop, print the 'person n' form n number of times, where n is the number fo people obtained from the previous step. You can use the same 'n' to print 'person 1, person 2...etc' as well as form name[1], name[2]... ie the name and also the corresponding email arrays.
Thus you need not print the form manually in your code for number of times, the for loop will take care of that. But remeber to put the form code (ie the form for person1, etc..) as a function and call it within the for loop to print it.
hope you got our ideas right ?!
Posted: Wed Jun 28, 2006 7:38 pm
by Robert Plank
Everah wrote:No. What I am saying is that instead of writing code to guess the number of form elements sent, you tell the form. It reduces the amount of code need to process the sent data because you are actually giving the script the same fixed number of fields the user told the script to generate, instead of having the script try to determine the number of posted elements. This is a trivial amount of code savings anyway, but if I can find a savings somewhere, I'll at least at it.
There is nothing to guess. If you use foreach you don't even have to know the number of elements in the form. Using foreach is more of a savings than using for because foreach uses iterators instead of random-access.
http://www.robertplank.com/foreach-benchmark.php
Source:
http://www.robertplank.com/foreach-benchmark.phps
foreach was 20 times faster than for when I ran that.
Posted: Wed Jun 28, 2006 10:31 pm
by basdog22
Sorry if this is offtopic :
Have you tried Javascript to do this? I think it is much easier and more user friendly.
Posted: Wed Jun 28, 2006 11:42 pm
by RobertGonzalez
Robert Plank wrote:Everah wrote:No. What I am saying is that instead of writing code to guess the number of form elements sent, you tell the form. It reduces the amount of code need to process the sent data because you are actually giving the script the same fixed number of fields the user told the script to generate, instead of having the script try to determine the number of posted elements. This is a trivial amount of code savings anyway, but if I can find a savings somewhere, I'll at least at it.
There is nothing to guess. If you use foreach you don't even have to know the number of elements in the form. Using foreach is more of a savings than using for because foreach uses iterators instead of random-access.
http://www.robertplank.com/foreach-benchmark.php
Source:
http://www.robertplank.com/foreach-benchmark.phps
foreach was 20 times faster than for when I ran that.
OK.
Posted: Wed Jun 28, 2006 11:45 pm
by daedalus__
I didn't read the thread all of the way through, but I saw 'arrays' and 'send the number of indicies' or something.
You should write code that can support a dynamic number of indices just so you can expand your system later. (if you are using a static number of indices)
Thanks Everyone!
Posted: Thu Jun 29, 2006 10:50 am
by bordman
This is what I came up with, which seems to do just fine, except for the email part which I haven't done yet.
Code: Select all
<?
$numowner = $_POST['numowner'];
$counter = 1 ;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form2" method="post" action="">
Select # of Business Owners:
<select name="numowner" id="numowner">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input type="submit" name="Submit2" value="Submit">
</form>
<form name="form1" method="post" action="">
<?
if ($numowner <> "") {
while ( $counter <= $numowner ) { ?>
<p><hr>
<strong>Owner <? echo $counter ; ?></strong><br>
Name:
<input name="name<? echo $counter ; ?>" type="text" id="name<? echo $counter ; ?>">
<br>
<br>
State:
<input name="address<? echo $counter ; ?>" type="text" id="address<? echo $counter ; ?>">
<br>
<br>
Phone:
<input name="phone<? echo $counter ; ?>" type="text" id="phone<? echo $counter ; ?>">
<br>
<br>
Email:
<input name="email<? echo $counter ; ?>" type="text" id="email<? echo $counter ; ?>">
<br>
<br>
<p> </p>
<?
$counter = $counter + 1;
}
}
?>
<?
if ($numowner <> "") {
?>
<hr>
<input type="submit" name="Submit" value="Submit">
<?
}
?>
</form>
</body>
</html>
Thanks for all your help!