Suggest an AJAX solution to form submission without reloading page, with action="" instead of action="response.php"
I have a search page: (please excuse the bad syntax, just for demo purposes)
<form action="" method="post" name="form">
//form elements and such with a hidden input name="action" value="search"
</form>
<?php if(isset($_POST['action']) and $_POST['action'] == 'search'): ?>
<?php include results.php ?> //takes form data, build SQL query, puts results in array
<?php foreach($results as result) blah blah //for each result display it ?>
<?php endforeach; ?>
<?php endif; ?>
I've seen solutions where the action attribute is set to a php file. Unfortunately in my example it doesn't have one. Take this one: http://www.simonerodriguez.com/ajax-form-submit-example/
开发者_C百科<form name="MyForm" action="response_ajax.php" method="post" onsubmit="xmlhttpPost('response_ajax.php, 'MyForm', 'MyResult', '<img src=\'pleasewait.gif\'>'); return false;">
It looks nice but unfortunately I don't have the first parameter required: reqsponse_ajax.php, mine is just blank.
The JS can be found here: http://www.simonerodriguez.com/wp-content/plugins/downloads-manager/upload/ajaxsbmt.js
If anyone can make modifications to the script or suggest a better solution, that'll be awesome, thanks.
it shouldn't matter at all what's in the action attribute when you're using AJAX. it's the onsubmit or the submit button's onclick attribute that starts the AJAX processing.
you do understand the basics of AJAX, right? instead of actually submitting the form, which would load a new page, you send an XMLHttpRequest to the server, POSTing the data, and wait for an answer back with onreadystatechange, at which point you dynamically update your page. there are literally hundreds if not thousands of examples of this on the web.
精彩评论