gsvavarsson Posted October 6, 2011 Share Posted October 6, 2011 hi, i'm new PHP, so I need your help. I modified a calculator to my conditions but I want the result to overwrite the input boxes and submit btn. here is the code: ps. don't mind the Danish, i don't <?php $page = $_GET['page']; class calc { var $number1; var $number2; function add($number1,$number2) { $result =$number1-($number2*0.98); echo("Du kommer til med ad spare $result dkk<br>"); exit; } } $calc = new calc(); ?> <TITLE>PHP Calculator v1</TITLE> <form name="calc" action="?page=calc" method="POST"> Nuværende månedlig udgift: <input type=text name=value1><br> Forbrug af kopper/md.: <input typ e=text name=value2><br> <input type=submit value="Calculate"> </form> <?php if($page == "calc"){ $number1 = $_POST['value1']; $number2 = $_POST['value2']; $oper = $_POST['oper']; if(!$number1){ echo("Du skal tast ind nuværende måndlig udgift!"); exit; } if(!$number2){ echo("Du skal tast ind Forbrug p/md.!"); exit; } /*if(!$oper){ echo("You must select an operation to do with the numbers!"); exit; } */ if(!eregi("[0-9]", $number1)){ echo("De skal være nummer!"); exit; } if(!eregi("[0-9]", $number2)){ echo("De skal være nummers!"); exit; } if($number2<299) { echo("Månedlig forbrug skal være 300 eller mere"); } else{ $calc->add($number1,$number2); } } ?> mvh Link to comment https://forums.phpfreaks.com/topic/248554-price-comparison-calculator/ Share on other sites More sharing options...
thomasw_lrd Posted October 6, 2011 Share Posted October 6, 2011 The way I've been handling this is to actually put html in a function, and call that function to return the html with the new values. There might be a better way. switch ($action){ case 'step1': echo page_update_form($result); break; function page_update_form($result){ global $config; $ret.=' <form> <table> </table> </form>'; return $ret; } Link to comment https://forums.phpfreaks.com/topic/248554-price-comparison-calculator/#findComment-1276476 Share on other sites More sharing options...
gsvavarsson Posted October 6, 2011 Author Share Posted October 6, 2011 thanks but right now i still don't quite understand. could you explain it a bit further? best regards Link to comment https://forums.phpfreaks.com/topic/248554-price-comparison-calculator/#findComment-1276500 Share on other sites More sharing options...
Psycho Posted October 6, 2011 Share Posted October 6, 2011 In my opinion, the structure of your page really needs a makeover. You have a class that does nothing but a simple calculation. You then have a lot of error handling outside the class. If that is all your class is going to do, I wouldn't even use a class. But, you could move the validations into the class to make it more useful. Also, your error handling is not very elegant since it uses exit(). Instead you should always display a complete page. Also, your class defines two properties for the class - but never uses them. The variables you use in the add() method are not the same as the properties you defined for the class. I have made some major modifications below. I think it is all correct - but since the code has no comments and the output is not english I'm not entirely sure. <?php class calc { var $number1 = ''; var $number2 = ''; var $errors = array(); var $response = ''; function add($value1, $value2) { $this->set_number1($value1); $this->set_number2($value2); if(count($this->errors)) { $this->response = "The following errors occured:\n"; $this->response .= "<ul>\n"; foreach($this->errors as $error) { $this->response .= "<li>{$error}</li>\n"; } $this->response .= "</ul>\n"; return false; } else { $result = $this->number1 - ($this->number2*0.98); $this->response ="Du kommer til med ad spare $result dkk<br>"; return true; } } function set_number1($value) { $this->number1 = trim($value); if(empty($this->number1)) { $this->errors[] = "Du skal tast ind nuværende måndlig udgift!"; } elseif(!ctype_digit($this->number1)) { $this->errors[] = "De skal være nummer!"; } return; } function set_number2($value) { $this->number2 = trim($value); if(empty($this->number2)) { $this->errors[] = "Du skal tast ind Forbrug p/md.!"; } elseif(!ctype_digit($this->number2)) { $this->errors[] = "De skal være nummer!"; } elseif($this->number2>299) { $this->errors[] = "Månedlig forbrug skal være 300 eller mere"; } return; } } $calc = new calc(); if(isset($_POST['value1']) && isset($_POST['value2'])) { $calc->add(($_POST['value1'], $_POST['value2']); } ?> <html> <head> <title>PHP Calculator v1</title> </head> <body> <form name="calc" action="" method="post"> Nuværende månedlig udgift: <input type=text name="value1" value="<?php echo $calc->number1; ?>"><br> Forbrug af kopper/md.: <input type=text name="value2" value="<?php echo $calc->number2; ?>"><br> <input type=submit value="Calculate"><br><br> <?php echo $calc->response; ?> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/248554-price-comparison-calculator/#findComment-1276507 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.