PHP表单使用AJAX获取MySQL [重复]

This question already has an answer here:

I developed a php form which initiates a database request to fetch data depending on a drop down choice.

PHP Form:

<form method="get" action="<?php echo $url = basename($_SERVER['PHP_SELF']); ?>">
<select name="town" onchange='this.form.submit()'> 
    <?php $result= mysql_query('Query'); ?> 
<option value="x" selected>Select Choice</option>
    <?php while($row= mysql_fetch_assoc($result)) { ?> 
        <option value="<?php echo htmlspecialchars($row['town']);?>" > 
            <?php echo htmlspecialchars($row['town']); ?> 
        </option> 
    <?php } ?> 
<input type="hidden" name="action" value="submit" /><br>
</select>
</form>

Form action:

<?php
if(isset($_GET["action"])) { 

$var1= $wpdb->get_results("Query");
$var2= $wpdb->get_results("Query");

Content to show once executed }


 ?>

How can I make the form fetch the Data using AJAX not to stay refreshing the whole page continuously but only the form part?

</div>
<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
  <form id="form_id" action="<?php echo $url = basename($_SERVER['PHP_SELF']); ?>" method="post">
    <select id="town" name="town" onchange="send_to_server()"> 
<?php $result= mysql_query("Query"); ?> 
<option value="x" selected>Select Choice</option>
<?php while($row= mysql_fetch_assoc($result)){ ?> 
    <option value="<?php echo htmlspecialchars($row['town']); ?>"> 
        <?php echo htmlspecialchars($row['town']); ?> 
    </option> 
<?php } ?> 
<input type="hidden" name="action" value="submit" /><br>
</select>
 </form>
<script type='text/javascript'>
/* attach a submit handler to the form */
function send_to_server(){

var value = $("#town").val();

  /* get some values from elements on the page: */
  var $form = $("#form_id"); var url = $form.attr('action');

  /* Send the data using post */
  var posting = $.post( url, { option_value: $("#town").val() } );

  posting.done(function( data ) {
    alert('success');
  });
}
</script>
</body>
</html>

The above does exactly what you want. Check it in localhost