Assignment operator does not work

Discussion in 'Javascript' started by design@benhubbell.com, Nov 20, 2005.

  1. Guest

    Greetings,

    I have a situation in which an assignment operator isn't working. I'm
    trying to reset the value of "document.forms.f.shipping.value" to 0.
    I want this new value to be transmitted when the form is submitted.
    Unfortunately, the form's shipping element's value remains set at
    "19.00."

    I have looked for an answer in "JavaScript: the Definitive Guide 4th
    edition" and in the clj FAQ. I also reviewd clj posts dating back to
    November 14.

    <script language="JavaScript" type="text/javascript">

    function test(f){
    alert('Function call successful');
    document.forms.f.shipping.value = 0;
    document.forms.f.submit();
    return false;
    }

    </script>

    <form name="ufs" onsubmit="return test(ufs);" target="paypal"
    action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
    />
    <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/
    x-click-but22.gif" name="submit" />
    <input type="hidden" name="shipping" value="19.00" />
    </form>

    --
    Thank you to all who can help,
    Mountain Man
    , Nov 20, 2005
    #1
    1. Advertising

  2. Matt Kruse Guest

    wrote:
    > <form name="ufs" onsubmit="return test(ufs);" target="paypal"
    > action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
    > />


    First, you're passing 'ufs' without quotes, which is trying to pass a
    variable named 'ufs'. You should quote 'ufs'.
    A better approach, though, is to pass a form reference:
    <form name="ufs" onsubmit="return test(this);" ...>

    Then do:

    function test(f){
    alert('Function call successful');
    f.shipping.value = 0;
    return true;
    }

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Nov 20, 2005
    #2
    1. Advertising

  3. wrote:

    > I have a situation in which an assignment operator isn't working. I'm
    > trying to reset the value of "document.forms.f.shipping.value" to 0.
    > I want this new value to be transmitted when the form is submitted.
    > Unfortunately, the form's shipping element's value remains set at
    > "19.00."


    And there is most certainly an error message that indicates why.
    I am quite sure the FAQ mentions how to retrieve it.

    > I have looked for an answer in "JavaScript: the Definitive Guide 4th
    > edition" and in the clj FAQ. I also reviewd clj posts dating back to
    > November 14.


    With me, it is November 20 today. Yours does not strike me as
    being an exhaustive search at all. It's not even a week since.

    > <script language="JavaScript" type="text/javascript">
    >
    > function test(f){
    > alert('Function call successful');
    > document.forms.f.shipping.value = 0;
    > document.forms.f.submit();
    > return false;
    > }


    You should have continued the previous thread. At least the above
    script code looks like something I have proposed shortly before.

    > </script>
    >
    > <form name="ufs" onsubmit="return test(ufs);" target="paypal"
    > action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
    > />
    > <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/
    > x-click-but22.gif" name="submit" />
    > <input type="hidden" name="shipping" value="19.00" />
    > </form>


    1. This is most certainly neither (part of) Valid HTML nor XHTML.
    Make it Valid HTML 4.01 and remove `language="JavaScript"'.

    <http://validator.w3.org/>

    2. Your form (element) is named `ufs', not `f'. And the reference
    to `ufs' in the intrinsic event handler attribute value is probably
    `undefined' as not all UAs make named or IDed elements available
    as properties of the Global Object. Pass `this' instead and omit
    `document.forms.' in your method.

    3. You must not name any form control "submit", otherwise you
    overwrote the HTMLFormElement object's submit() method.

    4. You should not force new windows or tabs upon users, so you better
    remove the `target' attribute.


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 20, 2005
    #3
  4. Matt Kruse wrote:

    > wrote:
    >> <form name="ufs" onsubmit="return test(ufs);" target="paypal"
    >> action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
    >> />

    >
    > First, you're passing 'ufs' without quotes, which is trying to pass a
    > variable named 'ufs'. You should quote 'ufs'.


    That would accomplish nothing as he used the argument value like a
    reference.


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 20, 2005
    #4
  5. RobG Guest

    wrote:
    > Greetings,
    >
    > I have a situation in which an assignment operator isn't working. I'm
    > trying to reset the value of "document.forms.f.shipping.value" to 0.
    > I want this new value to be transmitted when the form is submitted.
    > Unfortunately, the form's shipping element's value remains set at
    > "19.00."
    >
    > I have looked for an answer in "JavaScript: the Definitive Guide 4th
    > edition" and in the clj FAQ. I also reviewd clj posts dating back to
    > November 14.
    >
    > <script language="JavaScript" type="text/javascript">
    >
    > function test(f){
    > alert('Function call successful');
    > document.forms.f.shipping.value = 0;


    When using dot notation, the values are treated literally. This is
    looking for a form named 'f'. If you want f to be evaluated to get the
    value you assigned to it (you attempt to pass the string 'ufs'), then:

    document.forms[f].shipping.value = 0;

    > document.forms.f.submit();
    > return false;
    > }
    >
    > </script>
    >
    > <form name="ufs" onsubmit="return test(ufs);" target="paypal"


    Here you are trying to pass a string[1], so do so:

    <form name="ufs" onsubmit="return test('ufs');" target="paypal"


    But anyway, why not pass a reference to the form using 'this'?

    <form name="ufs" onsubmit="return test(this);" target="paypal"


    Now in your function you can do:

    f.shipping.value = 0;


    since f is now a reference to the form.

    [...]

    1. I think (without testing) that IE will actually pass a reference to
    the form, since it adds names and IDs to the global object, so this may
    have worked (but only in IE) without other modification:

    f.shipping.value = 0;


    --
    Rob
    RobG, Nov 20, 2005
    #5
  6. matty Guest

    Thomas 'PointedEars' Lahn wrote:
    > And there is most certainly an error message that indicates why.
    > I am quite sure the FAQ mentions how to retrieve it.


    LOL and yea "undefined object" helps.

    > With me, it is November 20 today. Yours does not strike me as
    > being an exhaustive search at all. It's not even a week since.


    Maybe because the poster used a web-based browser and after reading 20
    posts per page he figured out his answer was not there?

    > You should have continued the previous thread. At least the above
    > script code looks like something I have proposed shortly before.


    I totally agree. History helps. Your post is a follow up question to a
    previous post, and PointedEars gave you relevant information. Creating
    a new post "forgets" the history and that's a bad thing.
    matty, Nov 20, 2005
    #6
  7. matty Guest

    > Thank you to all who can help,

    To put it simple, use "this" instead of "ufs".

    Matty.
    matty, Nov 20, 2005
    #7
  8. matty wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> And there is most certainly an error message that indicates why.
    >> I am quite sure the FAQ mentions how to retrieve it.

    >
    > LOL and yea "undefined object" helps.


    I don't think that the FAQ restricts its recommendations to
    Internet Explorer because particularly the error messages of
    this user agent turn out to be extremely seldom helpful.

    >> With me, it is November 20 today. Yours does not strike me as
    >> being an exhaustive search at all. It's not even a week since.

    >
    > Maybe because the poster used a web-based browser and after reading 20
    > posts per page he figured out his answer was not there?


    A "Web-based browser" includes the opportunity to use Google Groups search.


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 20, 2005
    #8
  9. matty wrote:

    >> Thank you to all who can help,

    >
    > To put it simple, use "this" instead of "ufs".


    No, passing this(!) would break with the current function code.


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 20, 2005
    #9
  10. matty Guest

    Thomas 'PointedEars' Lahn wrote:
    > matty wrote:
    >
    > >> Thank you to all who can help,

    > >
    > > To put it simple, use "this" instead of "ufs".

    >
    > No, passing this(!) would break with the current function code.
    >
    >
    > PointedEars


    True, I should have extended my "simple" answer to include the current
    function code.
    matty, Nov 20, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    489
    Barry Schwarz
    Feb 8, 2005
  2. Tarun
    Replies:
    5
    Views:
    390
    Tarun
    Jul 14, 2005
  3. Replies:
    4
    Views:
    367
    Victor Bazarov
    Nov 9, 2005
  4. Chris
    Replies:
    34
    Views:
    1,515
  5. Hicham Mouline
    Replies:
    2
    Views:
    693
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page