darran
Sep 17 2006, 02:35 AM
This can be considered my first php page after a long while, I did a little basic php years ago and I have totally forgotten most of it. But I have come up with a page; a simple calculator if you will with the essential add, subtract, multiply and divide functions. When I load the page on my localhost, I get this error  However when I upload it to trap17, there is no problem, please take a look http://darran.trap17.com/php/Calculator/calculator.phpI know this does not matter but I want to find out where I went wrong on my first php page. The code listing for my calculator.php are as follows CODE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Calculator</title>
<style type="text/css"> <!-- .style1 {font-family: Georgia, "Times New Roman", Times, serif} .style3 {font-family: Georgia, "Times New Roman", Times, serif; font-size: 36px; } body,td,th { font-family: Georgia, Times New Roman, Times, serif; } --> </style> </head>
<body> <form id="frmCalculator" name="Calculator" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <table width="200" border="1" align="center" cellpadding="50" cellspacing="0" bordercolor="#000000"> <tr> <td><table width="562" border="0" cellspacing="0" cellpadding="0"> <tr> <td><div align="center"><span class="style3">Simple Calculator</span> </div></td> </tr> </table> <br /> <table width="546" border="0" align="center" cellpadding="5" cellspacing="0" bordercolor="#000000"> <tr> <td width="101"><span class="style1">1st Number </span></td> <td width="151"><span class="style1"> <label> <input name="txt1stNumber" type="text" id="txt1stNumber" /> </label> </span></td> <td width="110"><span class="style1">2nd Number </span></td> <td width="144"><span class="style1"></span> <input name="txt2ndNumber" type="text" id="txt2ndNumber" /></td> </tr> <tr> <td colspan="4"><label></label> <span class="style1"> <label></label> <label></label> <label></label> </span> <label></label> <div align="center"> <label> <span class="style1"> <input type="radio" name="rad1" value="Add" /> Add</span></label> <span class="style1"> <input type="radio" name="rad1" value="Subtract" /> <label>Subtract</label> <label> <input type="radio" name="rad1" value="Multiply" /> Multiply</label> <label> <input type="radio" name="rad1" value="Divide" /> Divide</label> </span></div></td> </tr> <tr> <td colspan="4"><div align="center"> <input name="btnSubmit" type="submit" class="style1" value="Submit"/> <input name="btnReset" type="reset" class="style1" id="btnReset" value="Reset"/> </div></td> </tr> <tr> <td colspan="4"><strong>Result:</strong> <?php $rad1 = $_POST['rad1']; $_1stNumber = $_POST['txt1stNumber']; $_2ndNumber = $_POST['txt2ndNumber']; $result = "";
if ($rad1 != null){ switch($rad1){ case "Add": $result = $_1stNumber + $_2ndNumber; break; case "Subtract": $result = $_1stNumber - $_2ndNumber; break; case "Multiply": $result = $_1stNumber * $_2ndNumber; break; case "Divide": $result = $_1stNumber / $_2ndNumber; break; } echo("<font color=#FF0000>$result</font>"); } ?></td> </tr> </table></td> </tr> </table> </form> <p> </p> </body> </html> I would appreciate it if anyone of you can guide me in this?
Reply
Albus Dumbledore
Sep 17 2006, 02:51 AM
PHP does not work when opened in a browser. for instance, when i try to look at my webpages in a browser when not uploaded to a website it will not show the PHP Includes that i have in my webpage. i dont know why, but it just wont show PHP functions like that
Reply
darran
Sep 17 2006, 02:57 AM
I believe php works when opened in a browser, I mean I uploaded the php page into my trap17 host and it works fine. However when I do it on a localhost, I have errors coming. I think when you upload it, for security reason, Firefox or IE will simply format it. Anyway do you know what is wrong with my page? They both have the same code except 1 is previewed using my localhost and the other, on the trap17 host
Reply
Albus Dumbledore
Sep 17 2006, 02:58 AM
what do you mean by your localhost?
Reply
tdktank59
Sep 17 2006, 03:02 AM
he means hes got apache and php instaled on his local computer or a local server at his place for testing... or something along those lines i belive... if not theres the problem lol
Reply
darran
Sep 17 2006, 03:13 AM
tdktank59 says it. I have got apache and php installed in my system and the problem should not be anything pertaining to these services not installed in my system.
Reply
jlhaslip
Sep 17 2006, 07:47 AM
Typically, I would've handled this differntly than you have done. Check the following code which includes an input form for calculating the value of a purchase. Notice the difference in the logical structure? The first thing this form does is check for a previously 'submitted' value and then handle the form or else present the form and include the 'hidden' field which is what is used in the first logic block above it. CODE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Widget Cost Calculator</title> </head> <body> <?php // Check if the form has been submitted. if (isset($_POST['submitted'])) {
// Cast all the variables to a specific type. $quantity = (int) $_POST['quantity']; $price = (float) $_POST['price']; $tax = (float) $_POST['tax'];
// All variables should be positive! if ( ($quantity > 0) && ($price > 0) && ($tax > 0)) { // Calculate the total. $total = ($quantity * $price) * (($tax/100) + 1); // Print the result. echo '<p>The total cost of purchasing ' . $quantity . ' widget(s) at $' . number_format ($price, 2) . ' each is $' . number_format ($total, 2) . '.</p>'; } else { // Invalid submitted values. echo '<p><font color="red">Please enter a valid quantity, price, and tax rate.</font></p>'; } } // End of main isset() IF.
// Leave the PHP section and create the HTML form. ?> <h2>Widget Cost Calculator</h2> <form action="calculator.php" method="post"> <p>Quantity: <input type="text" name="quantity" size="5" maxlength="10" value="<?php if (isset($quantity)) echo $quantity; ?>" /></p> <p>Price: <input type="text" name="price" size="5" maxlength="10" value="<?php if (isset($price)) echo $price; ?>" /></p> <p>Tax (%): <input type="text" name="tax" size="5" maxlength="10" value="<?php if (isset($tax)) echo $tax; ?>" /> (optional)</p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> </body> </html>
There is always 'another way to do things, so check this page to see if you can recognize the difference in structure. As to what is wrong with your form? I don't know. I had a terribly diffifult time when I read it. Tables and div's all mashed in together? No hidden field to see if the form had been submitted? Multiple uses of the same 'name'. The best thing I can do about your problem is to show you a sample of code which I would've used and perhaps you can learn from it and find the error. Besides, it is late at night and I need some sleep. Might have another look in the morning. What version of php are you running on the XAMMP set-up? Might be a version problem. And the reset button doesn't clear a calculated value when I tried to use the page you posted. Good effort for the first page, at least you got a result. Besides, the messages are 'Notices', not errors. Keep learning.
Reply
darran
Sep 17 2006, 10:37 AM
QUOTE(jlhaslip @ Sep 17 2006, 03:47 PM)  Typically, I would've handled this differntly than you have done. Check the following code which includes an input form for calculating the value of a purchase. Notice the difference in the logical structure? The first thing this form does is check for a previously 'submitted' value and then handle the form or else present the form and include the 'hidden' field which is what is used in the first logic block above it.
There is always 'another way to do things, so check this page to see if you can recognize the difference in structure. As to what is wrong with your form? I don't know. I had a terribly diffifult time when I read it. Tables and div's all mashed in together? No hidden field to see if the form had been submitted? Multiple uses of the same 'name'. The best thing I can do about your problem is to show you a sample of code which I would've used and perhaps you can learn from it and find the error. Besides, it is late at night and I need some sleep. Might have another look in the morning.
What version of php are you running on the XAMMP set-up? Might be a version problem. And the reset button doesn't clear a calculated value when I tried to use the page you posted.
Good effort for the first page, at least you got a result. Besides, the messages are 'Notices', not errors. Keep learning.
I have checked the structure of my code, based on the difference, I am missing a hidden input type and my php code is being placed at the bottom. Based on my understanding with your code, the isset() method checks for whether a variable is null or not, and this checks for the submission of the form. Correct? However I am not too sure how the hidden field 'submitted' works, does the boolean value changes after I submit. For e.g. will the value of the hidden field be false until I submit the form? And also the reason I put the php code at the bottom is because I want to print the error message in a particular td of the table. Is there a way I can do this but at the same time putting the php code at the very beginning of the body tag? The issue with you seeing all the tables and divs being mashed up is because I wanted to organise my simple calculator page in a table layout and do some centralising. The centralising was done using the GUI of Dreamweaver, and I am sure that there is no problem since I did not move the codes. However there are such redundant codes in there, I will clear that up. I do not recall installing PHP in my system. XAMPP only provides Apache and MySQL, and I am using easyPHP for the server. Clearing the result value? I am not too sure how to do that, I just need to reset the $result variable to null right? But where is the place which allows me to do that? I am thinking along the lines of this, do look it through CODE <?php if (isset($_POST['reset']) { $result = null; }
Am I right or is there a more appropriate way of doing it? Thank you for the compliments, I will continue working on it to make it a perfect page, but I do need your help in certain areas.
Reply
darran
Sep 17 2006, 02:44 PM
I did some modification to the codes CODE <?php $result = ""; if (isset($_POST['submitted'])){ $_1stNumber = $_POST['txt1stNumber']; $_2ndNumber = $_POST['txt2ndNumber']; if (!is_numeric($_1stNumber) || !is_numeric($_2ndNumber)) { echo("<font color=#FF0000>Enter a number in the textbox</font>"); } else { if (isset($_POST['rad1'])){ $rad1 = $_POST['rad1']; switch($rad1){ case "Add": $result = $_1stNumber + $_2ndNumber; break; case "Subtract": $result = $_1stNumber - $_2ndNumber; break; case "Multiply": $result = $_1stNumber * $_2ndNumber; break; case "Divide": $result = $_1stNumber / $_2ndNumber; break; } echo("<font color=#FF0000>$result</font>"); } else { exit("<font color=#FF0000>No operation selected</font>"); } } } ?> However I am left still with the placing of the php code as well as the reset button. I want to place the error message in a particular place but at the same time I want the php codes to be at the beginning of the <body> tag. And the reset button, I want to reset the $result as well. I have done some validation, and you can preview this page at http://darran.trap17.com/php/Calculator/calculator.php
Reply
jlhaslip
Sep 17 2006, 02:59 PM
Nice clean code now. Try setting the $result = 0; in the line where you have $result = ''; at the top of the page. Also, watch for division by zero if the rad1=divide. XAMPP provides Apache, MySQL,Perl, Php, Mercury Mail, an FTP client, phpadmin and even more. It is very easy to install. A default install will handle most situations. It should NOT be used in a live web situation, though, due to security factors without changes to its security. You need to know what settings to change on the server, etc.
Reply
Latest Entries
darran
Sep 23 2006, 12:57 PM
I know what you are getting at. For my problem, I want the result to be reflected back at the original value. So after clicking the default reset button, I want the result to be shown as 0 or something like that. As you said Javascript handles button clicks on the client side, so I want javascript to reset the value but this variable is a php variable, how can I port it into javascript so that I can reset it?
Reply
Spectre
Sep 21 2006, 07:13 AM
It doesn't work like that. PHP is only server-side, and JavaScript is only client-side. As I said, the variables are going to have whatever value you assign them in the script. You can't assign them a value of '0', and then expect them to retain that value on later executions, because they won't. The only way you can do that is by storing the value somewhere, and recalling it later. If someone executes your script to add the values '2' and '3' together, the script isn't going to 'remember' that it used '2' on the next run, so the variable isn't going to have a default value of '2' or anything. I think that's what you're getting at, anyway.
Reply
darran
Sep 21 2006, 04:37 AM
My question was a way to reset the values by pressing a button, it is a default reset button but I want it to be able to reset my values to the original ones I defined. In other words, I can reset the value of the variable using javascript? Something along the lines of CODE <script language="javascript" for="btnReset" event="onclick"> <? php $result = 0; ?> </script> Please correct me on this? But lets say a person decides to disable javascript for whatever wierd reason, is there a way to handle button clicks in a web-based environment?
Reply
Spectre
Sep 19 2006, 07:11 AM
I'm not quite sure what you're asking, but if you mean how do you reset the variables within PHP when the Reset button is pressed... the value is only going to be sent to the server when the user clicks 'Submit'. The Reset button is handled only on the client-side, so clicking it doesn't interact with the server at all. The variables are going to contain the value they were initially assigned (in this case, the integer value '0') each time the script is executed, and the values will not be 'remembered'. And no, the only way to handle button clicks (and most other client-side events outside of the standard markup) is via JavaScript. Most people have it enabled.
Reply
darran
Sep 19 2006, 04:21 AM
Here is the code CODE <?php function calculate() { $result = 0; $_1stNumber = 0; $_2ndNumber = 0;
if (isset($_POST['submitted'])){ $_1stNumber = stripslashes(trim($_POST['txt1stNumber'])); $_2ndNumber = stripslashes(trim($_POST['txt2ndNumber']));
if (!is_numeric($_1stNumber) || !is_numeric($_2ndNumber)) { exit("<font color=#FF0000>Enter a number in the textbox</font>"); }
if (isset($_POST['rad1'])){ $rad1 = $_POST['rad1']; switch($rad1){ case "Add": $result = $_1stNumber + $_2ndNumber; break; case "Subtract": $result = $_1stNumber - $_2ndNumber; break; case "Multiply": $result = $_1stNumber * $_2ndNumber; break; case "Divide": $result = $_1stNumber / $_2ndNumber; break; } echo("<font color=#FF0000>$result</font>"); } else { exit("<font color=#FF0000>Select an operation</font>"); } } } ?> However I feel this is not the right way, this is just setting the starting value of the 2 variables $_1stNumber and $_2ndNumber to 0. And clicking on the reset button does not help in changing this value back to 0. Is there a way to check when the reset button is pressed so that I can reset the value of the result there? On a general question, is there anyway to handle button clicks other than using javascript because not everyone would have javascript enabled.
Reply
Similar Topics
Keywords : submit, restrictions, bypass, localhost
- Restrictions On Php & Forms?
$_GET/POST/REQUEST? (2)
Stop Double Post/submit
(10) Hi Whats the best way of stopping a double post from occurring? Re-directs? I was hoping that
there was another way because I am using smarty and with the current setup of my site implementing
redirects would be a right pain. Thanks for any help. ....
Getting Information From Submit Button
I need to grab information from a submit button. (1) I basically have an array that uses characters as keys and has an integer value to it. I have a
loop that outputs the list in that array. For each entry I want a a button to remove it. The button
will submit the form to another page which contains the script to be able to remove it from the
array. The infomation for the keys name is used for the buttons name value. Im not sure how you go
about grabbing this information once on the submitted page. heres my code CODE
echo("<form name='jobs' action='remove_job.php'>"); foreach....
<?php ?> Get Search Results To Your Localhost
Simple way to get 100 results without allowing cookies (2) Ultimately simple script that allows you getting google search results to your localhost. This is
the first part of the Crawler script i'm developing, and if you are interested in developing the
script with me, IM me @icq328866661@msn/evil_matak/ a \hotmail-com. Here's the form
part... QUOTE search.php QUOTE if (@$_POST ==""){ echo "What are you
doing?"; } else{ $query =
file_get_contents("http://www.google.com/search?q=".urlencode($_POST
)."&num=100&hl=en&ie=UTF-8&filter=2"); //needs to be added with more queries ....
Form Dosn't Submit In Opera
Works in IE and FireFox (1) My form dosn't submit, it works in IE and FF, its validated HTML 4.01 and all, Code:
/**********************************/ /** Post Topic :: EvilBoard **/
/**********************************/ /* Session Start */ session_start(); /* Start Submit Script */
if ( isset($_POST )) { header("Refresh: 0; redirect.php?posttopic");
define("RELOADED","YES"); } /* End Submit Script */ /* Include File::Header.php */
include("include/header.php"); echo " "; /* If Script can't find SESSION user_name */ if (
!$_SESSION ) { /* Echo :: Forbiden */ echo '....
Request: Php Submit Script With Save In Host
Get Picture form url (13) Hi all i want make one form whit php , when users submit there urls , then my site give one picture
form url and save on my host ! how can do that ? thanks....
$_post Without A Form
Trying to post a value with using a submit button (9) Hey me again. Looks like im having trouble with php. THis time the solution should be easy but i
just cant find it anywhere. I am trying to post a variable but i do not want to use a form because i
hate the look of submit buttons for what i am using it for. I do not want to use javascript because
of compatibility issues. If you know how to make a submit button look like a nomal text link that
would work if you know how to send post data through an href that also would work. Or if you just
know how to do it another way im open to suggestions. Thanks for all of your help....
Looking for submit, restrictions, bypass, localhost
|
|
Searching Video's for submit, restrictions, bypass, localhost
|
advertisement
|
|