Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple forms, on one page as a single function

    I have been going round in circles trying raw ajax and a number of other methods and think the best thing to do is use jquery or similar but I am a little stuck.

    I have a page that generates a table, each row of the table becomes a separate form. This is for a work roster style thing, that shows user statuses, such as at lunch, busy etc for a distributed team that I work in.

    I am trying to make the page a little cleaner and use AJAX for the form processing as well as bringing in the content and refreshing it at regular intervals without the need to refresh the whole page.

    Anyway the content would look like this
    Code:
    <table>
    <tr>
    <td><form id="user1form" name="user1form><select name="theaction"><option value="1">Available</option>
    <option value="2">At Lunch</option>
    </select>
    <input type="hidden" name="userid" id="userid" value="1"></td><td><input type="submit" name="submit" value="go"></td>
    </form>
    </tr>
    <tr>
    <td><form id="user2form" name="user2form><select name="theaction"><option value="1">Available</option>
    <option value="2">At Lunch</option>
    </select></td>
    <input type="hidden" name="userid" id="userid" value="2">
    </form>
    <td><input type="submit" name="submit" value="go"></td>
    </tr>
    </table>
    Thats a basic version of what is there, but gives you an idea. It is all generated from a database using PHP

    Now I want to use a single ajax function to process which ever form has been updated, update the database and then reload the data on the page.

    Looking at a basic JQuery example I would use something like

    Code:
    <script type="text/javascript">
    	$(document).ready(function(){
    		$("#myform").validate({
    			debug: false,
    			rules: {
    				name: "required",
    				email: {
    					required: true,
    					email: true
    				}
    			},
    			messages: {
    				name: "Please let us know who you are.",
    				email: "A valid email will help us get in touch with you.",
    			},
    			submitHandler: function(form) {
    				// do other stuff for a valid form
    				$.post('process.php', $("#myform").serialize(), function(data) {
    					$('#results').html(data);
    				});
    			}
    		});
    	});
    	</script>
    However I would need to wrap that in a function and create put a variable for myForm of the form name to represent user1form etc.
    Also likely I wouldn't need the validation stuff

    Is this even possible?

    Cheers
    Dan
    Last edited by Dannymh; 02-02-2012 at 05:52 AM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •