🏠 » Software, Coding & Hosting » NewPast Chemical Software » Online PHP Software Example: Online Hypochlorite Dilution Calculator

Online PHP Software Example: Online Hypochlorite Dilution Calculator

    With online software; your software code is safe. you will not distribute it, it is easy to integrate with your site. simple to use
    It does not need installation to run. Here is an example of the Hypochlorite Dilution Calculator Source Code.

    About Online software

    • Online software is easy to use and does not need installation to run
    • Your software and your code will be safe since you will not distribute them to the final user
    • Your software will be easy to integrate with your site

    Introduction to Hypochlorite Dilution Calculator

    • We use sodium hypochlorite solution at home, swimming water disinfecting, poultry production plants & many more.
    • The most know trade name of it is Clorox.
    • Many small plants buy the concentrated solutions in bulk for diluting and packing.
    • This calculator is for providing online calculations of the diluting.

    Using Hypochlorite Dilution Calculator

    • Decide what concentration you want to produce example 4%w/w
    • Get the concentration of the stock solution you have.
    • Go to the online page for the calculation Online calculator
    • Enter your values then press calculates
    • the online calculator will use AJAX so the page will display the result without reloading it.
    • The webpage will send all the input data to a PHP script that will make the calculation.

    Point of Interest:

    This article discusses the following technique:

    • How to build simple online software that you can run on free hosting.
    • Simple implantation of Ajax
    • Testing your software on your PC

    Hosting requirements:

    • Free or paid hosting account
    • We need no database for this software
    • The hosting should support PHP.

    Why PHP?

    • It is the most server-side script available on the webserver even on free plans
    • We can run it on both Windows and Unix server

    Testing your code locally

    To test your code locally, you will need a web server with PHP support.

    List of some easy-to-use servers with PHP support by default.

    List of some easy-to-use servers you should add PHP support by additional software.

    How does this application work?

    • This application depends on a table of concentration in different units and the density of the sodium hypochlorite solution. It contains also required excess sodium hydroxide for each concentration.
    • To calculate the quantity of water needed to dilute 8% w/w solution to be 4% solution as an example.
    • We search in the table for the input and output chlorine weight percentage and the density.
    • If we do not found the concentration we will calculate it depending on the nearest concentration
    //This function will search for a concentration 
    //$var:  value to be searched
    //$field:unit of the value 
    function searchit($var, $field){
        global $hypochlorite_table;  
        //array contains the data table of chlorine concentrations 
        //and density and reqiured sodium hydroxide
        global $precision;           
        //precision for each field in the $hypochlorite_table array
        if ( is_numeric ( $var )) $var = (trim($var) == '')? 0 :$var; else $var = 0;
        for ($index = 0; $index < count($hypochlorite_table); $index++) {
            if($var == $hypochlorite_table[$index][$field]){
                return outputrow($index);
            }elseif($var < $hypochlorite_table[$index][$field]){
                if(!$index) return outputrow($index);
                $delta = ($var-$hypochlorite_table[$index-1][$field])
                return outputrow($index,$delta);
        return false;
    function outputfield($field,&$row,$index,$delta=0){
        global $hypochlorite_table,$precision;
            $row[$field]= round($hypochlorite_table[$index][$field], 
            + $delta*($hypochlorite_table[$index][$field]
            - $hypochlorite_table[$index-1][$field]) 
            , $precision[$field]);
    function outputrow($index,$delta=0){
        outputfield(ClWV     ,$row,$index,$delta);
        outputfield(d        ,$row,$index,$delta);
        outputfield(dChange  ,$row,$index,$delta);             
        outputfield(ClWW     ,$row,$index,$delta);
        outputfield(NaOClWV  ,$row,$index,$delta);
        outputfield(NaOClWW  ,$row,$index,$delta);
        outputfield(NaOH     ,$row,$index,$delta);
        outputfield(dNone    ,$row,$index,$delta);
        return $row;

    here is a part of the table

    Cl %W/VCl%Chlorometric
    NaOCl % W/VNaOCl%Required
    excess of
    % W/W
    Specific Gravity
    change when adding
    1% NaOH
    Specific Gravity
    With required
    excess of NaOH
    Specific Gravity
    Without NaOH

    Storing table data in the array:

    • There are 3 data fields and the other fields are calculated fields.
    • I store the data fields in the array
       $hypochlorite_table =array(
      array(00.0, 1.001, 0.012),
      array(01.0, 1.020, 0.064),
      array(01.5, 1.027, 0.053),
      array(02.0, 1.034, 0.050),
      array(02.1, 1.035, 0.049),
      array(02.5, 1.041, 0.044),
      array(02.6, 1.043, 0.041),
      </pre lang="php">
    • I define fields constants
      0=>'ClWV',       //Cl(%w/v): weight of chlorine in 100 ml of the solution
      1=>'d',          //Specific Gravity of Sodium Hypochlorite solution that 
                       //contains default required excess of Sodium Hydroxide
      2=>'dChange',    //Specific Gravity change when adding 1% NaOH
      3=>'ClWW',       //Cl%= ClWV /d
      4=>'ClDegrees',  //°Cl Chlorometric Degrees = = 3.2 * ClWV
      5=>'NaOClWV',    //NaOCl% w/v: weight of Sodium Hypochlorite in 100 ml of the solution 
      6=>'NaOClWW',    //NaOCl%: weight of Sodium Hypochlorite in 100 g of the solution  
      7=>'NaOH',       //NaOH%: Default required excess of Sodium Hydroxide for 
                       //this concentration of hypochlorite=0.25+ClWW*0.75/16
      8=>'dNone'       //Specific gravity of Sodium Hypochlorite solution that 
                       //does not contain any excess of Sodium Hydroxide    
      foreach ($fields as $key => $value) define($value,$key);
    • Then I store the calculated fields in the array for easy searching.
    • I wrote an Html page that will be the interface of the application

    Using AJAX to call PHP data

    Once the user fills the form and press calculate button, if will call AjaxRefresh function.

    This function will do the following:

    • Store all form data in string
    • Create XMLHttpRequest object
    • Create a function that will handle the AJAX response
    • Open XMLHttpRequest object and send the data
    function AjaxRefresh(){
    var q = 'ajax.php'  + '?id='        + 
    '&CIn='         + CIn.value         + 
    '&COut='        + COut.value        + 
    '&dIn='         + dIn.value         + 
    '&lang='        + lang.value        + 
    '&NaOHIn='      + NaOHIn.value      + 
    '&QOut='        + QOut.value        + 
    '&UnitIn='      + UnitIn.value      + 
    '&UnitInAr='    + UnitInAr.value    + 
    '&UnitOut='     + UnitOut.value     + 
    '&UnitOutAr='   + UnitOutAr.value   +
    '&UnitQOut='    + UnitQOut.value    +
    '&UnitQOutAr='  + UnitQOutAr.value  ;
    var xmlhttp=new XMLHttpRequest();
      if (xmlhttp.readyState===4 && xmlhttp.status===200)
        var pairs = xmlhttp.responseText.split('&');
           var pair = pairs[i].split('=');
           var element = document.getElementById(pair[0]);
                try{element.innerHTML = pair[1].trim();}catch(e){};
                try{element.value = pair[1].trim();}catch(e){};

    The PHP script will send the Html elements ids with their new values