473,414 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Problems submitting a form via Javascript

5
Hi guys,

I've got a very basic form, like so:

Expand|Select|Wrap|Line Numbers
  1.     <form method="post" name="shra" action="http://url.com">
  2.  
  3.         <input type="submit" name="Submit" value="Submit">
  4.  
  5.     </form>
Then a basic check for the POST in PHP, again it works fine:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     if(isset($_POST['Submit'])) {
  3.         echo "POST!"; die;
  4.     }
  5. ?>
Again, no problem. Hit the submit button and the magic happens.

Now, I'm trying to achieve a similar thing though JavaScript since I didn't want to use an actual button for another implementation:

Expand|Select|Wrap|Line Numbers
  1. <span onclick="document.shra.submit();">Submit</span>
And it doesn't submit, but it does seem to refresh the page.

Any suggestions please chaps?

Many thanks
Sep 1 '08 #1
7 1040
acoder
16,027 Expert Mod 8TB
Why can't you use a button?

From what you've posted, it should work. Can you post the rest of the relevant code.
Sep 1 '08 #2
blizeH
5
A button seems to have weird styling issues, basically the page is quite long, so I wanted to have an inline submit button at the very top of the page, and a span with javascript (and styled appropriately with a cursor etc) seemed to be the best way around this.

Just wondering, what additional code would you need? I think it does something at the moment, but it doesn't trigger the if statement in the PHP to say POST has been declared :(
Sep 2 '08 #3
blizeH
5
Just stripped it down to the bare bones:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     if(isset($_POST['Submit'])) {
  3.         echo "POST!";
  4.         die;
  5.     }
  6. ?>
  7.  
  8.     <form method="post" name="shra" action="http://url.com">
  9.  
  10.         <input type="submit" name="Submit" value="Submit">
  11.  
  12.         <span onclick="document.shra.submit();">Submit</span>
  13.  
  14.     </form>
Clicking on the input button submits fine, but clicking on the span doesn't :(
Sep 2 '08 #4
acoder
16,027 Expert Mod 8TB
Of course it's not going to work. It's a problem with your PHP code. It's testing for a posted submit button which is not set when using a JavaScript submit. So the submit is working, but you'll need to modify your server-side code.
Sep 2 '08 #5
blizeH
5
What a moron, thank you for the tip and sorry for wasting your time :(
Sep 2 '08 #6
blizeH
5
Apologies for another question, but is there a way to check for a POST in general?

I've just created a hidden field for now to check for, but there could be a better way?
Sep 2 '08 #7
acoder
16,027 Expert Mod 8TB
Well, you're going to post something, so you don't even need a hidden field for that purpose. Just check if one of the fields is defined in the $_POST array. By the way, this is more of a PHP question, so if you have problems with it, ask in the PHP forum.
Sep 2 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div...
1
by: David Bradbury | last post by:
Hi On my form, as soon as the user clicks my submit button a message pops up saying "Form processing" as the form submits. This is fine as long as the user only clicks the submit button once....
3
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to...
2
by: Karuna | last post by:
Hi am quite new to Javascript and CGI related stuff. I have following javascript code : ----------------------------------------------- <html> <head>
2
by: Greg T | last post by:
Hi, I have a rather long form that I don't want people submitting unless they are absolutely sure they are ready. I figured the easiest way to prevent an accidental form submission by way of...
5
by: Don | last post by:
I have a need to submit a form, but don't need the user to click on a button. How do I do this? Is there some way, using JavaScript, to setup a <form> tag to do this? Thanks, Don ----==...
5
by: Tyler Style | last post by:
Hullo - looking for a little advice here. I have a form on a page in one domain submitting to a cgi in another domain. Weirdly, on some Windows XP systems, a form on the page fails to submit/post...
4
by: Rick | last post by:
Hello, I'm having trouble with submitting my form when checking to see if data is present in the user-inputted fields. What I want to happen is for the user to input various pieces of data,...
18
by: NavinM | last post by:
I have a couple of forms that are misbehaving in FireFox, but work fine in IE. when i do submit( with submit button) a javascript function validates all of the fields entered, and stops the...
3
sunbin
by: sunbin | last post by:
Hi, I am having in a Trouble when working with dynamic checkboxes (i.e. checkboxes with the same name, e.g. <input type="checkbox" name = "check" value="dynamic integer value">) I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.