PHP Form echo set of numbers based on input
Moderator: General Moderators
-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
PHP Form echo set of numbers based on input
I have a form that outputs two different numbers "quotes" based on the person's age and sex. I am able to program it so that it pulls the right quote for the age, but how do I specify between age and sex? For example: if a person is between the age of 18-24 and is female her "quote" is 44.65, and if a person is between the age of 18-24 and is male his "quote" is 31.44.
Part of form that users input:
<select name="sex" id="sex">
<option value="female">female</option>
<option value="male">male</option>
My PHP Code
<?php
//page display after form submission
echo "<p><CENTER>Thank you, <b>$_POST[name]</b>, for requesting a quote.<br>
Your are eligable for heatlh insurance rates as low as:</p>";
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25))
$quote=31.44;
elseif (($_POST['age'] >= 26) && ($_POST['age'] <= 30))
$quote=35.91;
elseif (($_POST['age'] >= 31) && ($_POST['age'] <= 35))
$quote=40.78;
elseif (($_POST['age'] >= 36) && ($_POST['age'] <= 40))
$quote=51.33;
elseif (($_POST['age'] >= 41) && ($_POST['age'] <= 45))
$quote=65.95;
elseif (($_POST['age'] >= 46) && ($_POST['age'] <= 50))
$quote=87.87;
elseif (($_POST['age'] >= 51) && ($_POST['age'] <= 60))
$quote=117.50;
elseif (($_POST['age'] >= 61) && ($_POST['age'] <= 64))
$quote=184.07;
//add children
$quote+=($_POST['children']*30.04);
echo "$".$quote."/month</p>
<p>CALL NOW: 1-877-278-3771<br>
TO START YOUR HEALTH INSURANCE COVERAGE!</p>";
//start building the mail string
$msg = "Name: $_POST[name]\n";
$msg .= "Age: $_POST[age]\n";
$msg .= "Sex: $_POST[sex]\n";
$msg .= "Children: $_POST[children]\n";
$msg .= "E-Mail: $_POST[email]\n";
$msg .= "Message: $_POST[message]\n";
//set up the mail to company
$recipient = "info@company.com";
$subject = "Health Insurance Quote";
$mailheaders = "From: 2 Step Health <info@company.com> \n";
//send the mail to company
mail($recipient, $subject, $msg, $mailheaders);
?>
Part of form that users input:
<select name="sex" id="sex">
<option value="female">female</option>
<option value="male">male</option>
My PHP Code
<?php
//page display after form submission
echo "<p><CENTER>Thank you, <b>$_POST[name]</b>, for requesting a quote.<br>
Your are eligable for heatlh insurance rates as low as:</p>";
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25))
$quote=31.44;
elseif (($_POST['age'] >= 26) && ($_POST['age'] <= 30))
$quote=35.91;
elseif (($_POST['age'] >= 31) && ($_POST['age'] <= 35))
$quote=40.78;
elseif (($_POST['age'] >= 36) && ($_POST['age'] <= 40))
$quote=51.33;
elseif (($_POST['age'] >= 41) && ($_POST['age'] <= 45))
$quote=65.95;
elseif (($_POST['age'] >= 46) && ($_POST['age'] <= 50))
$quote=87.87;
elseif (($_POST['age'] >= 51) && ($_POST['age'] <= 60))
$quote=117.50;
elseif (($_POST['age'] >= 61) && ($_POST['age'] <= 64))
$quote=184.07;
//add children
$quote+=($_POST['children']*30.04);
echo "$".$quote."/month</p>
<p>CALL NOW: 1-877-278-3771<br>
TO START YOUR HEALTH INSURANCE COVERAGE!</p>";
//start building the mail string
$msg = "Name: $_POST[name]\n";
$msg .= "Age: $_POST[age]\n";
$msg .= "Sex: $_POST[sex]\n";
$msg .= "Children: $_POST[children]\n";
$msg .= "E-Mail: $_POST[email]\n";
$msg .= "Message: $_POST[message]\n";
//set up the mail to company
$recipient = "info@company.com";
$subject = "Health Insurance Quote";
$mailheaders = "From: 2 Step Health <info@company.com> \n";
//send the mail to company
mail($recipient, $subject, $msg, $mailheaders);
?>
Re: PHP Form echo set of numbers based on input
What is your question?
-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
Re: PHP Form echo set of numbers based on input
Sorry if I didn't clarify...how can I amend the php echo statement to give me two different numbers based on the person's sex (from a drop down menu)? For example: if a person is between the age of 18-24 and is female her "quote" is 44.65, and if a person is between the age of 18-24 and is male his "quote" is 31.44.
I tried this but it didn't work:
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25) && ($_POST['sex'] ="male"))
$quote=31.44;
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25) && ($_POST['sex'] ="female"))
$quote=44.65;
...
Thanks!
I tried this but it didn't work:
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25) && ($_POST['sex'] ="male"))
$quote=31.44;
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25) && ($_POST['sex'] ="female"))
$quote=44.65;
...
Thanks!
Re: PHP Form echo set of numbers based on input
That should work. If it doesn't, make sure you are getting a value for $_POST['sex'].ringartdesign wrote:Sorry if I didn't clarify...how can I amend the php echo statement to give me two different numbers based on the person's sex (from a drop down menu)? For example: if a person is between the age of 18-24 and is female her "quote" is 44.65, and if a person is between the age of 18-24 and is male his "quote" is 31.44.
I tried this but it didn't work:
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25) && ($_POST['sex'] ="male"))
$quote=31.44;
if (($_POST['age'] >= 18) && ($_POST['age'] <= 25) && ($_POST['sex'] ="female"))
$quote=44.65;
...
Thanks!
I would suggest a different way to code your algorithm, though. Something like this:
Code: Select all
...
if(isset($_POST['age']) $age=$_POST['age'];
if(isset($_POST['sex']) $sex=$_POST['age'];
$lookup = array(
0 => array("age" => 18, "male" => 31.44, "female" => 35.91),
array("age" => 26, "male" => 40.78, "female" => 45.22),
array("age" => 31, "male" => 51.33, "female" => 57.90),
array("age" => 36, "male" => 65.95, "female" => 70.87)
);
$i = 0;
while($age >= $lookup[$i]["age"] && $i < 4) {
echo "\$i=$i, ".$lookup[$i]['male']."<br />"; // debug
$quote = $lookup[$i]["$sex"];
$i++;
}
echo "<br />For $age, $sex, the quote is $quote.";
}-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
Re: PHP Form echo set of numbers based on input
hey thanks...I tried your new algorithm and it gave me an error code when testing it live.
about the original way I had it set up...forgive me for being such a novice (i'm self taught in php) how might i write the code to get a value for $_POST['sex']? The values I've given it were while setting up my form submission file was "male" and "female" but maybe i need to reflect that somewhere in my php code?
any help would be appreciated!
about the original way I had it set up...forgive me for being such a novice (i'm self taught in php) how might i write the code to get a value for $_POST['sex']? The values I've given it were while setting up my form submission file was "male" and "female" but maybe i need to reflect that somewhere in my php code?
any help would be appreciated!
Re: PHP Form echo set of numbers based on input
I'm afraid I can't help you much without knowing what the error was.ringartdesign wrote:hey thanks...I tried your new algorithm and it gave me an error code when testing it live.
You can't 'give' values to a $_POST variable. That's the built-in superglobal variable that contains the results from an HTML form that uses method="post". If you're not running your script as it will be run with data coming from a form, it will have no value. (I don't know, maybe you can force a value in it, but that would not be a good thing to do.) What I was trying to convey to you is that you need to insert temporary debugging lines in your script to display what values ARE in critical variables. You do that by using the echo command.about the original way I had it set up...forgive me for being such a novice (i'm self taught in php) how might i write the code to get a value for $_POST['sex']? The values I've given it were while setting up my form submission file was "male" and "female" but maybe i need to reflect that somewhere in my php code?
Code: Select all
echo $_POST['sex'];Re: PHP Form echo set of numbers based on input
Unzip this test .php file and run it on your system. It works perfectly well on my computer.ringartdesign wrote:hey thanks...I tried your new algorithm and it gave me an error code when testing it live.
- Attachments
-
- testagesex.zip
- (640 Bytes) Downloaded 14 times
-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
Re: PHP Form echo set of numbers based on input
hey thanks for that...it essentially worked, but also returned part of the source code. This is what displayed:
Array ( [0] => Array ( [age] => 18 [male] => 31.44 [female] => 35.91 ) [1] => Array ( [age] => 26 [male] => 40.78 [female] => 45.22 ) [2] => Array ( [age] => 31 [male] => 51.33 [female] => 57.9 ) [3] => Array ( [age] => 36 [male] => 65.95 [female] => 70.87 ) )
For 22, female, the quote is 35.91.
Array ( [0] => Array ( [age] => 18 [male] => 31.44 [female] => 35.91 ) [1] => Array ( [age] => 26 [male] => 40.78 [female] => 45.22 ) [2] => Array ( [age] => 31 [male] => 51.33 [female] => 57.9 ) [3] => Array ( [age] => 36 [male] => 65.95 [female] => 70.87 ) )
For 22, female, the quote is 35.91.
Re: PHP Form echo set of numbers based on input
Edit the code to stop that stuff being displayed then.
Re: PHP Form echo set of numbers based on input
That's NOT part of the source code, it's a display of the contents of the array which I displayed so you can see what is actually contained in the array. It is printed by using the print_r() function.ringartdesign wrote:hey thanks for that...it essentially worked, but also returned part of the source code. This is what displayed:
Array ( [0] => Array ( [age] => 18 [male] => 31.44 [female] => 35.91 ) [1] => Array ( [age] => 26 [male] => 40.78 [female] => 45.22 ) [2] => Array ( [age] => 31 [male] => 51.33 [female] => 57.9 ) [3] => Array ( [age] => 36 [male] => 65.95 [female] => 70.87 ) )
For 22, female, the quote is 35.91.
-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
Re: PHP Form echo set of numbers based on input
that worked...thank you! 2 more questions:
1. Is there a way to add a spouse's age/sex into the equation? Their rates would be the same as the applicant's.
2. I would like to add children to the equation. How could I add 30.04 to the quote for 1 child, 60.08 for 2 children, and 90.12 for 3 children?
1. Is there a way to add a spouse's age/sex into the equation? Their rates would be the same as the applicant's.
2. I would like to add children to the equation. How could I add 30.04 to the quote for 1 child, 60.08 for 2 children, and 90.12 for 3 children?
Re: PHP Form echo set of numbers based on input
The same way. What I showed you is a general approach: set up the factors in an array, then choose the arguments. It becomes more complicated as you add more criteria, so I don't know that I would use the same methodology with all those variants. A database solution might make more sense. That would be the topic of a lengthy discussion for which I don't have the time, currently.ringartdesign wrote:that worked...thank you! 2 more questions:
1. Is there a way to add a spouse's age/sex into the equation? Their rates would be the same as the applicant's.
2. I would like to add children to the equation. How could I add 30.04 to the quote for 1 child, 60.08 for 2 children, and 90.12 for 3 children?