1

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

表格在reports.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>

同样,问题是当我查看它发布到哪里时,它发布到reports.php?tech=vale&loc=value 等,而不是getSurvey.php!我在这里想念什么?

4

2 回答 2

1

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

于 2013-06-02T16:28:25.047 回答
0

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

<script>
$("document").ready(function () {
    $("#reportform").submit(function(e) {

        e.preventDefault(); //prevents form submission
        //rest of function
    });
});
</script>
于 2013-06-02T16:28:37.147 回答