submit a form after authenticating another form - javascript

Here I'm having two HTML forms.
Login Form
<form name="login" method=post action="http://xxxx.com/auth.asp">
<input type=hidden id=UserName name="userid" value="xxxxx"></input>
<input type=hidden id=Password name="password" value="yyyyyy"></input>
</form>
Another form
<form name="action" method=post action="http://xxxxxx.com/user/xxxxx.asp">
<input type=hidden id=UserName name="xxxxxx" value="xxxxx"></input>
<input type=hidden id=Password name="yyyyyy" value="yyyyy"></input>
</form>
I can able to submit my 1st form by just calling
document.login.submit()
But after the above statement is performed, any code below it is not getting executed.
I have to execute the following code after the above mentioned code:
document.action.submit()

If you are submitting the form without ajax, the browser to perform a redirect or page refresh. This will cause the rest of the javascript to stop executing.

Related

Submit the another website submit button from website

I want to submit the another website submit button from my website using javascript, is it possible..? My Code are below:
<script type="text/javascript">
function formSubmit() {
document.getElementById("form1").submit();
}
</script>
<form name="form1" method="post" action="http://XXXXX/Login.aspx?ReturnUrl=%2fdefault.aspx?" id="form1" >
<input type="hidden" name="NewCorporateId" id="NewCorporateId" value="XX" />
<input type="hidden" name="UserName" id="UserName" value="XXXX" />
<input type="hidden" name="Password" id="Password" value="XXXX" />
</form>
<form name="form2" id="form2" method="post>
Click here
</form>
Specified URL opened fine in new tab and button not submitted in that
site. That site contain the one submit button and username and
password text filed.. I have username and password value.. So i try to
submit the button in referring site from my website.
Its possible and the post code is correct, but because you post on aspx page Login.aspx, aspx pages protect from sending data that is not come from the same site by adding a hash code of controls. Also there is the ViewState of the page, and other hidden parameters added by aspx - so your call will fail.
Contact with the one that handle the login.aspx page to give you some way to login there - not direct by the page.
related :
What is the purpose of __EVENTVALIDATION __VIEWSTATE in aspx?

Javascript - how to automatically post a form

I have a form that I would like to submit automatically on page load however it is submitting a blank form.
This is the manual code, when I click submit either yes or no is posted without a problem.
<form method="post">
<label>Do You Authorize <?php echo $this->escapeHtml($this->clientId) ?>?</label><br />
<input type="submit" name="authorized" value="yes">
<input type="submit" name="authorized" value="no">
</form>
This is the automated code:
<body onload="document.getElementById('myForm').submit();">
<form method="post" id="myForm">
<label>Do You Authorize <?php echo $this->escapeHtml($this->clientId) ?>?</label><br />
<input type="submit" name="authorized" value="yes">
</form>
The automated code is submitting perfectly, however when I dump the $_POST variable, it is empty.
Any ideas why?
Try changing your <input type="submit"> to a <input type="hidden">. It may be that because no one is actually clicking the submit button, the value isn't getting passed.

Input value from different form

I have two forms on my page...
<form method="post" action="">
<input type="text" name="name" id="name"/>
<input type="submit" name="form1_submit_pushed"/>
</form>
<form method="post" action="">
<input type="submit" name="form2_submit_pushed/>
</form>
On my php side I want to be able to know the value of the text input "name" when I push the submit button of the second form. Kind of like....
if(isset($_POST['form2_submit_pushed']))
{
echo $_POST['name']; //or something else?
}
The reason behind is that first form has a bunch of data that I don't want in the second form submission.
You could do something like this...this code uses jQuery:
<form id="form1" method="post" action="">
<input type="text" name="name" id="name"/>
<input type="submit" name="form1_submit_pushed"/>
<input type="hidden" name="form2_submit_pushed" id="form2_submit_pushed">
</form>
<form id="form2" method="post" action="">
<input type="submit" name="form2_submit_pushed"/>
</form>
<script>
$('#form2').submit(function(event) {
//prevent form2 from submitting and submit form1 instead...
event.preventDefault();
//before submitting, indicate that the "form2_submit_pushed" button was pushed
$('#form2_submit_pushed').val(true);
//submit form1
$('#form1').submit();
});
</script>
...but why you would want to I don't know. Why not make all the controls part of the same form? HTML is designed to send info from only one form (at a time) to the server...
UPDATE: Sorry, I didn't notice your line where you explain your reason for wanting to do this. If you want more explicit control over what gets sent to the server I recommend using AJAX to submit the form. Look at https://api.jquery.com/serializeArray/ and https://api.jquery.com/jQuery.ajax/
Correct me if I am wrong here, but I beleive normal HTML will only post the inputs from the form you are posting from. One option would be to have a hidden input on the second form which gets updated via javascript during the input's change event.
So, you could do something like this (I don't recommend inline javascript but it should get you in the right direction):
<form method="post" action="">
<input type="text" name="name" id="name" onchange="document.getElementById('hiddenname').value=this.value"/>
<input type="submit" name="form1_submit_pushed"/>
</form>
<form method="post" action="">
<input type="hidden" name="hiddenname" id="hiddenname"/>
<input type="submit" name="form2_submit_pushed/>
</form>
Then you just need to get it using
$_POST['hiddenname'];

Submitting form using button on enter

I have an html form that I want to only submit from a button located outside my form. I am using javascript to perform some verification and do not want the form to submit unless my javascript functions succeed. I found that if I have the button inside the form it will always submit regardless of the javascript, but if I have it outside the form when a user presses enter it simply submits the form. How can I force enter to perform the button javascript instead of submitting?
<form name="form1" action=<?$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"]?> method="post">
<input type="text" maxlength="5" size="5" name="frmZip" value="">
<input type="hidden" name="frmLat" value="200">
<input type="hidden" name="frmLng" value="200">
<input type="submit" disabled="disabled" style="display:none" />
</form>
<button type="button" id="GetCoordinates" onclick="doClick();">Find Stores</button>
EDIT:
Found my solution.
I changed from
</form>
<button type="button" id="GetCoordinates" onclick="doClick();">Find Stores</button>
to
<input type="button" name="frmSubmit" onclick="doClick();" value="Submit">
</form>
This prevented the button from submitting the form so I submitted it in my doClick() via javascript.
EDIT 2:
While this seemed to work for a time, it has stopped catching the enter keystroke. I updated my button to:
<input type="submit" name="frmSubmit" onclick="return doClick();" value="Find Stores">
And always returned false in doClick(). This allowed me to submit the form via javascript once everything had executed.
While this doesn't answer your direct question, you can actually keep the button and simply use your validation on the form submit:
<form onsubmit="return validateForm()">
Then, in your validateForm method, return true or false indicating whether or not the validation has passed.
However to answer your direct question, you can also use the same approach on the submit button which will prevent the form from being submitted.
Update
As pointed out in the comments, an unontrusive solution is often desirable so here's that:
document.getElementById('theForm').onsubmit = function() { return validateForm(); };
Your button inside the form will not submit the form on enter if you add preventDefault...
$("form").submit(function(e) {e.preventDefault();});

Auto Submitted form not carrying the values into Page which is mentioned in Action attribute of <form> tag

I tried to do auto form submission.It works fine and it redirected to the action page.But,it does not carry the values.Please help me to find the solution.
<form method="POST" action="SamplePage" name="myForm" id="formone">
<input type="hidden" name="AMT" value="10"/>
<input type="hidden" name="ID" id="cust"/>
<input type="submit" value="Submit" id="bu" name="i"/>
</form>
I used document.getElementById("formone").submit(); the for auto submission.
Try like this
document.forms["formone"].submit();
or you can try like
document.formone.submit();
Where your form name should be formone
And at your action page try to access the REQUEST values
print_r($_REQUEST);

Categories

Resources