Page 1 of 2
Form Calculator Index problems
Posted: Tue Sep 12, 2006 6:56 pm
by gkwhitworth
Ok, I have created a form for my freelance web design business that I am starting. I want to allow customers to basically have a chance to get a quick estimate for their total cost of the website. It calculates beautifully, but...if you don't select one of the options or you are new to the page....it has some issues because it doesn't know the variables so it lets you know this. How can I get around this and if anyone has any suggestions let me know.
error:
Code: Select all
Notice: Undefined index: asp in D:\users\gkwinspired.com\contact\contact.php on line 155
Php Code:
Code: Select all
<?
if (!isset ($_REQUEST['package'])){
echo "You didn't supply us with a package. So we can't come up with a valid estimate.";
}
$fname = $_REQUEST['fname'];
$lname = $_REQUEST['lname'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$address = $_REQUEST['address'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$package = $_REQUEST['package'];
$packagePrices = array (
'Basic' => 1500,
'Professional Lite' => 2300,
'Premium Professional' => 3500
);
$flash = $_REQUEST['flash'];
$flashPrice = 500;
$phpForm = $_REQUEST['phpForm'];
$phpPrices = array (
'1' => 200,
'2' => 400,
'3' => 600,
'4' => 800,
'5' => 1000
);
$asp = $_REQUEST['asp'];
$aspPrice = 1500;
$cms = $_REQUEST['cms'];
$cmsPrice = 3000;
if (isset ($_REQUEST['package'])) {
$answer = $packagePrices[$package];
}
if (isset ($_REQUEST['flash'])) {
$answer = $packagePrices[$package] + $flashPrice;
}
if (isset ($_REQUEST['phpForm'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm];
}
if (isset ($_REQUEST['asp'])) {
$answer = $packagePrices[$package] + $aspPrice;
}
if (isset ($_REQUEST['cms'])) {
$answer = $packagePrices[$package] + $cmsPrice;
}
if (isset ($_REQUEST['flash']) && ($_REQUEST['phpForm'])) {
$answer = $packagePrices[$package] + $flashPrice + $phpPrices[$phpForm];
}
if (isset ($_REQUEST['flash']) && ($_REQUEST['asp'])) {
$answer = $packagePrices[$package] + $flashPrice + $aspPrice;
}
if (isset ($_REQUEST['flash']) && ($_REQUEST['cms'])) {
$answer = $packagePrices[$package] + $flashPrice + $cmsPrice;
}
if (isset ($_REQUEST['phpForm']) && ($_REQUEST['asp'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm] + $aspPrice;
}
if (isset ($_REQUEST['phpForm']) && ($_REQUEST['cms'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm] + $cmsPrice;
}
if (isset ($_REQUEST['asp']) && ($_REQUEST['cms'])) {
$answer = $packagePrices[$package] + $aspPrice + $cmsPrice;
}
if (isset ($_REQUEST['phpForm']) && ($_REQUEST['asp']) && ($_REQUEST['flash']) && ($_REQUEST['cms'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm] + $aspPrice + $flashPrice + $cmsPrice;
}
?>
Thanks
--
Greg
Posted: Tue Sep 12, 2006 7:07 pm
by feyd
Don't blindly read data from $_REQUEST or any array for that matter. Simple enough. The following may be of interest.
Code: Select all
function pullElement($aElementName, $aDefaultValue = null, $aArray = null)
{
if (!is_array($aArray))
{
$aArray = $_REQUEST;
}
if (array_key_exists($aElementName, $aArray))
{
return $aArray[$aElementName];
}
else
{
return $aDefaultValue;
}
}
// example usage
$toy1 = pullElement('toy');
$toy2 = pullElement('toy', false);
$toy3 = pullElement('toy', 'not here', $_POST);
Posted: Tue Sep 12, 2006 7:13 pm
by gkwhitworth
confused....... some of it I recognize but there is some in there that just doesn't make sense, like $a, what the crap does that mean?
If you could explain it in a little more deatail, that would be great!
--
Greg

Posted: Tue Sep 12, 2006 7:20 pm
by feyd
There's no $a in the code.
$aElementName et al are variables. pullElement() is a function created by the code.
Posted: Tue Sep 12, 2006 7:22 pm
by RobertGonzalez
You should run your passed vars through isset() if they are not assigned. You should also not use $_REQUEST. If the vars are posted from a form use $_POST, if they are coming from the querystring, use $_GET.
Posted: Tue Sep 12, 2006 7:27 pm
by gkwhitworth
I think I need a book.

Oh well.
everah
You should run your passed vars through isset()
I am passing them through isset....look at the code. I would like to use what feyd posted, but I don't understand it enough to implement it.
form use $_POST, if they are coming from the querystring, use $_GET.
I plan on also having this form email me their info in the future....also display it on another page...so GET and Post aren't what I want to use. IF I am wrong let me know...I don't fully grasp the php stuff yet.
Posted: Tue Sep 12, 2006 9:14 pm
by RobertGonzalez
None of these assignments are checking if the $_REQUEST var is set. That is why you get the undefined index notice.
Code: Select all
<?php
$fname = $_REQUEST['fname'];
$lname = $_REQUEST['lname'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$address = $_REQUEST['address'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$package = $_REQUEST['package'];
$packagePrices = array (
'Basic' => 1500,
'Professional Lite' => 2300,
'Premium Professional' => 3500
);
$flash = $_REQUEST['flash'];
$flashPrice = 500;
$phpForm = $_REQUEST['phpForm'];
$phpPrices = array (
'1' => 200,
'2' => 400,
'3' => 600,
'4' => 800,
'5' => 1000
);
$asp = $_REQUEST['asp'];
$aspPrice = 1500;
$cms = $_REQUEST['cms'];
?>
You are going to want to use post to capture information from a form. $_REQUEST is a superglobal var that contains the $_POST and $_GET arrays. It is also a potential security risk, especially if you think you are checking a form var and instead someone passes a querystring var to your script.
Here is the thing....
Posted: Wed Sep 13, 2006 3:27 pm
by gkwhitworth
Feyd said:
function pullElement($aElementName, $aDefaultValue = null, $aArray = null)
{
if (!is_array($aArray))
{
$aArray = $_REQUEST;
}
if (array_key_exists($aElementName, $aArray))
{
return $aArray[$aElementName];
}
else
{
return $aDefaultValue;
}
}
// example usage
$toy1 = pullElement('toy');
$toy2 = pullElement('toy', false);
$toy3 = pullElement('toy', 'not here', $_POST);
Here is the thing feyd: I could easily copy your code and use it, and it would probably fix my problems, but I actually want to learn php - not just copy code and hack it up. So if you could explain it line for line, or someone else that gets what is happening in each line. Thanks....
I did some clicking!
Posted: Wed Sep 13, 2006 3:56 pm
by gkwhitworth
ok I did some clicking and I am going to try to disect this myself....you guys can tell me where I am wrong.
function pullElement($aElementName, $aDefaultValue = null, $aArray = null)
{
if (!is_array($aArray))
{
$aArray = $_REQUEST;
}
if (array_key_exists($aElementName, $aArray))
{
return $aArray[$aElementName];
}
else
{
return $aDefaultValue;
}
}
Code: Select all
function pullElement($aElementName, $aDefaultValue = null, $aArray = null)
{
This sets up the function of name "pullElement" then the arguments of the function ($a has to be here due to syntax issues?) I don't get these two parts
Code: Select all
$aDefaultValue = null, $aArray = null
Ok this now:
Code: Select all
if (!is_array($aArray))
{
$aArray = $_REQUEST;
}
this says if this is not an array, then null = $_REQUEST right?
then:
Code: Select all
if (array_key_exists($aElementName, $aArray))
{
return $aArray[$aElementName];
}
this says that if there is an array key (Name of Element, Null)) then return Null (Element name)
This part is quite confusing to me....where do they get the element names?
This is the part I like the most because it tells the form that if it isn't there...then tell it is the default value. So since I didn't get a response on the other posts I thought I'd narrow down my questions.
1. how does elementName work and why do I use it?
2. can someone explain the following lines of code:
Code: Select all
if (!is_array($aArray))
{
$aArray = $_REQUEST;
}
if (array_key_exists($aElementName, $aArray))
{
return $aArray[$aElementName];
}
I guess the main problem that I have is I am very, how shall I say it....uhm.....very objective I guess. Like all parts have to have a connection. So it doesn't make sense to me to just have code that does'nt have a reason. Sorry if that was too phsycological mumbo jumbo.....but the session is over.
--
Greg
Posted: Wed Sep 13, 2006 4:11 pm
by RobertGonzalez
Code: Select all
<?php
/**
* This function grabs an element of an array based on Key
*
* @param string $aElementName
* @param string $aDefaultValue
* @param array $aArray
* @return mixed
*/
function pullElement($aElementName, $aDefaultValue = null, $aArray = null)
{
// If a value was passed for $aArray and it is not an array...
if (!is_array($aArray))
{
// Set the value of $aArray to the superglobal array $_REQUEST
$aArray = $_REQUEST;
}
// If there is a key of the $aArray named $aElementName...
if (array_key_exists($aElementName, $aArray))
{
// Send the value for that key back to the calling script
return $aArray[$aElementName];
}
else
{
// Otherwise return the default value you set
return $aDefaultValue;
}
}
// example usage
$toy1 = pullElement('toy'); // If there is a $_REQUEST['toy'] the value of it will be returned, else null will be returned
$toy2 = pullElement('toy', false); // If there is a $_REQUEST['toy'] the value of it will be returned, else false will be returned
$toy3 = pullElement('toy', 'not here', $_POST); // If there is a $_POST['toy'] the value of it will be returned, else 'not here' will be returned
?>
Posted: Wed Sep 13, 2006 5:09 pm
by gkwhitworth
Thanks I'll give it a try.
Posted: Wed Sep 13, 2006 6:16 pm
by RobertGonzalez
Just to note, I didn't write that function. feyd originally posted it. You asked for an explanation of what it did so I posted the code back with comments. It is feyd's code and deserves any credit for it that might be offered.
Posted: Wed Sep 13, 2006 9:13 pm
by gkwhitworth
Yeah I know... I just like to know the ins and outs of the code so I can use the logistics in the future...thanks everah.
And Feyd.
--
Greg
feeling pretty dumb....
Posted: Thu Sep 14, 2006 12:23 am
by gkwhitworth
Ok here is my code:
Code: Select all
<?
if (!isset ($_POST['package'])){
echo "You didn't supply us with a package. So we can't come up with a valid estimate.";
}
function pullElement($aElementName, $aDefaultValue = null, $aArray = null)
{
if (!is_array($aArray))
{
$aArray = $_POST;
}
if (array_key_exists($aElementName, $aArray))
{
return $aArray[$aElementName];
}
else
{
return $aBasic;
}
}
$fname = pullElement('fname','not here', $_POST);
$lname = pullElement('lname','not here', $_POST);
$email = pullElement('email','not here', $_POST);
$phone = pullElement('phone','not here', $_POST);
$address = pullElement('address','not here', $_POST);
$city = pullElement('city','not here', $_POST);
$state = pullElement('state','not here', $_POST);
$zip = pullElement('zip','not here', $_POST);
$package = pullElement('package','not here', $_POST);
$packagePrices = pullElement('packagePrices',false, $_POST);
$flash = pullElement('flash','not here', $_POST);
$phpForm = pullElement('phpForm','not here', $_POST);
$asp = pullElement('asp','not here', $_POST);
$cms = pullElement('cms','not here', $_POST);
$packagePrices = array (
'Basic' => 1500,
'Professional Lite' => 2300,
'Premium Professional' => 3500
);
$flashPrice = 500;
$phpPrices = array (
'1' => 200,
'2' => 400,
'3' => 600,
'4' => 800,
'5' => 1000
);
$aspPrice = 1500;
$cmsPrice = 3000;
if (isset ($_POST['package'])) {
$answer = $packagePrices[$package];
}
if (isset ($_POST['flash'])) {
$answer = $packagePrices[$package] + $flashPrice;
}
if (isset ($_POST['phpForm'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm];
}
if (isset ($_POST['asp'])) {
$answer = $packagePrices[$package] + $aspPrice;
}
if (isset ($_POST['cms'])) {
$answer = $packagePrices[$package] + $cmsPrice;
}
if (isset ($_POST['flash']) && ($_POST['phpForm'])) {
$answer = $packagePrices[$package] + $flashPrice + $phpPrices[$phpForm];
}
if (isset ($_POST['flash']) && ($_POST['asp'])) {
$answer = $packagePrices[$package] + $flashPrice + $aspPrice;
}
if (isset ($_POST['flash']) && ($_POST['cms'])) {
$answer = $packagePrices[$package] + $flashPrice + $cmsPrice;
}
if (isset ($_POST['phpForm']) && ($_POST['asp'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm] + $aspPrice;
}
if (isset ($_POST['phpForm']) && ($_POST['cms'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm] + $cmsPrice;
}
if (isset ($_POST['asp']) && ($_POST['cms'])) {
$answer = $packagePrices[$package] + $aspPrice + $cmsPrice;
}
if (isset ($_POST['phpForm']) && ($_POST['asp']) && ($_POST['flash']) && ($_POST['cms'])) {
$answer = $packagePrices[$package] + $phpPrices[$phpForm] + $aspPrice + $flashPrice + $cmsPrice;
}
?>
Here are the errors I am getting:
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
Notice: Undefined variable: aBasic in D:\users\gkwinspired.com\contact.php on line 93
I am obvously doing something wrong....I think that I have to rename the 'ElementName' to the array name within my form....but I am not sure. Please help.
--
Greg
Posted: Thu Sep 14, 2006 8:28 am
by RobertGonzalez
Since we don't have line numbers to reference (like you do

) can you post lines 90 to 95 of your code?