Solved

Javascript Submit

Posted on 2014-04-05
5
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Find out what you should include to make the best professional email signature for your organization.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses

622 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