Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Submit

Posted on 2014-04-05
5
Medium Priority
?
561 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 1200 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

Industry Leaders: 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!

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.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
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…

688 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