submit form...created with php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce Duncan

    submit form...created with php

    I know this isn't a php group but I was wondering if my problem is with my
    javascript in my php code. I'm debugging someone else's code and I can't
    figure out why this form won't submit.

    This code doesn't do anything:
    <?PHP
    $var1 = "http://localhost/php/index.php";
    $varx = "bruce";
    echo "<form name='formx3' method='post'>< input type='text'><in put
    type=button value=go onclick=documen t.formx3.action ='".$var1."';
    document.formx3 .submit();></form>";
    ?>

    This code works, but I want to know why the above code doesn't.
    <?PHP
    $var1 = "http://localhost/php/index.php";
    $varx = "bruce";
    echo "<form name='formx2' action='".$var1 ."'><input type='text'><in put
    type=button value=go onclick=documen t.formx2.submit ();></form>";
    ?>

    It seems awful weird to even try to assign the forms action and then submit
    it all in the onclick event...are you allowed to do this in Javascript?

    Thanks in advance...
    -Bruce


  • kaeli

    #2
    Re: submit form...created with php

    In article <10bf0tpjtie657 6@corp.supernew s.com>,
    bruce~w~duncan@ ~hotmail.com enlightened us with...[color=blue]
    > I know this isn't a php group but I was wondering if my problem is with my
    > javascript in my php code. I'm debugging someone else's code and I can't
    > figure out why this form won't submit.
    >[/color]

    When you are debugging a server-side app and think it's the client-side
    script, your best bet is to view the html source of the rendered doc to
    see if you got what you thought you should get. I've cought many an
    eaten quote that way. ;)

    It isn't working because you are missing quotes that are required for
    compound onClick.
    [color=blue]
    > This code doesn't do anything:
    > <?PHP
    > $var1 = "http://localhost/php/index.php";
    > $varx = "bruce";
    > echo "<form name='formx3' method='post'>< input type='text'><in put
    > type=button value=go onclick=documen t.formx3.action ='".$var1."';
    > document.formx3 .submit();></form>";
    > ?>[/color]

    The onClick, rendered (view->source), should look like this.
    onclick="docume nt.formx3.actio n='someAction'; document.formx3 .submit();">

    Note the double quotes that you don't have because your PHP would eat
    them. I don't remember the exact way to escape double quotes in PHP, but
    assuming it is a backslash, it would look like this in PHP.

    echo "<form name='formx3' method='post'>< input type='text'><in put
    type=button value=go onclick=\"docum ent.formx3.acti on='".$var1."';
    document.formx3 .submit();\"></form>";
    [color=blue]
    >
    > It seems awful weird to even try to assign the forms action and then submit
    > it all in the onclick event...are you allowed to do this in Javascript?[/color]

    Yup.
    Most of us put compound actions in functions and call those, though, for
    ease of modification later.


    --
    --
    ~kaeli~
    You feel stuck with your debt if you can't budge it.



    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: submit form...created with php

      kaeli wrote:
      [color=blue]
      > Note the double quotes that you don't have because your PHP would eat
      > them. I don't remember the exact way to escape double quotes in PHP, but
      > assuming it is a backslash, it would look like this in PHP.
      >
      > echo "<form name='formx3' method='post'>< input type='text'><in put
      > type=button value=go onclick=\"docum ent.formx3.acti on='".$var1."';
      > document.formx3 .submit();\"></form>";[/color]

      Both from a J(ava)Script and a PHP hackers point of view, this is a
      Bad Thing. As for PHP: Double quotes enforce expansion of variable
      references, so if there are none within the string they are highly
      inefficient. And as for J(ava)Script, I don't see the necessity of
      using (here) error-prone client-side scripting at all. Consider
      this instead:

      <?php
      ...
      ?>
      <form action="<?php echo $var1; ?>" name="formx3" method="post">
      <input>
      <input
      type="submit"
      value="go">
      </form>
      <?php
      ...
      ?>


      PointedEars

      Comment

      • Hywel

        #4
        Re: submit form...created with php

        In article <10bf0tpjtie657 6@corp.supernew s.com>, Bruce Duncan says...[color=blue]
        > I know this isn't a php group but I was wondering if my problem is with my
        > javascript in my php code. I'm debugging someone else's code and I can't
        > figure out why this form won't submit.
        >
        > This code doesn't do anything:
        > <?PHP
        > $var1 = "http://localhost/php/index.php";
        > $varx = "bruce";
        > echo "<form name='formx3' method='post'>< input type='text'><in put
        > type=button value=go onclick=documen t.formx3.action ='".$var1."';
        > document.formx3 .submit();></form>";
        > ?>
        >
        > This code works, but I want to know why the above code doesn't.[/color]

        It's probably because by the time it gets to the browser it's
        incorrectly formed.

        Your code outputs this as the onclick:
        onclick = document.formx3 .action='someth ing'; document.formx3 .submit();

        It should be something like this:
        onclick = "document.formx 3.action='somet hing'; document.formx3 .submit()"

        --
        Hywel I do not eat quiche


        Comment

        • Dominique

          #5
          Re: submit form...created with php


          try as follows
          <?PHP
          $var1 = "http://localhost/php/index.php";
          $varx = "bruce";
          echo "<form name='formx3' method='post'>< input type='text'><in put
          type=button value=go document.formx3 .action='".$var 1."';
          onclick=documen t.formx3.submit ();></form>";
          ?>
          the action must be defined before onclick I think
          i have not tried myself
          bye
          --
          Ce message a ete poste via la plateforme Web club-Internet.fr
          This message has been posted by the Web platform club-Internet.fr


          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: submit form...created with php

            Dominique wrote:
            [color=blue]
            > try as follows
            > <?PHP
            > $var1 = "http://localhost/php/index.php";
            > $varx = "bruce";
            > echo "<form name='formx3' method='post'>< input type='text'><in put
            > type=button value=go document.formx3 .action='".$var 1."';
            > onclick=documen t.formx3.submit ();></form>";
            > ?>
            > the action must be defined before onclick I think[/color]

            Correct, a "form" element without a defined "action" attribute is invalid
            HTML. But AFAIS there is no client-side J(ava)Script required at all.


            PointedEars

            Comment

            • MJ

              #7
              Re: submit form...created with php

              > the action must be defined before onclick I think[color=blue]
              > i have not tried myself[/color]

              Actually, that's not true. I often define the action (based on conditions)
              after the onclick. However, usually I use a function like:

              <script>
              function Submit_form(){
              if
              (document.form. category.option s[document.form.c ategory.selecte dIndex].value
              == "option1"){
              document.form.a ction = "whatever1.php" ; // Define the action
              document.form.s ubmit(); // Submit the page
              return true;
              } else if
              (document.form. category.option s[document.form.c ategory.selecte dIndex].value
              == "option2"){
              document.formSe arch.action = "whatever2.php" ;
              document.formSe arch.submit(); // Submit the page
              return true;
              } // and so on...
              }
              </script>

              <input name="Search" type="button" id="Search" value="Search"
              onClick="return Submit_form();" >

              Works like a charm. I think the original poster's problem was improperly
              formated code (lack of proper quotes). His onclick code was:

              onclick=documen t.formx3.action ='".$var1."'; document.formx3 .submit();

              Should have been:

              onclick=\"docum ent.formx3.acti on='".$var1."'; document.formx3 .submit();\"

              the quotes are escaped for PHP.


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: submit form...created with php

                MJ wrote:
                [color=blue][color=green]
                >> the action must be defined before onclick I think
                >> i have not tried myself[/color]
                >
                > Actually, that's not true. I often define the action
                > (based on conditions) after the onclick. [...][/color]

                So you are creating invalid HTML and/or create forms that are useless for
                users without client-side J(ava)Script and will malfunction for some with
                client-side J(ava)Script (as the "action" is either not changed or not
                changed fast enough).


                PointedEars

                Comment

                • MJ

                  #9
                  Re: submit form...created with php

                  > So you are creating invalid HTML and/or create forms that are useless for[color=blue]
                  > users without client-side J(ava)Script and will malfunction for some with
                  > client-side J(ava)Script (as the "action" is either not changed or not
                  > changed fast enough).
                  >
                  >
                  > PointedEars[/color]

                  It's only invalid HTML under outdated W3C standards written 7 years ago.
                  All versions from the past four years of the most popular browsers (IE,
                  Netscape, Opera, Mozilla) support doing this. I have never heard of any
                  sort of "malfunctio n" on the client side. I know several extremely high
                  traffic web sites that use similar functions.

                  Who doesn't have client-side Javascript these days? I'm sure less than one
                  half of one percent of all users. If they have Javascript turned off,
                  that's their tough luck. If their systems don't support Javascript, they
                  need to update their computers. My sites aren't intended for people with
                  386's running Windows 3.1.


                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: submit form...created with php

                    "MJ" <no_spam@thank. you> writes:
                    [color=blue]
                    > Who doesn't have client-side Javascript these days?[/color]

                    Statistics differ. They usually put the number at, or just above, 10%
                    of users. I see now that TheCounter.com has dropped sharply to around
                    5% at the beginning of the year, suggesting they changed their method
                    of disvocering Javascript availability. We still don't know what that
                    method is. Statistics are ... well, statistics (as in "lies, damn
                    lies, and ..."). It's also all we have.
                    [color=blue]
                    > I'm sure less than one half of one percent of all users.[/color]

                    Based on intuition or any actual research?
                    [color=blue]
                    > If they have Javascript turned off, that's their tough luck.[/color]

                    Not if it's a commercial site. Then it's the competition's luck. Or
                    the ADA (or similar other-nationality legislation) litigation lawyers'
                    luck
                    [color=blue]
                    > If their systems don't support Javascript, they need to update their
                    > computers.[/color]

                    Their handheld computers or mobile phones might not be upgradeable.
                    They are also likely to be more current than any version of Internet
                    Explorer.
                    [color=blue]
                    > My sites aren't intended for people with 386's running Windows 3.1.[/color]

                    That's your decission. Not a decission I would expect from a competent
                    web developer, though, as it doesn't take that much extra work to make
                    pages *usable* without Javascript. After all, the most important part
                    of a page *is* the content.

                    /L
                    --
                    Lasse Reichstein Nielsen - lrn@hotpop.com
                    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    • Andrew Thompson

                      #11
                      Re: submit form...created with php

                      On Mon, 31 May 2004 14:37:33 +0200, Lasse Reichstein Nielsen wrote:
                      ....[color=blue]
                      > ...After all, the most important part
                      > of a page *is* the content.[/color]

                      I prefer to think of it like this..

                      There are three important things
                      to a web-site, they are
                      - quality of content
                      - content usability/accessibility
                      - volume of content
                      ( In no particular order )

                      --
                      Andrew Thompson
                      http://www.PhySci.org/ Open-source software suite
                      http://www.PhySci.org/codes/ Web & IT Help
                      http://www.1point1C.org/ Science & Technology

                      Comment

                      • MJ

                        #12
                        Re: submit form...created with php

                        [color=blue][color=green]
                        > > I'm sure less than one half of one percent of all users.[/color]
                        >
                        > Based on intuition or any actual research?[/color]

                        Actual research. Over 99% of users use Javascript compatible browsers. If
                        TheCounter.com is reporting 5%, more than 4% of that is people who have
                        Javascript turned off.
                        [color=blue]
                        >[color=green]
                        > > If they have Javascript turned off, that's their tough luck.[/color]
                        >
                        > Not if it's a commercial site. Then it's the competition's luck. Or
                        > the ADA (or similar other-nationality legislation) litigation lawyers'
                        > luck[/color]

                        At this time web sites are not required to conform to the ADA, although a
                        few people have brought court cases, nobody has won yet. Even if the ADA
                        did apply, it states that businesses only have to make a reasonable
                        accomidations. Redesigning a site to ADA standards would be too costly and
                        take too many man hours for many commercial sites.
                        [color=blue][color=green]
                        > > If their systems don't support Javascript, they need to update their
                        > > computers.[/color]
                        >
                        > Their handheld computers or mobile phones might not be upgradeable.
                        > They are also likely to be more current than any version of Internet
                        > Explorer.[/color]

                        Most sites that support mobile phones and handhelds design specific versions
                        of their sites for those devices.
                        [color=blue]
                        >[color=green]
                        > > My sites aren't intended for people with 386's running Windows 3.1.[/color]
                        >
                        > That's your decission. Not a decission I would expect from a competent
                        > web developer, though, as it doesn't take that much extra work to make
                        > pages *usable* without Javascript. After all, the most important part
                        > of a page *is* the content.[/color]

                        Judging by the majority of sites on the Net, competent web developers are
                        few and far between. The most important part is giving your visitors what
                        they want. Developing a stripped down versions of sites to please less than
                        1% of visitors is a waste of time and resources that could be put to better
                        use. Technology can't move forward if everyone is trying to support
                        outdated and obsolete systems and software. In almost any business this is
                        the case. The recording industry doesn't produce LPs anymore because they
                        have become outdated and obsolete, even though many people still have
                        turntables. Losing less than 1% of my visitors to a competitor's site
                        doesn't bother me, because I'm gaining exponentially more customers by
                        having a much better site than my competitors.


                        Comment

                        • MJ

                          #13
                          Re: submit form...created with php

                          [color=blue]
                          > At this time web sites are not required to conform to the ADA, although a
                          > few people have brought court cases, nobody has won yet. Even if the ADA
                          > did apply, it states that businesses only have to make a reasonable
                          > accomidations. Redesigning a site to ADA standards would be too costly[/color]
                          and[color=blue]
                          > take too many man hours for many commercial sites.
                          >[/color]

                          Correction: The ADA can apply to web sites, but reasonable accommodations
                          and spending thousands of dollars to tens of thousands of dollars to make a
                          site ADA compliant really isn't a reasonable accommodation, unless your
                          company is making a sizable chunk of profit every year.


                          Comment

                          • Yann-Erwan Perio

                            #14
                            Re: submit form...created with php

                            MJ wrote:
                            [color=blue][color=green][color=darkred]
                            >>>I'm sure less than one half of one percent of all users.[/color][/color][/color]

                            0,5% may still be huge for a successful website.
                            [color=blue]
                            > Developing a stripped down versions of sites to please less than
                            > 1% of visitors is a waste of time and resources that could be put to better
                            > use.[/color]

                            I agree with this, however this is not the point here, if I may explain.

                            There are probably more than 100 browsers available on the net, offering
                            different scripting capabilities. As a result, the approach to
                            programming is very particular and differs considerably from usual
                            programming, in a known environment. In fact, it's likely that any code
                            will fail on at least one platform (and of course not even mentioning
                            javascript-disabled ones).

                            Therefore, the general conception should be adapted, and it's being more
                            and more accepted that scripting should be made on a two-layers' model:
                            - first building a usable website, with simple HTML. This is what your
                            visitors will get if the scripts do not work (or if javascript is disabled),
                            - then *adding* the javascript, transforming the HTML and adding
                            functionalities - taking advantage of the unique javascript capability
                            to determine, at run-time, whether the script will be supported, and if
                            not, cleany degrading to the already existing HTML.

                            As you can see, this simple model also applies to the "old" user agents
                            you mention, since they'll be given the already usable HTML, which they
                            can interpret correctly.

                            You've previously given an example of setting a form action. Why don't
                            you use something like

                            <form action="default Action"
                            onsubmit="retur n setAppropriateA ction(this)">
                            <input type="submit">
                            </form>

                            which will work even if js is disabled (you just have to handle the form
                            server-side if the script fails, as you probably already do as it's
                            trivial for a user to trick a client-side script at run-time). Such a
                            construct doesn't introduce a dependency on javascript and works exactly
                            the same.
                            [color=blue]
                            > Losing less than 1% of my visitors to a competitor's site
                            > doesn't bother me, because I'm gaining exponentially more customers by
                            > having a much better site than my competitors.[/color]

                            That may be fair (although I doubt such things can be accurately
                            measured) but the point is that you certainly don't have to waste these
                            1% customers, it's actually a conception issue, you're (IMHO) applying a
                            wrong conception when using javascript, which makes you lose clients
                            while it'd be easy to not lose them.

                            Now there are of course situations in which you know your environments,
                            like intranets, in which case the general conception can be a bit
                            different that the one exposed above.


                            Regards,
                            Yep.

                            Comment

                            • Jim Ley

                              #15
                              Re: submit form...created with php

                              On Mon, 31 May 2004 07:46:44 -0700, "MJ" <no_spam@thank. you> wrote:
                              [color=blue][color=green]
                              >> Based on intuition or any actual research?[/color]
                              >
                              >Actual research. Over 99% of users use Javascript compatible browsers.[/color]

                              Rubbish, possibly the majority of browsers in the world are not script
                              enabled (the ones shipped with the majority of mobile phones) Of
                              course they're rarely used, especially on sites that TheCounter uses.
                              [color=blue]
                              > If
                              >TheCounter.c om is reporting 5%, more than 4% of that is people who have
                              >Javascript turned off.[/color]

                              Right... and they use statistically representative sample because?
                              [color=blue][color=green]
                              >> Not if it's a commercial site. Then it's the competition's luck. Or
                              >> the ADA (or similar other-nationality legislation) litigation lawyers'
                              >> luck[/color]
                              >
                              >At this time web sites are not required to conform to the ADA, although a
                              >few people have brought court cases, nobody has won yet. Even if the ADA
                              >did apply[/color]

                              Remember he said or similar other nationality.
                              [color=blue]
                              > Redesigning a site to ADA standards would be too costly and
                              >take too many man hours for many commercial sites.[/color]

                              Ooops! Unfortunately that defence didn't work in SOCOG-Maguire and
                              won't elsewhere, simply because it's patently not true, and there are
                              of lots of genuine experts capable of standing up in court and saying
                              it.
                              [color=blue]
                              >Judging by the majority of sites on the Net, competent web developers are
                              >few and far between. The most important part is giving your visitors what
                              >they want. Developing a stripped down versions of sites to please less than
                              >1% of visitors is a waste of time and resources that could be put to better
                              >use.[/color]

                              Have you ever read the group? Have you ever seen any mention of
                              creating stripped down websites as the solution other than as a
                              strawman?
                              [color=blue]
                              > The recording industry doesn't produce LPs anymore because they
                              >have become outdated and obsolete, even though many people still have
                              >turntables.[/color]

                              completely irrelevant, but last year the vinyl market was larger than
                              the year before...
                              [color=blue]
                              > Losing less than 1% of my visitors to a competitor's site
                              >doesn't bother me, because I'm gaining exponentially more customers by
                              >having a much better site than my competitors.[/color]

                              Except of course, he chose to employe competent techniques, and his
                              site not only works without javascript, but is also more visible to
                              google, and works for all his customers.

                              Jim.
                              --
                              comp.lang.javas cript FAQ - http://jibbering.com/faq/

                              Comment

                              Working...