469,306 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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.

Jul 23 '05 #1
4 32499
milkyway wrote:
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.


Hi,

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

<form action="secretposting.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
Jul 23 '05 #2
Hello again everyone :-)

Adding more clarification below ...

Erwin Moller wrote:
milkyway wrote:

Hi,

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

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

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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:

http://groupsbeta.google.com/group/c...reply_to=group

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.

Jul 23 '05 #3
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="JAVASCRIPT">
<!--

var frmUser = document.getElementById("frmone")
frmUser.submit(frmUser);

-->
</script>

or you could try:

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--

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

-->
</script>

Jul 23 '05 #4
"milkyway" <d0******@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hello again everyone :-)

Adding more clarification below ...

Erwin Moller wrote:
milkyway wrote:

Hi,

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

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

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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:


So create an <input> for every value you want to send to the server.
http://groupsbeta.google.com/group/c...reply_to=group

See how the '&'is used to separate the <value name / value>
combinations? Something like:
You'll get the exact same thing from:

<form method="GET" action="your_form_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">
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.


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_returned; ++i) {
print_to_client(
"<a href=\"/products/look_at_product.jsp?" +
"product_id=" +
dataset.getRow(i).getColumn("product_id") +
"\">" +
dataset.getRow(i).getColumn("product_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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Darrell G | last post: by
3 posts views Thread by =?Utf-8?B?UGV0ZXIgSiwgU2Nhbmlh?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.