Form won't submit when navigated too but will work when accessed directly?
I'm developing a mobile application using phonegap and trying to integrate a form using jquery to post data to an sql database but the form seems to acting in a strange way.
If I set the application to navigate directly to the form page upon startup of the application it works fine but if I try to navigate to the form page from an different initial page the form will refuse to work.
The problem seems to be with declaring the jquery script on the first initial page.
If anybody has any idea what's going on that would be great because I just can't figure it out!
HTML from the first page navigating to the form page
- <!DOCTYPE HTML>
- <html lang="en-US">
- <head>
- <title>jQuery form post</title>
- <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
-
- </head>
- <body>
- <div data-role="page" class="ui-responsive-panel" >
- <div data-role="header" >
- <h1>New Submission</h1>
- </div>
- <div data-role="content">
- <a href="comment_2.html" data-role="button" data-transition="slide" data-icon="arrow-r" data-iconpos="right">Submit new location</a>
- </div>
-
- </body>
- </html>
HTML from the form page... this works on it's own but doesn't when you navigate to it.
- <!DOCTYPE HTML>
- <html lang="en-US">
- <head>
- <title>jQuery form post</title>
- <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
- <script src="js/post.js"></script>
- </head>
- <body>
- <div data-role="page" class="ui-responsive-panel" >
- <div data-role="header" >
- <h1>New Submission</h1>
- </div>
- <div data-role="content">
- <div id="landmark-1" data-landmark-id="1">
- <form id="comment">
- <label for="email">
- <b>Email</b>
- <input type="email" id="email" name="email">
- </label>
- <label for="comment">
- <b>Comment</b>
- <textarea id="comment" name="comment" cols="30" rows="10"></textarea>
- </label>
-
- <input type="submit" value="Save">
- </form>
- </div>
- </div>
- </body>
- </html>
Javascript of form
- $(function(){
- $('#comment').submit(function(){
- var landmarkID = $(this).parent().attr('data-landmark-id');
- var postData = $(this).serialize();
- $.ajax({
- type: 'POST',
- data: postData+'&lid='+landmarkID,
- //change the url for your project
- url: 'save.php',
- success: function(data){
- console.log(data);
- alert('Your comment was successfully added');
- },
- error: function(){
- console.log(data);
- alert('There was an error adding your comment');
- }
- });
- return false;
- });
- });