Post data to server from javascript on client side

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

    Post data to server from javascript on client side

    Hello everyone,

    I have a few values and variables that I want to post to a server
    (without using a SUBMIT button). Is there a way to post data from
    within javascript - do sockets or connections have to be open for this
    to work?

    Any help, hints or advice is appreciated :-)

    Kindest Regards.

  • Erwin Moller

    #2
    Re: Post data to server from javascript on client side

    milkyway wrote:
    [color=blue]
    > Hello everyone,
    >
    > I have a few values and variables that I want to post to a server
    > (without using a SUBMIT button). Is there a way to post data from
    > within javascript - do sockets or connections have to be open for this
    > to work?
    >
    > Any help, hints or advice is appreciated :-)
    >
    > Kindest Regards.[/color]

    Hi,

    Yes, just call the submit() method of a form, like this:

    <form action="secretp osting.php" name="aForm">
    <input type="hidden" name="var1" value="hai">
    </form>

    <script type="text/javascript">
    document.forms["aForm"].submit();
    </script>

    do sockets or connections have to be open for this to work?
    Don't worry about sockets, this is all just plain http/javascript.
    But a TCPIP connection that is working comes in very handy when posting
    forms. :P


    Regards,
    Erwin Moller

    Comment

    • milkyway

      #3
      Re: Post data to server from javascript on client side

      Hello again everyone :-)

      Adding more clarification below ...

      Erwin Moller wrote:[color=blue]
      > milkyway wrote:
      >
      > Hi,
      >
      > Yes, just call the submit() method of a form, like this:
      >
      > <form action="secretp osting.php" name="aForm">[/color]
      [color=blue]
      > <input type="hidden" name="var1" value="hai">[/color]
      ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^

      the thing is that I do not have an <input ....> defined for every value
      I wish to send to the server. I remember (in the old days), the string
      sent to the server would sook something like:



      See how the '&'is used to separate the <value name / value>
      combinations? Something like:

      http://clothes.com/run_this?socks=blue&shirts=10& ...

      How would one generate the string to send to the server? Is there a
      better way to get data to the server?
      I am open for any suggestions.

      Kindest Regards.

      Comment

      • milkyway

        #4
        Re: Post data to server from javascript on client side

        Hello all again,

        I found some code that kind of looks similar to what I am trying to
        achieve - except here, there is the allowing of the posting of *one*
        variable. Is there a way to post many variables (
        the idea is that the post will go to server side processing and send
        back an HTML page).

        Regards,
        Milkyway

        <FORM ACTION="URLHere " ID="frmone" NAME="frmUser" METHOD="POST" >
        ....form stuff here
        </form>

        <SCRIPT LANGUAGE="JAVAS CRIPT">
        <!--

        var frmUser = document.getEle mentById("frmon e")
        frmUser.submit( frmUser);

        -->
        </script>

        or you could try:

        <SCRIPT LANGUAGE="JAVAS CRIPT">
        <!--

        var frmUser = document.frmUse r
        frmUser.submit( frmUser);

        -->
        </script>

        Comment

        • Grant Wagner

          #5
          Re: Post data to server from javascript on client side

          "milkyway" <d0mufasa@hotma il.com> wrote in message
          news:1106079749 .345938.211800@ c13g2000cwb.goo glegroups.com.. .[color=blue]
          > Hello again everyone :-)
          >
          > Adding more clarification below ...
          >
          > Erwin Moller wrote:[color=green]
          >> milkyway wrote:
          >>
          >> Hi,
          >>
          >> Yes, just call the submit() method of a form, like this:
          >>
          >> <form action="secretp osting.php" name="aForm">[/color]
          >[color=green]
          >> <input type="hidden" name="var1" value="hai">[/color]
          > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
          >
          > the thing is that I do not have an <input ....> defined for every
          > value
          > I wish to send to the server. I remember (in the old days), the string
          > sent to the server would sook something like:[/color]

          So create an <input> for every value you want to send to the server.
          [color=blue]
          > http://groupsbeta.google.com/group/c...reply_to=group
          >
          > See how the '&'is used to separate the <value name / value>
          > combinations? Something like:[/color]

          You'll get the exact same thing from:

          <form method="GET" action="your_fo rm_handler.jsp" >
          <!-- note method="GET" -->
          <input type="hidden" name="a" value="b">
          <input type="hidden" name="c" value="d">
          <input type="hidden" name="e" value="f">
          [color=blue]
          > http://clothes.com/run_this?socks=blue&shirts=10& ...
          >
          > How would one generate the string to send to the server? Is there a
          > better way to get data to the server?
          > I am open for any suggestions.[/color]

          Please explain what you are trying to do. If you have a link that
          displays a specific product for example, you do something like this _on_
          _the_ _server_:

          // read the database for product ids and names
          // pretend the data returned is in -dataset-
          for (var i = 0; i < dataset.rows_re turned; ++i) {
          print_to_client (
          "<a href=\"/products/look_at_product .jsp?" +
          "product_id =" +
          dataset.getRow( i).getColumn("p roduct_id") +
          "\">" +
          dataset.getRow( i).getColumn("p roduct_name") +
          "</a>"
          );
          }

          Then /products/look_at_product .jsp would do something like:

          var product_id = Request.value(" product_id");
          // run the following SQL safely:
          // "select * from products
          // where product_id=" + product_id
          // (Note: never never never just stick
          // untrusted data like -product_id-
          // obtained from the client and simply
          // use it to build SQL)

          // now display the stuff retrieved from
          // the database query, or display an
          // error if the product couldn't be
          // found or whatever

          So like I said, please explain what you are attempting to do. I think
          you don't need JavaScript at all (but without more details I can't tell
          for sure). If you are trying to display information about data you have
          on the server, write the links as shown, from the server.

          --
          Grant Wagner <gwagner@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq


          Comment

          Working...