sonicthehedgehog Posted September 24, 2015 Share Posted September 24, 2015 (edited) I'm trying to get a form the submit and get the results from a database without the page reloading. I've got a form and want the results to update whenever somebody clicks on a checkbox or button. The form works and gets the right results but only if the page loads. This is the code I'm using: $('.filter_link').on('click', function(e) { e.preventDefault // prevent form submission $.ajax({ url:'/results/index.php', data: $(this).serialize(), // serialize form data and attach to request type:'POST', success:function(results) { $(".result").html(results); } });}); I don't get any errors at all and nothing happens. The form also has the /results/index.php as the action too and filter_link is the class id for the checkboxes and buttons. I've also got a div in the page with the class 'result' Edited September 24, 2015 by sonicthehedgehog Link to comment Share on other sites More sharing options...
sonicthehedgehog Posted September 24, 2015 Author Share Posted September 24, 2015 I've now managed to get it to load the new page within the div but it's not loading the results. It just says that the query was empty so for whatever reason the post details aren't being to the page that gets the results. Any thoughts as to why? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 24, 2015 Share Posted September 24, 2015 Unless filter_link is the class of the form element, you're not serializing the form data. You're trying to serialize whatever that element is. Link to comment Share on other sites More sharing options...
sonicthehedgehog Posted September 25, 2015 Author Share Posted September 25, 2015 The form id is #filter_form but if I change it to that then nothing happened at all and the form doesn't even submit Link to comment Share on other sites More sharing options...
dsonesuk Posted September 25, 2015 Share Posted September 25, 2015 Probably becuase you are using e.preventDefault, i don't know you used it for checkbox as it won't submit form? If this is used for button as well perhaps you should check what type of input is clicked then use e.preventDefault if type button. Link to comment Share on other sites More sharing options...
Ingolme Posted September 25, 2015 Share Posted September 25, 2015 That e.preventDefault line wouldn't do anything because you forgot to call the function uses parentheses (). In order for it to work it would have to be e.preventDefault() Link to comment Share on other sites More sharing options...
dsonesuk Posted September 25, 2015 Share Posted September 25, 2015 I don't understand I've now managed to get it to load the new page within the div but it's not loading the results It should only load html content and results that would normally be between and NOT including <body>...</body> tags. and IT SHOULD work just fine referencing class on checkbox only. So e.preventDefault(); prevents checkbox being checked, AND not just preventing submission of input type submit or button in this case right? Just place simple echo $_POST['name_used_for_check_box']; in index.php to make sure it is picking up this value, then go on from there to make sure sql query, connection is correct, since we have no idea what you are actually doing in index.php file. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now