SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Show percent of total before insert?

    Hello

    I'm sorry if this is the wrong subforum I'm not sure that this is possible to do with PHP.

    Inside a form I have three textfields:

    Total
    Woman
    Men

    Is it possible to show the percent of woman / men when you fill in the textfields? That is before you submit the form.

    For example: you fill in "100" in total, "30" in woman and "30" is shown besides the textfield "woman".

    I have tried writing my own function, but I get a warning: "Division by zero"

    Anyone who can point me in the right direction for a solution?

    Regards
    Magnus

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you are trying to achieve this before submitting the form, this has to be done on the client side in Javascript and not in php.

    if you have to calculate the percent of women/men, "men" should have a non-zero value, otherwise you will get the divide by zero error. As a good practice, before dividing by any number, you should always check that the denominator is not zero.

    In your case, if the only inputs are total and women, you should first calculate

    men = total - women

    and then calculate the percent.

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Example using YAHOO user Interface library.

    Code Javascript:
    //YUI object shortcuts
    var $D = YAHOO.util.Dom;
    var $E = YAHOO.util.Event;
    var $ = $D.get;
     
    //Listen to women blur event
    function init() {
      $E.on(['total', 'women'], 'blur', setPercentage);
    }
     
    //Calculate women percentage
    function setPercentage(e) {
      var total = parseInt($('total').value);
      var women = parseInt($('women').value);
     
      if(total == '' || total == 0 || women == '') return; //Rough validation
     
      var percentage = (Math.round((women/total)*10000))/100;
      percentage = percentage + ' %';
      $('percentage').innerHTML = percentage;
    }
     
    $E.onDOMReady(init);

    Code HTML:
    <script type="text/javascript" src="http://yui.yahooapis.com/2.3.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
     
    <form method="post" action="my_script.php">
    <p>
    Total: <input type="text" name="total" id="total"><br>
    Women: <input type="text" name="women" id="women"> <span id="percentage"></span><br>
    Men: <input type="text" name="men" id="men">
    </p>
    </form>

    ideally the total would be derived automatically from men and women fields

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks both of you

    I know close to nothing about Javascript but I will try your script cranial-bore.

    Regards,
    Magnus

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    One thing, you mentioned a division by zero error. You got this when the total = 0, because you would be doing 0/0. To fix that, just add an if statement something like:
    if(total != 0)
    // Get the percent.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    ideally the total would be derived automatically from men and women fields
    Hello again – and sorry it took so long.

    I proberly explaind what I wanted bad (or I'm even worse in Javascript then I thought )

    I made this dummy (see screen shot) of how I want it to work. When you tab from field "woman" to field "men" the percent of woman should be displayed next to the field "woman" ("40" in this simple example).

    Is this at all possible?

    In the database I only save the numbers from the fields.

    Regards Magnus

    Screen shot:



Bookmarks

Posting Permissions

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