4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 Problems with onClick in form
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
Thomas2
Junior WebHelper
Junior WebHelper


Joined: 22 Jul 2003
Posts: 9
Location: Leicester, UK

PostPosted: Thu Oct 16, 2003 8:58 am (14 years ago) Reply with QuoteBack to Top

I put an onClick event handler in the Input-tag of form which should trigger a javascript function if the form is submitted but nothing happens. I also tried using onSubmit in the Form-tag but it does not work either. Using onClick in the Form-tag or Textarea-tag runs the script when I put the cursor in the Textarea field which is not what I want however.
It all works OK if the function is triggered from outside the form (e.g. from a link).

The code (which is part of a chatroom script) is basically as follows

<HTML>
<HEAD>
<SCRIPT Language="Javascript" src="refresh.js" type="text/javascript"> </SCRIPT>
</HEAD>
<BODY>
<form method="post" action="input.php" >
<textarea name="txt" type="text" rows="5" cols="52" id="txt"></textarea>
<input type="submit" name="Submit" value=" Chat! " onClick="refr()">
</form>
</BODY>
</HTML>

where refresh.js is given by

function refr() {
parent.mainFrame.location.href="messages.php";
}

This should refresh the content in mainFrame but it doesn't (there are no javascript error messages of any kind). The function is actually called (I checked it by putting in a javascript alert) but it does not refresh the frame. As mentioned, it works perfectly OK if the javascript is called outside the form.

Any suggestions what might cause the problem ?
OfflineView User's ProfileFind all posts by Thomas2Send Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Thu Oct 16, 2003 1:29 pm (14 years ago) Reply with QuoteBack to Top

This is just a wild stab in the dark, as I'm no JavaScript expert, but try this:

onClick="refr();return false";
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Thomas2
Junior WebHelper
Junior WebHelper


Joined: 22 Jul 2003
Posts: 9
Location: Leicester, UK

PostPosted: Thu Oct 16, 2003 2:02 pm (14 years ago) Reply with QuoteBack to Top

Darren wrote:
This is just a wild stab in the dark, as I'm no JavaScript expert, but try this:

onClick="refr();return false";

I tried your suggestion and the script runs now as it should but instead the content of the form is not being submitted (without the 'return false' it is just the other way around). There seems to be some conflict between submitting the form and running the script.
OfflineView User's ProfileFind all posts by Thomas2Send Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Thu Oct 16, 2003 2:45 pm (14 years ago) Reply with QuoteBack to Top

See if adding this somewhere in your function helps
document.name_of_form.submit();

I'm getting this from a bit of JS that validates a form. In this case if any of the fields are empty it throws up an alert message else it runs the line I pasted above. Might help. really don't know Wink
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Thomas2
Junior WebHelper
Junior WebHelper


Joined: 22 Jul 2003
Posts: 9
Location: Leicester, UK

PostPosted: Thu Oct 16, 2003 5:14 pm (14 years ago) Reply with QuoteBack to Top

It occurred to me now that the problem with the code as given above is that the frame refreshes before the content of the form is sent. I tried therefore now to submit the form from the javascript by putting the line you suggested in a function subm(), calling subm() from the input-tag with onClick="subm()" and then calling the refresh function from subm() with setTimeout('refr()',1000). Unfortunately this does not work either as apparenly refr() is not called (I checked this by inserting a javascript alert in refr()). It seems that submitting the form stops the scripts in either way.
(actually this solution submits the content of the form twice, once from the HTML and once from the script; adding the 'return false' results only in one submission but still does not result in the refresh being executed).

P.S.: I have posted the problem already in forums.34sp.com and also in forums.webdeveloper.com without getting any closer to the solution
OfflineView User's ProfileFind all posts by Thomas2Send Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Fri Oct 17, 2003 8:58 am (14 years ago) Reply with QuoteBack to Top

You say this for a chatroom, maybe it would be useful to look at some other chatroom scripts to see how they do what you are trying to do.

Hopefully someone with proper knowledge of JavaScript will visit the forums soon. Let us know if you find a solution elsewhere.
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Thomas2
Junior WebHelper
Junior WebHelper


Joined: 22 Jul 2003
Posts: 9
Location: Leicester, UK

PostPosted: Fri Oct 17, 2003 9:18 am (14 years ago) Reply with QuoteBack to Top

The chatroom scripts I have checked out refresh the messages simply through a 'meta-refresh' tag in the HTML. However this refreshes the frame continuously at the specified interval as long as the page is loaded. In order to reduce the server load, I had the idea to refresh the messages only a couple of times after the user has submitted his text and then switch to a manual refresh mode (so the above code is actually not the complete thing I had in mind).
However I think it should be possible to run my script through an onLoad event handler as the form code above is reloaded anyway through the PHP script after submitting the text.
OfflineView User's ProfileFind all posts by Thomas2Send Personal Message
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.059978 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme