Link to home
Start Free TrialLog in
Avatar of DeepTracker15
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
Avatar of Zyloch
Zyloch
Flag of United States of America image

The only way I see for it to post to both locations is with frames. (or another window)
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,action,target) {
   frm.target = target;
   frm.action = action;
   frm.submit();
}

//-->
</script>
</head>
<body>
<form name="myform" action="defaultlocation.php" target="" onsubmit="while(var i=0;i<submitArray.length;i++){var tar;if (i==submitArray.length-1){tar='';}else{tar='newWin'+i;} submitFormToTarget(this,submitArray[i],tar);})">
<input type="submit" value="Submit" />
</form>
</body>
</html>
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,action,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,submitArray[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>
Avatar of DeepTracker15
DeepTracker15

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="09876"

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="broadway"&zip="09876"/test.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.
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
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["myFormName"]);
-------------------

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.