Solved

Javascript Submit

Posted on 2014-04-05
5
523 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now