Results 1 to 10 of 10

Thread: PHP/Javascript help!

  1. #1
    Coder
    Join Date
    Apr 2009
    Location
    Cork
    Posts
    60
    Post Thanks / Like

    Default PHP/Javascript help!

    Hi lads

    Firstly, my knowledge of php & javascript is very limited. I need to remake this calculator for the clients microsite but cannot get access to the files on the server Contract Cleaning Ireland Contract cleaners Ireland Duct Cleaning Forecourt Cleaning Construction Cleaning Halcyon Hygiene

    Ive rebuilt the form by using the code & css copied from the broswer (view source).

    Thing is, how do i get it to calculate? There is Javascript embedded also but should there be an external php file on the server also that works out the calculations?

    They sent me this code by email, pretty sure theres something missing:

    $total = 0;
    $WaterUse = 0;
    $Kwatts = 0;
    $totalSaving = 0;
    $Co2 = 0;
    $WaterUse2 = 0;
    $Kwatts2 = 0;
    $Co22 = 0;

    $rates['Carlow']=2.73;
    $rates['Cavan']=2.51;
    $rates['Clare']=2.98;
    $rates['Cork']=2.3;
    $rates['Cork City']=2.3;
    $rates['Donegal']=2.31;
    $rates['Dublin City']=1.64;
    $rates['South Dublin']=1.56;
    $rates['Dun Laoghaire Rathdown']=2.34;
    $rates['Fingal']=1.94;
    $rates['Galway']=2.12;
    $rates['Galway City']=1.96;
    $rates['Kerry']=1.79;
    $rates['Kildare']=2.98;
    $rates['Kilkenny']=2.53;
    $rates['Laois']=2.45;
    $rates['Leitrim']=2.28;
    $rates['Limerick']=2.6;
    $rates['Limerick City']=1.99;
    $rates['Longford']=2.63;
    $rates['Louth']=1.95;
    $rates['Mayo']=2.36;
    $rates['Meath']=2.36;
    $rates['Monaghan']=2.11;
    $rates['Offaly']=2.02;
    $rates['Roscommon']=2.32;
    $rates['Sligo']=2.3;
    $rates['Tipperary North']=2.4;
    $rates['Tipperary South']=2.1;
    $rates['Waterford']=2.43;
    $rates['Waterford City']=2.32;
    $rates['Westmeath']=2.45;
    $rates['Wexford']=2.63;
    $rates['Wicklow']=3.04;

    if (isset($_POST['submit'])) {
    if ($ccprices == "") {
    $feedback="Please select your local county council.";
    }else if($_POST['email']==""){
    $feedback="Please enter an email address.";
    }else if($_POST['urinals']==""){
    $feedback="Please enter an value in the urinals text box.";
    }else{
    $ccprices = $rates[$_POST['ccprices']];

    $urinals = $_POST['urinals'];
    $wateruseperurinal = 175000;
    $TotalWaterUsed = ($wateruseperurinal*$urinals);
    $TotalWaterCostperCC = (($TotalWaterUsed/1000)*$ccprices);
    $TotalKwattsUsed = (($TotalWaterUsed/1000)*9.04);
    $TotalCo2 = (($TotalWaterUsed/1000)*0.04);

    $totalCost = (($wateruseperurinal/1000)*$urinals*$ccprices);

    $wateruseperurinalSaved = 145800;
    $wateruseperurinalNew = 29200;
    $TotalWaterSaved = ($wateruseperurinalSaved*$urinals);

    $TotalKwattsSaved = $TotalKwattsUsed * ($wateruseperurinalSaved / $wateruseperurinal);
    $TotalCo2Saved = $TotalCo2 * ($wateruseperurinalSaved / $wateruseperurinal);

    $newCost = ((($wateruseperurinalNew/1000)*$urinals*$ccprices)+($urinals*140));
    $totalsaved=$totalCost-$newCost;

    $q="INSERT INTO calculator (email, location, rate, urinals) VALUES ('".addslashes($_POST['email'])."', '".addslashes($_POST['ccprices'])."', ".addslashes($ccprices).", ".addslashes($_POST['urinals']).")";


    Any help with this would be great
    cheers

  2. #2
    Wannabe Geek php.allstar's Avatar
    Join Date
    Apr 2009
    Location
    Monamolin, Gorey, Co. Wexford
    Posts
    405
    Post Thanks / Like

    Default

    Hi,

    I'm not sure if I understand you correctly, but I've looked at the code and I don't see any problems off hand, it should calculate no problem.

    Javascript is not needed.

    Here's how it works:

    1. A user submits the form
    2. The form submits to itself (<form action=""></form>)
    3. The page reloads
    4. PHP checks to see if the form has been submitted (if (isset($_POST['submit'])) {...)
    5. If the form has been submitted, some pretty standard vvalidation is done on the user input to check for errors
    6. If there were no errors, the calculation is performed based on the values the user entered in the form along with some hardcoded factors
    7. The results are saved to a database.

    I don't see anything here for displaying the results. To display the results, you would have to echo the variables from the calculation (<?php echo $TotalWaterUsed; ?>) in the appropriate location in the source code.

    Sorry for being so vague about the display but If I saw the full php source code I could pinpoint the problem for you.

    By the way, there is a security concern in your source code. On your mysql query you are using addslashes on $_POST variables. This is a big no-no. You should be using mysql_real_escape_string(), its slightly more secure.

    addslashes can be tricked by injecting the hexadecimal character 0xbf27, addslashes coverts this to 0xbf5c27 which is a legal muti byte character followed by a single quote... now I understand that this may be over your head, don't be worried, just replace "addslashes" with "mysql_real_escape_string" provided your database connection is open or persistent.

  3. #3
    Coder
    Join Date
    Apr 2009
    Location
    Cork
    Posts
    60
    Post Thanks / Like

    Default

    Thanks for your reply allstar. Like I said my knowledge of php is very limited so alot of what you said is over my head

    You said all the code looks fine but where do I place this code? I am in the process of remaking the form from scratch which is the easy part. Then I need to work out how to calculate using the details above.

    You said you would like to see the full php source code, where would I find this? Should it not be in the above code I was sent by client?

    Thanks again for your help

  4. #4
    Wannabe Geek php.allstar's Avatar
    Join Date
    Apr 2009
    Location
    Monamolin, Gorey, Co. Wexford
    Posts
    405
    Post Thanks / Like

    Default

    Yeah, sorry for getting technical, but there will be others reading this post too so its nice to share the knowledge anyway!

    Ok, I'll try to break it down.


    1. Go to the url that contains the form in your browser
    2. Copy the source code
    3. Save it as whatever.php
    4. Above your opening <html> tag insert the code you received via email within <?php and ?> tags.
    5. there will be some variables available once the form is posted (like $TotalWaterSaved, $TotalKwattsSaved, $TotalCo2Saved etc... )
    6. You will have to edit the source code of the file to echo your php variables (see the attached screenshot) the variable names are pretty self explanatory so you should be able to figure out where to echo them!
    7. I presume you don't have a testing server so comment out the mysql_query line and upload the file to some obscure location on your web server (like http:// www. cleaning .ie /calculator-test.php)
    8. Go to the url in your browser and test
    9. Works now?!

    Let us know how you get on.

    Here's a nice little place to pickup the basics of php PHP Tutorial - Introduction
    Attached Images Attached Images
    Last edited by php.allstar; 16-07-2009 at 01:12 PM. Reason: Forgot to add screenshot

  5. #5
    Coder
    Join Date
    Apr 2009
    Location
    Cork
    Posts
    60
    Post Thanks / Like

    Default

    Cheers dude, your a legend. Ill give that a shot.

    Just couple of things:

    1. So I save whatever page Im putting the calculator on as a php file & the rest as .html?
    2. Is the whole thing self contained then in that php file? ie I dont need an external php file to run the script?
    3. I understand what you mean by echoing variables but how come when I look at the source code of the site (through the browser as I have no access to files) there is no php code or variables in it?


    Sorry if im being thick!

    thanks for the help

  6. #6
    Wannabe Geek php.allstar's Avatar
    Join Date
    Apr 2009
    Location
    Monamolin, Gorey, Co. Wexford
    Posts
    405
    Post Thanks / Like

    Default

    LOL!

    I almost fell off my chair when I read that!

    1. The whole thing has to be saved as a php file.
    2. It's just one file, no external php scripts needed.
    3. PHP is a server side scripting language. Your web server will read the php code and then parse it (covert and display) as html, but it will still have a .php extension

    Sorry but that was funny! Why are you taking on php work if you don't know it!

  7. #7
    Coder
    Join Date
    Apr 2009
    Location
    Cork
    Posts
    60
    Post Thanks / Like

    Default

    I knew that, just double checking!

    Hey, its all learning. I like a challenge.

    Thanks for the help.

  8. #8
    Wannabe Geek php.allstar's Avatar
    Join Date
    Apr 2009
    Location
    Monamolin, Gorey, Co. Wexford
    Posts
    405
    Post Thanks / Like

    Default

    Well fair play, keep it up, thats how I started off too, just said what the hell and took a dive in the deep end.

    Keep it up, you'll get there eventually.

    Here's a nice series of articles to help you learn php

    Also make sure to set up your own development server on your local machine using something like Xampp

    Then you can test your php files on your local computer (open a browser and type http://localhost) without having to upload to a server everytime.

  9. #9
    Coder
    Join Date
    Apr 2009
    Location
    Cork
    Posts
    60
    Post Thanks / Like

    Default

    Cheers man, yeah im using MAMP (on a mac)

    Just one more dumb question:

    You said to comment out 'mysql_query'. Where do I find this? its not in the code they emailed.

    Edit: sorry for being annoying but I did those steps & its showing up blank in the browser?

    Thanks again man
    Last edited by EggDesign; 16-07-2009 at 02:35 PM.

  10. #10
    Wannabe Geek php.allstar's Avatar
    Join Date
    Apr 2009
    Location
    Monamolin, Gorey, Co. Wexford
    Posts
    405
    Post Thanks / Like

    Default

    Hi,

    It was
    $q="INSERT INTO calculator (email, location, rate, urinals) VALUES ('".addslashes($_POST['email'])."', '".addslashes($_POST['ccprices'])."', ".addslashes($ccprices).", ".addslashes($_POST['urinals']).")";
    But now that I look at it, thats just building the query string. No need to comment it out.

    Just pm or email me with the full source code, I'll tell you why its coming up blank

Similar Threads

  1. JavaScript and CSS
    By johnrochie in forum Coding Help
    Replies: 2
    Last Post: 15-06-2009, 06:39 PM
  2. Need Javascript Help
    By javascript_enthusiast in forum Coding Help
    Replies: 3
    Last Post: 16-11-2008, 10:31 PM
  3. Javascript Help
    By garycocs in forum Coding Help
    Replies: 9
    Last Post: 30-07-2008, 12:39 PM
  4. javascript
    By 7aken in forum Coding Help
    Replies: 1
    Last Post: 03-04-2007, 03:50 PM
  5. javascript help
    By 7aken in forum Coding Help
    Replies: 2
    Last Post: 20-02-2007, 05:09 AM

Visitors found this page by searching for:

No visitors have come to this page from a search engine, yet!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •