Jump to content

PHP Form Math Help


PauliePaulie

Recommended Posts

 

Hello Everyone,

 

I'm a newbie trying to improve my script below.

The script works fine but I would like to add the 3 radio buttons computations enhancements

Ergo,  if a user chooses 'Matte Paper" a 10% would be added to the Price Quote result.

If a user chooses ' Gloss Paper,  a 10% would be added to the Price Quote result.

If a user chooses 'Canvas" , a 20 % would be added to the Price Quote result.

If someone could assist with the math or post me a helpful link it would be greatly appreciated.

Thank you.

 

Paul from Melbourne, Australia.

 

 

<div style="border:dashed 1px; width:570px;">

 

<form method="post" action="<?php echo $_SERVER['php_SELF'];?>">

 

<p>

1. Enter the size (100w.x180h. cm) you want your picture printed:

Width

<input type="text" name="width" size="1"  maxlength="3"

value="0" onFocus="this.value=''; this.style.color='#ff0000';">

Height

<input type="text" name="height" size="1" value="0"  maxlength="3"

value="0" onFocus="this.value=''; this.style.color='#ff0000';">

</p>

 

 

<p>

2. Choose the printing material for your picture:

<input type="radio"  value="cms in Matte paper"  name="material" size="3"  maxlength="3" /> Matte Paper

<input type="radio"  value="cms in Gloss Paper:"  name="material" size="3"  maxlength="3" /> Gloss Paper

<input type="radio" value="cms in Canvas:"  name="material"  size="3"  maxlength="3" />  Canvas

</p>

 

<p>

3. Submit your info for our Price Quote below :

<input type="submit" name="submit" value="Submit" />

or:

<input type="submit" name="clear" value="Reset" />

</p>

 

 

<p>

5. Our Price Quote to print your artwork sized: 

 

<?php

 

$width = $_POST['width'];

/* if artwork wider than 101 cm no quote, exits*/

if ($width >=101)

{echo "Error!"; exit ;}

 

$height = $_POST['height'];

/* if artwork higher than 181 cm no quote, script exits */

if ($height >=181)

{echo "Error!"; exit ;}

 

$measurement = $_POST['measurement'];

$material = $_POST['material'];

$result = $width * $height;

echo "

$width

x

$height

$material

is: ";

echo "$".number_format($result,2). "" ;

?>

</p>

 

</form>

</div>

 

 

Link to comment
Share on other sites

There is nothing in that code to calculate a price, so it's kind of hard to add 10% or 20% to a value that does not exist!

 

Also, something seems off about that script. The script seems to be primarily for entering the specifications, but there is some code at the end for calculating the area of the piece. You should have separate scripts for the form and the processing logic. They can be in the same file, but they should be logically separated.

Link to comment
Share on other sites

Try this for your radio buttons:

 

2. Choose the printing material for your picture:
<input type="radio"  value="1.1"  name="material" size="3"  maxlength="3" /> Matte Paper
<input type="radio"  value="1.1"  name="material" size="3"   maxlength="3" /> Gloss Paper
<input type="radio" value="1.2"  name="material"  size="3"  maxlength="3" />  Canvas

 

Then your $result would be:

 

$result = $width * $height * $material;

Link to comment
Share on other sites

Try this for your radio buttons:

 

2. Choose the printing material for your picture:
<input type="radio"  value="1.1"  name="material" size="3"  maxlength="3" /> Matte Paper
<input type="radio"  value="1.1"  name="material" size="3"   maxlength="3" /> Gloss Paper
<input type="radio" value="1.2"  name="material"  size="3"  maxlength="3" />  Canvas

 

Then your $result would be:

 

$result = $width * $height * $material;

 

But, then they would not have the information of what stock (i.e. paper) was selected for the estimate. I did consider the OP wanted to adjust the $result variable, but he stated in the description that he wanted to adjust the price. For all I know there are other determination value for the price. If he only wanted to adjust the 'area' value he did a poor job of explaining it. But, if that's the case then you would want to create an array to use both for the creation of the radio group and for calculating the adjustment. By using an array (or even a database) you don't have to worry about the radio group options getting out of sync with the code that does the calculation.

 

Here is a more thorough script

<?php

$materialsList = array(
    'Matte Paper' => .1,
    'Gloss Paper' => .1,
    'Canvas'      => .2
);

//Create radio group options for material
$materialGroup = '';
foreach($materialsList as $label => $value)
{
    $materialGroup .= "<input type='radio'  value='{$label}'  name='material' size='3'  maxlength='3' /> {$label}\n";
}

//Determine quote if form was posted
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $width  = isset($_POST['width'])  ? floatval($_POST['width'])  : 0;
    $height = isset($_POST['height']) ? floatval($_POST['height']) : 0;
    $material = isset($_POST['material']) ? $_POST['material'] : false;
    $measurement = isset($_POST['measurement']) ? $_POST['measurement'] : false;

    $errors = array();
    /* if artwork wider than 101 cm no quote, exits*/
    if ($width >=101 || $width <= 0)
    {
        $errors[] = "Width must be between 1cm and 100cm";
    }
    /* if artwork higher than 181 cm no quote, script exits */
    if ($height >=101 || $height <= 0)
    {
        $errors[] = "Height must be between 1cm and 181cm";
    }
    if(!isset($materialsList[$material]))
    {
        $errors[] = "Invalid material type selected";
    }

    if(count($errors))
    {
        $response  = "The following errors occured:\n";
        $response .= "<ul>\n";
        foreach($errors as $error)
        {
            $response .= "<li>$error</li>\n";
        }
        $response .= "</ul>\n";
    }
    else
    {
        $adjustment = $materialsList[$material];
        $price = ($width * $height);
        $price = $price * (1 + $adjustment); //Make adjustment
        $price = '$' . number_format($price, 2); //Format for display

        $response  = "5. Our Price Quote to print your artwork sized:<br>\n";
        $response .= "{$width} x {$height} cms in {$material} is: {$price}";
    }
}

?>

<div style="border:dashed 1px; width:570px;">

<form method="post" action="<?php echo $_SERVER['php_SELF'];?>">

<p>
1. Enter the size (100w.x180h. cm) you want your picture printed:
Width
<input type="text" name="width" size="1"   maxlength="3"
value="0" onFocus="this.value=''; this.style.color='#ff0000';">
Height
<input type="text" name="height" size="1" value="0"  maxlength="3"
value="0" onFocus="this.value=''; this.style.color='#ff0000';">
</p>

<p>
2. Choose the printing material for your picture:
<?php echo $materialGroup; ?>
</p>

<p>
3. Submit your info for our Price Quote below :
<input type="submit" name="submit" value="Submit" />
or:
<input type="submit" name="clear" value="Reset" />
</p>


<p>
<?php echo $response; ?>
</p>

</form>
</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.