• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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
0
DeepTracker15
Asked:
DeepTracker15
  • 4
  • 2
1 Solution
 
ZylochCommented:
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>
0
 
ZylochCommented:
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>
0
 
DeepTracker15Author Commented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZylochCommented:
I'm not quite too sure what you mean. You can of course use Javascript to build a long string of form variables, something like:

querystring = "email=" + document.forms["myFormName"].elements["emailInputName"] + "&street=" + document.forms["myFormName"].elements["emailInputName"];

etc.etc. that can be called in onsubmit of the form. Is this what you're asking about?
0
 
DeepTracker15Author Commented:
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...
0
 
ZylochCommented:
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now