zero-perfoliate
zero-perfoliate

Author Topic: Form Calculation php  (Read 446 times)

Offline illpayyoulater

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
Form Calculation php
« on: January 30, 2009, 12:21:42 PM »
Hello - Firstly appologise I'm going to phrase my question badly. But I'll try my best!

Basically on my website ->  http://rfcoxandsonelectricalservices.com/services2.php#z4 I'm trying to get my potential customer to insert a value in the text box press the "The estimate is:" and the answer gets published back on the same webpage next to the £ sign.
BUT If the customer enters a value ranging from 1-10 I want that value to be multiplied by my hidden value number01, & if they enter a value between 11-25 it gets multiplied buy number001 etc.. but If the customer enters more than 151 it reports an error like "please contact us for a quote"
I have managed to get a simpler calculation to work higher up that page but with a simple multiplying of the entered value!

I have loaded the code I am trying to use....
Code: [Select]
<?php
$number1 
$_GET['number1'];
$number01 $_GET['number01'];
$number001 $_GET['number001'];
$number0001 $_GET['number0001'];
$number00001 $_GET['number00001'];
$op $_GET['op'];

if ( 
$op == "The estimate is:" )
{
  
$filename 'ans4.html';
if (
$number1 151){ 
$answer $number1*$number00001;
}
if (
$number1 31){
$answer $number1*$number0001;
}
if (
$number1 16){
$answer $number1*$number001;
}
if (
$number1 6){
$answer $number1*$number01;
}

if (
is_writable($filename)) {

    if (!
$handle fopen($filename'r+')) {
         echo 
"Cannot open file ($filename)";
         exit;
    }
    
    if (
fwrite($handle$answare) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;
    }
    echo (
'<meta http-equiv="refresh" content="0;url=calc.php" />');

    
fclose($handle);

}else { 
echo 
"The file $filename is not writable";
}
}
if ( 
$op == "Reset" 
{
$filename 'ans4.html';
$answare "_";

if (
is_writable($filename)) {

    if (!
$handle fopen($filename'wb')) {
         echo 
"Cannot open file ($filename)";
         exit;
    }

    if (
fwrite($handle$answare) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;
    }
    echo (
'<meta http-equiv="refresh" content="0;url=services2.php#z4" />');

    
fclose($handle);

} else {
    echo 
"The file $filename is not writable";
}
?>
Can you help or point me in a direction that will guide me to my goal.

Thanks

Offline jenovachild

  • New PHP Members
  • Posts: 4
  • Karma: +0/-0
Re: Form Calculation php
« Reply #1 on: February 12, 2009, 08:41:38 PM »
Hey,

Ok, so there are a few options available to you. Let me just start by saying that re-writing the file just to make a "dynamic change" to content data is not the way to go. You can have a page "ready" for a form submit, by printing variables you know will be there once they have submit the form:

Code: [Select]
<?php
if(!$_GET)
print 
'I will tell you your name in a minute! First, you have to tell me<br /><br />';
else
print 
'Hello, ' $_GET['name'] . '!<br /><br />';
?>

<form method="GET">
Your name? <input type="text" name="name" />
<input type="submit" value=" OK " />
</form>

So, of course, it stands to reason that you can also alter the variable $_GET['name'], or, if it is a number, multiply it.

Now, if you want a "seemless" action to happen, and still require functionality of PHP later down the track, for file writing, database manip. etc. Then a combination of javascript & PHP will work, otherwise javascript alone will do the trick perfectly. Here is soemthing you should try out:

Create 2 files in the same directory on your server: form.html and parse.php

form.html contents:

Code: [Select]
<html>
<head>
<script type="text/javascript">

var http = null;

function calculate()
{
var myForm = document.form1;
var queryString = "";
var url = "parse.php";

// loop through the form and retrieve information for all text boxes into query string
// to send to xml http object
for(var i = 0; i < myForm.elements.length; i++)
if(myForm.elements[i].type == "text")
queryString += (myForm.elements[i].name + "=" + myForm.elements[i].value + "&");
queryString = queryString.substr(0,queryString.length-1);

// append queryString to url
url += "?" + queryString;

// attempt to create new xmlhttp request object
if(window.XMLHttpRequest)
http = new XMLHttpRequest();
else if (window.ActiveXObject)
http = new ActiveXObject("Microsoft.XMLHTTP");

if(http != null)
{
http.open("GET",url,true);
http.onreadystatechange = function()
{
if(http.readyState == 4 || http.readyState == 200)
{
var response = http.responseText;
var fieldArr = response.split("\|");
for(var i = 0; i < fieldArr.length; i+=2)
document.getElementById(fieldArr[i] + "_div").innerHTML = "£" + fieldArr[i+1];
}
}
http.send(null);
}
else
alert('Sorry, your browser does not support XMLHttpRequest');

}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>  </label>
  <table width="550" border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td width="144"><input type="text" name="textfield" id="textfield" /></td>
      <td width="392"><div id="textfield_div"></div></td>
    </tr>
    <tr>
      <td><input type="text" name="textfield2" id="textfield2" /></td>
      <td><div id="textfield2_div"></div></td>
    </tr>
    <tr>
      <td><input type="text" name="textfield3" id="textfield3" /></td>
      <td><div id="textfield3_div"></div></td>
    </tr>
    <tr>
      <td><input type="text" name="textfield4" id="textfield4" /></td>
      <td><div id="textfield4_div"></div></td>
    </tr>
    <tr>
      <td><input type="text" name="textfield5" id="textfield5" /></td>
      <td><div id="textfield5_div"></div></td>
    </tr>
    <tr>
      <td><input type="text" name="textfield6" id="textfield6" /></td>
      <td><div id="textfield6_div"></div></td>
    </tr>
    <tr>
      <td><input type="text" name="textfield7" id="textfield7" /></td>
      <td><div id="textfield7_div"></div></td>
    </tr>
    <tr>
      <td><input type="button" name="button" id="button" value="Button" onclick="calculate()" /></td>
      <td>
      </td>
    </tr>
  </table>
</form>
</body>
</html>

parse.php contents:

Code: [Select]
<?php

$num1 
1;
$num2 2;
$num3 3;
$num4 4;

foreach(
$_GET as $k => $v)
{
if($v 151 && $v 31)
$v *= $num4;
else if($v 31 && $v 16)
$v *= $num3;
else if($v 16 && $v 6)
$v *= $num2;
else if($v && $v > -1)
$v *= $num1;

echo $k.'|'.$v.'|';
}
?>

This is using a combination of PHP and javascript to accomplish the goal. Take note of the naming conventions, each textfield can have whatever name you wish, though the <div> tag beside each one must have the same name as its corresponding text field, with the _div added to it as you can see here.

$num1 - $num4 in parse.php are your num01 - num00001. It is best that these values remain in PHP only and not parsed through the html form as an input field, otherwise it is not difficult for someone to change those values before submitting the form.


Try and get this to work and come back if you have any further problems.