jQuery美元.post在PHP文件中使用时似乎不能识别url参数


jQuery $.post doesn't seem to recognize url parameter when used in a PHP file

我有一个包含表单的PHP文件。我想使用从jQuery的$.post方法来发布表单结果,并在没有页面刷新的情况下用结果填充页面上的DIV。当我按下提交时,我在Chrome开发工具的网络部分看到的是指定的参数被传递到当前页面(reports.php),而不是我在URL中指定的页面!

表单是报告。php,我想张贴到getSurveys.php。下面是reports.php的代码:

<?php
include("php/common.php");
?>
<html>
<head>
<title>Reports</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>
$("document").ready(function () {
$("#reportform").submit(function() {
var tech = $("#tech_id").val();
var loc = $("#location").val();
var startdate = $("#startdate").val();
var enddate = $("#enddate").val();
var url = "getSurveys.php";
$("#results").html('<br><img src="ajax-loader.gif" /><br>');
$.post(
        url,
        {
         tech: tech,
         loc: loc,
         startdate: startdate,
         enddate: enddate
        },
        function (data) {
            var result = data;
            console.log(result);
        },
        "json"
        );
});
});
</script>
</head>
<body>
<center>
<div  id="criteria" name="criterai" width="75%">
<center>
<h2>Criteria</h2>
<form id="reportform">
<table id="criteriatable" name="criteriatable" width="75%" border="0" cellpadding="3">
<tr>
<td>
<label for="tech_id">Choose a Technician</label>
<br>
<select name="tech_id" id ="tech_id">
<?php 
$q = $conn->query("SELECT *
FROM technicians");
$q->setFetchMode(PDO::FETCH_ASSOC);
while($t = $q->fetch()) {
$techs = $t['tech_id'];
echo "<option>
$techs
</option>";
}
?>
</select>
</td>
<td>
<label for="location">Choose a Location</label>
<br>
<select name="location" id ="location">
<?php 
$q = $conn->query("SELECT *
FROM locations");
$q->setFetchMode(PDO::FETCH_ASSOC);
while($l = $q->fetch()) {
$locs = $l['name'];
echo "<option>
$locs
</option>";
}
?>
</select>
</td>
<td>
<h4>Choose Date Range</h4>
<br>
<label for="startdate">Start Date</label>
<br>
 <input  id="startdate" type=date  name="startdate" >
<br>
 <label for="enddate">End Date</label>
<br>
<input  id="enddate" type=date  name="enddate" >
</td>
</tr>
</table>
<center>
<input type="submit" id="refresh" name="refresh" value ="Refresh">
</center>
</form>
<script>
$( "#reportform" ).validate({
  rules: {
    startdate: {
      required: true,
      date: true
    },
    enddate: {
        required: true,
        date: true
    }
  }
});
</script>
<br>
</center>
</div>
<br>
<br>
<div id="results" name = "results" width="75%"></div>
</center>
</body>
</html>

再一次,问题是当我看它是张贴到哪里,它张贴到报告。php?tech=vale&loc=value等,而不是getSurvey.php!我遗漏了什么?

您需要从您的事件处理程序中return false;来防止浏览器采取提交表单的默认操作

取消导致表单提交的默认事件。

<script>
$("document").ready(function () {
    $("#reportform").submit(function(e) {
        e.preventDefault(); //prevents form submission
        //rest of function
    });
});
</script>