Jump to content

Need help with maths in a form, please help very important


apg1985

Recommended Posts

Hi Guys,

 

If you could help me that would be great, its very important for a project and needed ASAP.

 

I've got a form with select fields and 1 label at the very end. The first select field you can choose £1000 or £2000, once you select one of them it shows up in the label at the end. After that select field is another select field with yes and no, if you choose yes nothing happens but if you choose no it takes 10% away from the label, after that select field is another select field the same as the yes and no one but when you select no this time it adds £100.

 

I've add the form below and also added a name for each selection which I hope helps. Any direction would really help.

 

<form>

<table>

<tr>

<td>Select value</td>

<td>

<select name="select1">

<option name=”1000”>£1000</option>

<option name=”2000”>£2000</option>

</select>

<td>

</tr>

<tr>

<td>Select Yes or No</td>

<td>

<select name="select2">

<option name=”yes1”>Yes</option>

<option name=”no1”>No</option>

</select>

<td>

</tr>

<tr>

<td>Select Yes or No</td>

<td>

<select name="select3">

<option name=”yes2”>Yes</option>

<option name=”no2”>No</option>

</select>

<td>

</tr>

<label name=”label”/>

</form>

Link to comment
Share on other sites

Thanks for doing that.

 

It is basic arithmetic but because im new to php I find it quite hard to put the calculation from paper into code.

 

Stupid question but got to ask, is $price the label?

 

Why do I need to give the options a value is it to clean the code up or for a different reason (just to help give me a better understanding of what going on)?

 

O and do you mean like <option name="name" value="value">

Link to comment
Share on other sites

Why do I need to give the options a value is it to clean the code up or for a different reason (just to help give me a better understanding of what going on)?

 

If they do not have any value then you aren't sending anything. It's just a matter of doing e.g. <option value="1000">$1000</option> essentially.

 

Stupid question but got to ask, is $price the label?

 

$price was set to $_POST['select1'], i.e. the first select in your code.

Link to comment
Share on other sites

I'm testing this now and for some reason uits not working, please help, its not taking the selected number and showing it in the input field at the end ($price)

 

CODE:

 

<!-- START EDITABLE1 -->

<?php

$price = (float) $_POST['select1'];

if ($_POST['select2'] == 'yes') {

$price *= 0.9;

}

 

if ($_POST['select3'] == 'yes') {

$price += 100;

}

?>

<form method="post" name="form1" id="form1">

<table width="500px">

<tr>

<td>Select value</td>

<td>

<select name="select1">

<option name="1000">£1000</option>

<option name="2000">£2000</option>

</select>

<td>

</tr>

<tr>

<td>Select Yes or No</td>

<td>

<select name="select2">

<option name="yes1">yes</option>

<option name="no1">no</option>

</select>

<td>

</tr>

<tr>

<td>Select Yes or No</td>

<td>

<select name="select3">

<option name="yes2">yes</option>

<option name="no2">no</option>

</select>

<td>

</tr>

<tr>

<td><input type="button" value="Submit"/></td>

<td><input type="text" name="price" id="price"/></td>

</tr>

</table>

</form>

<!-- END EDITABLE1 -->

Link to comment
Share on other sites

See the button isnt part of it, all I want is the user to be able to select a number say £1000 and that to appear in the input box at the end. Do the rest of the selections and while that is happening %'s are being taken off the number in the input box at end.

 

When the submit button is clicked its just going to take the final value in that input box and email it to me.

Link to comment
Share on other sites

See the button isnt part of it, all I want is the user to be able to select a number say £1000 and that to appear in the input box at the end. Do the rest of the selections and while that is happening %'s are being taken off the number in the input box at end.

 

When the submit button is clicked its just going to take the final value in that input box and email it to me.

PHP can only do the calculation if a request is sent to the server. If you want to do this calculation without submitting a request to the server, then you'll need to use javascript, or other client-side scripting language to perform the mathematics.
Link to comment
Share on other sites

Dam,

 

Ok well would it be possible to do what i want if it did the php on submission like through the button

 

So like <form action="loadphp.php">

 

and in the loadphp page I had

 

<?php

$price = (float) $_POST['select1'];

if ($_POST['select2'] == 'yes1') {

$price *= 0.9;

}

 

if ($_POST['select3'] == 'yes2') {

$price += 100;

}

 

if(isset($_POST['submit'])) {

 

$to = "my email address";

$subject = "FORM";

$price = $_POST['price'];

 

$body = "$price";

 

header("Location: index.php");

mail($to, $subject, $body);

}

else

{

echo ("Not Sent");

}

?>

Link to comment
Share on other sites

Ok this is just a update of my code, i think were nearly there. Ive got the submit button working and its sending the number in the price field to my email address but its showing a 0.

 

HTML:

<?php
$price = (float) $_POST['select1'];
if ($_POST['select2'] == 'yes1') {
$price *= 0.9;
}

if ($_POST['select3'] == 'yes2') {
$price += 100;
}
?>
<form action="1testformphp.php" method="post" name="form1" id="form1">
<table width="500px">
<tr>
<td>Select value</td>
<td>
<select name="select1">
<option name="1000" value="1000">1000</option>
<option name="2000" value="2000">2000</option>
</select>
<td>
</tr>
<tr>
<td>Select Yes or No</td>
<td>
<select name="select2">
<option name="selectbox">select box</option>
<option name="yes1" value="yes1" id="yes1">yes1</option>
<option name="no1" value="no1" id="yes1">no1</option>
</select>
<td>
</tr>
<tr>
<td>Select Yes or No</td>
<td>
<select name="select3">
<option name="yes2" value="yes2" id="yes2">yes2</option>
<option name="no2" value="no2" id="no2">no2</option>
</select>
<td>
</tr>
<tr>
<td><input type="text" name="price" id="price" value="<?php echo $price; ?>"></td>
<td><input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>

 

1testformphp.php (email php)

 

<?php
if(isset($_POST['submit'])) {

$to = ""; 
$subject = "FORM"; 
$price = $_POST['price']; 
  
$body = "$price"; 
  
header("Location: index.php"); 
mail($to, $subject, $body); 
} 
else 
{
echo ("Not Sent");
}
?>

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.