auto-submit problem

K

kkuniya

Situation :
- A form (method : POST, action : itself, onsubmit : alert 'Submit' )
- Got 2 submit button ( 'Save' , 'View')
- Got navigation 1|2|3|4

What I want to do :
- Once clicked on the navigation page, it will save the page without
need to click on 'Save' button. Currently, if i manually clicked on the
'Save' button; it will save the page.

What have I done :
- tried using js form.submit(); not submitted coz no alert comes out
- tried using js button.click(); submitted coz got the alert, but it
didnt trigger the function to Save the form.

The code :
<form method="POST" name="theform" action="this page"
onSubmit="alert('submitted')">
:
:
:
<a href="thispage&p=3" onClick="submitForm()">3</a>
<a href="thispage&p=4" onClick="submitForm()">4</a>
:
<input type="submit" name="mysave" value="Save Value">
<input type="submit" name="myview" value="View Mode">
:
</form>

<script>
function submitForm() {
var theobject = document.theform.mysave
theobject.click()
}
</script>

<?php
if ($_POST['mysave']) {
....tried echo something here also not printed anything...
}
?>

May anyone help me on this?..
 
E

Erwin Moller

Situation :
- A form (method : POST, action : itself, onsubmit : alert 'Submit' )
- Got 2 submit button ( 'Save' , 'View')
- Got navigation 1|2|3|4

What I want to do :
- Once clicked on the navigation page, it will save the page without
need to click on 'Save' button. Currently, if i manually clicked on the
'Save' button; it will save the page.

What have I done :
- tried using js form.submit(); not submitted coz no alert comes out
- tried using js button.click(); submitted coz got the alert, but it
didnt trigger the function to Save the form.

The code :
<form method="POST" name="theform" action="this page"
onSubmit="alert('submitted')">
:
:
:
<a href="thispage&p=3" onClick="submitForm()">3</a>
<a href="thispage&p=4" onClick="submitForm()">4</a>

This needs improvent, read on.
:
<input type="submit" name="mysave" value="Save Value">
<input type="submit" name="myview" value="View Mode">
:
</form>

<script>
function submitForm() {
var theobject = document.theform.mysave
theobject.click()
}
</script>

<?php
if ($_POST['mysave']) {
....tried echo something here also not printed anything...
}
?>

May anyone help me on this?..

Hi,

The problem with your setup is that you use a hyperlink, THAT REPLACES THE
WHOLE PAGE, and a FORM-action, THAT WILL REPLACE THE SAME PAGE.

I hightlighted the part that give you trouble. ;-)

So what you do need is:
1) A working form. (Which you made already)
2) A hyperlink that does something else than hyperlinking, namely:
a) remember the url
b) set it in a hidden var in the form
c) submits the form. (It won't follow the hyperlink)
4) The receiving scripts redirects to an url passed by the hidden var.

So solve your problem by making your hyperlink do nothing and let Javascript
set the URL as a hidden var in the form.
Then, when processing the form, check for the url, and redirect.

Here is an example/proof-of-concept, using PHP as serverside language:

Regards,
Erwin Moller


<?
// Received a form?
if (isset($_POST["formposted"])){
// process the form
$firstname = $_POST["firstname"];
// do your stuff here

// redirect?
if ($_POST["gotourl"] != "NA"){
// go there:
header("Location: ".$_POST["gotourl"]);
exit;
}
}
?>

<html>
<head>
<title>Test hyperlink</title>
</head>
<body>

Nav:
<a href="" onClick="submitAndGo('test1.php?p=1'); return false;">nav 1</a>
<br>
<a href="" onClick="submitAndGo('test1.php?p=2'); return false;">nav 2</a>
<br>
<a href="" onClick="submitAndGo('test1.php?p=3'); return false;">nav 3</a>
<br>

<hr>
<form action="test1.php" method="POST" name="myform">
<input type="hidden" name="formposted" value="y">
<input type="hidden" name="gotourl" value="NA">
firstname: <input type="text" name="firstname">
<br>
<input type="submit" name="mysave" value="Save Value">
<br>
<input type="submit" name="myview" value="View Mode">
</form>

<hr>

<hr>

<script type="text/javascript">
function submitAndGo(aURL){
document.forms["myform"].gotourl.value=aURL;
document.forms["myform"].submit();
}
</script>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top