a matter of logic?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Cox

    a matter of logic?!

    Hello,

    I am not getting the logic right here...

    I want to send to a php file the results of clicking some buttons but
    only when an email address has been added, so

    function checkBeforeSend (){

    if (not all questions answered) {
    alert("complete all questions");
    } else if (no email added) {
    alert ("add email address");
    } else {
    send();
    }

    }

    something wrong here - ideas please!

    Cheers

    Geoff



  • Evertjan.

    #2
    Re: a matter of logic?!

    Geoff Cox wrote on 24 mrt 2008 in comp.lang.javas cript:
    Hello,
    >
    I am not getting the logic right here...
    >
    I want to send to a php file the results of clicking some buttons but
    only when an email address has been added, so
    >
    function checkBeforeSend (){
    >
    if (not all questions answered) {
    alert("complete all questions");
    >} else if (no email added) {
    alert ("add email address");
    >} else {
    send();
    >}
    >
    >}
    >
    something wrong here - ideas please!
    Logically nothing is wrong.
    The {} are superfluous, or there are two missing.

    The last else "belongs to the last if, btw.
    Javascript has no elssif like vbscript,
    so write these keywords on seperate lines.
    Good logical indenting will show you:

    function checkBeforeSend (){
    if (! allQuestionsAns wered)
    alert("complete all questions")
    else
    if (! emailAdded)
    alert ("add email address")
    else
    send();
    };

    But if you want complete {} do it this way:

    function checkBeforeSend (){
    if (! allQuestionsAns wered) {
    alert("complete all questions");
    }
    else { // this { you missed
    if (! emailAdded) {
    alert ("add email address");
    }
    else {
    send();
    };
    }; // and this corresponding closing }
    };

    Another way is by exchangeing the else clauses for early return clauses,
    this is perhaps easier to do and to maintain:

    function checkBeforeSend (){
    if (! allQuestionsAns wered) {
    alert("complete all questions");
    return;
    };
    if (! emailAdded) {
    alert ("add email address");
    return;
    };
    send();
    };



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Geoff Cox

      #3
      Re: a matter of logic?!

      On 25 Mar 2008 00:11:06 GMT, "Evertjan."
      <exjxw.hannivoo rt@interxnl.net wrote:

      >But if you want complete {} do it this way:
      >
      >function checkBeforeSend (){
      if (! allQuestionsAns wered) {
      alert("complete all questions");
      }
      else { // this { you missed
      if (! emailAdded) {
      alert ("add email address");
      }
      else {
      send();
      };
      }; // and this corresponding closing }
      >};
      Thanks Evertjan,

      Ive used the above and it works fine if all the questions are answered
      and the email address is entered.

      But! If the questions are all answered but the email address is not
      entered I get the warning to add an email address but the send()
      results do not appear. (I'm using AJAX.Updater for this)

      To call the checkBeforeSend (() I use

      <button onclick = "checkBeforeSen d();">Click here to see if you were
      right!</button>

      I can get the results by clicking on this button once the email
      address has been added but I would like the data to be sent after
      entering the email address without needing to go back to the button.

      Can you see the answer without having all the code?!

      Cheers

      Geoff

      Comment

      • Richard Cornford

        #4
        Re: a matter of logic?!

        Geoff Cox wrote:
        <snip>
        Ive used the above and it works fine if all the questions
        are answered and the email address is entered.
        >
        But! If the questions are all answered but the email address
        is not entered I get the warning to add an email address but
        the send() results do not appear.
        Isn't that what you specified and programmed it to do?
        (I'm using AJAX.Updater for this)
        That means nothing without context. All you are saying here is that you
        are using a property accessor, nothing about what property of what
        object is being accessed, or how it is being used.
        To call the checkBeforeSend (() I use
        >
        <button onclick = "checkBeforeSen d();">Click here to see
        if you were right!</button>
        BUTTON elements without TYPE attributes are problematic because the
        official default TYPE is 'submit' but IE defaults the type to 'button',
        so you get inconsistent behaviour between browsers if you do not specify
        a TYPE.
        I can get the results by clicking on this button once
        the email address has been added but I would like the
        data to be sent after entering the email address without
        needing to go back to the button.
        That does not sound like a friendly UI (submitting an e-mail address
        before the user has a chance to notice and fix their typo).
        Can you see the answer without having all the code?!
        Can you see without light?

        Richard.


        Comment

        Working...