Solved

Javascript Submit

Posted on 2014-04-05
5
531 Views
Last Modified: 2014-04-06
Hello,

Trying to submit from an hyperlink but its not working or redirecting to the action page.


<!--html code-->
<input hidden variables...>
<a href="javascript:doPost();" onClick="javascript:recordClickEvent();" target="_blank">New Page</a>
...


<script language="javascript">
function doPost() {
    document.forms[0].action = "http://site_that_accepts_post.com/redirectpage";
    document.forms[0].method = "post";
    document.forms[0].submit;
}
</script>

</html>


The above doPost method is being called from the hyperlink but the post redirect page is not opening the new widow.

Tried it in on click also but that opens a new tab with the address bar URL as "javascript:doPost()"

Is it possible to post form javascript and open the action page in a new tab/window?

Not sure if it matters but this is a sharepoint wsp solution page.
0
Comment
Question by:sansoftura
  • 3
  • 2
5 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39980358
<a href="javascript:doPost();"

This simply does nothing at all. You have to put the javascript code to post the form in the recordClickEvent();

And I would suggest to consider to use jQuery: it makes using javascript easier...
0
 
LVL 6

Author Comment

by:sansoftura
ID: 39980581
Tried the code below:

<a href="#" onClick="javascript:doPost();" target="_blank">New Page</a>

function doPost() {
    $("#my_form").attr("action","http://site_that_accepts_post.com/redirectpage");
    $("#my_form").attr("method", "post");
    $("#my_form").attr("target", "_blank");
    $("#my_form").submit();
}

This doesn't work either. It opens new window but the new window has the same url as the current one. Not the url in action;
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 300 total points
ID: 39980900
Okay, I've rewritten the code. Here you can see it in action. Obviously, my form leads to another page in my srver, but this should make you look at the right direction: eetest.delphicoding.com/sansoftura/form.html

Here the html page:

form.html
<!DOCTYPE html>
<html>
	<head>
		<title>sansoftura test</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<script src="../js/jquery.min.js"></script>
		<script>
			$(document).ready(function(){
				$('#submit').on('click', function(e){
					e.preventDefault();
					alert('submit');
					$("#my_form").attr("action","target.php");
					$("#my_form").attr("method", "post");
					$("#my_form").attr("target", "_blank");
					$("#my_form").submit();					
				});
			});//end jquery
		</script>
	</head>
	<body>
		<form id="my_form">
			<input type="text" name="text_field" value="" />
		</form>
		<a id="submit" href="#">Submit</a>
	</body>
</html>

Open in new window


And here the target page target.php:
<?php

error_reporting( E_ALL );
ini_set( 'display_errors', 'On' );
$txt = filter_input(INPUT_POST, 'text_field');
if ($txt)
{
	echo "That's $txt!";
}

Open in new window


Hope this makes sense for you.
Cheers
0
 
LVL 6

Author Closing Comment

by:sansoftura
ID: 39981331
Thanks for the effort!
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39981371
Glad to help you!
Cheers
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question