DeepTracker15
asked on
Submit Form data to two places
Hello,
I currently have a very simple html form that uses the post method to post data to a specific URL.
The post location specified with the "action" variable is "http://www.test1.com/friends.php"
I would now like to submit that same form data also to a second location, during the same submit process.
The url of the second location would be "http://secondlocation.com/family.php".
I want this to happen browser-side, i.e. using javascript.
I suspect I can use the "onsubmit" variable, and call a java script that would submit to this second location, before the form itself posts to the main location?
If so, how would such java script look like?
Also, can I call and execute one java script function from within another?
If so, could you tell me how?
Thank you,
Daniel
I currently have a very simple html form that uses the post method to post data to a specific URL.
The post location specified with the "action" variable is "http://www.test1.com/friends.php"
I would now like to submit that same form data also to a second location, during the same submit process.
The url of the second location would be "http://secondlocation.com/family.php".
I want this to happen browser-side, i.e. using javascript.
I suspect I can use the "onsubmit" variable, and call a java script that would submit to this second location, before the form itself posts to the main location?
If so, how would such java script look like?
Also, can I call and execute one java script function from within another?
If so, could you tell me how?
Thank you,
Daniel
A revised code. I spent awhile looking for the error when I realized it was because I used a while loop. Wow I must be rusty!
<html>
<head>
<script language="javascript" type="text/javascript">
<!--
var submitArray = new Array;
submitArray[0] = "thirdlocation.php";
submitArray[1] = "secondlocation.php";
submitArray[2] = "firstlocation.php";
//Have array reversing. The third one was just to show you it's reversing.
function submitFormToTarget(frm,act ion,target ) {
frm.target = target;
frm.action = action;
frm.submit();
}
function submitForms(frm) {
var tar;
for (var i=0;i<submitArray.length;i ++) {
if (i==submitArray.length-1) {
tar="";
} else {
tar="_newWin"+i;
}
submitFormToTarget(frm,sub mitArray[i ],tar);
}
}
//-->
</script>
</head>
<body>
<form name="myform" action="" target="" method="get" onsubmit="submitForms(this );return false;">
<input type="submit" value="Submit" />
</form>
</body>
</html>
<html>
<head>
<script language="javascript" type="text/javascript">
<!--
var submitArray = new Array;
submitArray[0] = "thirdlocation.php";
submitArray[1] = "secondlocation.php";
submitArray[2] = "firstlocation.php";
//Have array reversing. The third one was just to show you it's reversing.
function submitFormToTarget(frm,act
frm.target = target;
frm.action = action;
frm.submit();
}
function submitForms(frm) {
var tar;
for (var i=0;i<submitArray.length;i
if (i==submitArray.length-1) {
tar="";
} else {
tar="_newWin"+i;
}
submitFormToTarget(frm,sub
}
}
//-->
</script>
</head>
<body>
<form name="myform" action="" target="" method="get" onsubmit="submitForms(this
<input type="submit" value="Submit" />
</form>
</body>
</html>
ASKER
how about this:
can java script create a long string out of all the formvariables and their values, such as
email="abc@abc.com"&street ="broadway "&zip="098 76"
this way, could i then have javascript construct an imaginary file name to be used in an external javascript call, that would refernce the following URL:
http://www.test1.com/email="abc@abc.com"&street="broa dway"&zip= "09876"/te st.js
server-side i would just build a parser that can extract that data from the url, and i would be all set.
so the question is, can i use javascript to construct such a string, and then use that string as the file name in an external java script call?
thank you much.
can java script create a long string out of all the formvariables and their values, such as
email="abc@abc.com"&street
this way, could i then have javascript construct an imaginary file name to be used in an external javascript call, that would refernce the following URL:
http://www.test1.com/email="abc@abc.com"&street="broa
server-side i would just build a parser that can extract that data from the url, and i would be all set.
so the question is, can i use javascript to construct such a string, and then use that string as the file name in an external java script call?
thank you much.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks a lot.
one other question: is there a way to create a string of all the values of all the fields, without knowing the field names? the order does not matter...
one other question: is there a way to create a string of all the values of all the fields, without knowing the field names? the order does not matter...
Hmm. I don't know if this is foolproof, but it uses values and names:
---------------------
function getVars(frm) {
var q = "";
for (var i=0;i<frm.elements.length; i++) {
if (i != 0) {
q += "&";
}
q += frm.elements[i].name + "=" + frm.elements[i].value;
}
return(querystring);
}
querystring = getVars(document.forms["my FormName"] );
-------------------
Another practical way would just to use a PHP file. Then you can use something like:
<?php
$querystring = "";
foreach ($_GET as $key => $var) {
$querystring .= $key . "=" . $var;
}
?>
or something like that.
---------------------
function getVars(frm) {
var q = "";
for (var i=0;i<frm.elements.length;
if (i != 0) {
q += "&";
}
q += frm.elements[i].name + "=" + frm.elements[i].value;
}
return(querystring);
}
querystring = getVars(document.forms["my
-------------------
Another practical way would just to use a PHP file. Then you can use something like:
<?php
$querystring = "";
foreach ($_GET as $key => $var) {
$querystring .= $key . "=" . $var;
}
?>
or something like that.
A way to do it with new windows is follows:
<html>
<head>
<script language="javascript" type="text/javascript">
<!--
var submitarray = new Array;
submitArray[0] = "thirdlocation.php";
submitArray[1] = "secondlocation.php";
submitArray[2] = "firstlocation.php";
//Have array reversing. The third one was just to show you it's reversing.
function submitFormToTarget(frm,act
frm.target = target;
frm.action = action;
frm.submit();
}
//-->
</script>
</head>
<body>
<form name="myform" action="defaultlocation.ph
<input type="submit" value="Submit" />
</form>
</body>
</html>