【发布时间】:2011-05-25 18:04:27
【问题描述】:
我想从 html 输入向 PHP 页面发布信息,并使用 jQUERY 和 AJAX 动态重新加载它。该函数应该从页面输入中获取所有值并将它们发布到 PHP 页面,然后在“图表”div 中重新加载页面。我有一个 html 页面、我正在使用的脚本函数以及要发布到的 PHP 页面。单击提交按钮时,我的函数成功地将 PHP 页面加载到 div 中,但我尝试发布的信息似乎没有传输。感谢您的帮助。
这是我使用的 jQUERY 脚本:
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//On-load defaults
var $critSelected = 'sex';
$(".criteria#sex").addClass("criteriaSelected");
//Action for update button
$('form#update').submit(function() {
var graphContent = $("#graphContent").attr("value");
var criteria = $critSelected;
var ageLow = $('#ageLow').attr('value');
var ageHigh = $('#ageHigh').attr('value');
var tr = $('#tr').attr('checked');
var ro = $('#ro').attr('checked');
var tilch = $('#tilch').attr('checked');
$.ajax({
type: "POST",
url: "graph.php",
data: "graphContent="+ graphContent + "criteria=" + criteria,
success: function(data){
$('div.graph').fadeOut(function(){$('div.graph').load("graph.php").fadeIn();});
}
});
return false;
});
//Change selected criteria
$(".criteria").click(function() {
switch($(this).attr("id")) {
case 'sex':
$(this).removeClass("criteriaDeselected").addClass("criteriaSelected");
$(".criteria#loc").removeClass("criteriaSelected").addClass("criteriaDeselected");
$(".criteria#type").removeClass("criteriaSelected").addClass("criteriaDeselected");
$critSelected = 'sex';
break;
case 'loc':
$(this).removeClass("criteriaDeselected").addClass("criteriaSelected");
$(".criteria#sex").removeClass("criteriaSelected").addClass("criteriaDeselected");
$(".criteria#type").removeClass("criteriaSelected").addClass("criteriaDeselected");
$critSelected = 'loc';
break;
case 'type':
$(this).removeClass("criteriaDeselected").addClass("criteriaSelected");
$(".criteria#loc").removeClass("criteriaSelected").addClass("criteriaDeselected");
$(".criteria#sex").removeClass("criteriaSelected").addClass("criteriaDeselected");
$critSelected = 'type';
break;
}
});
});
</script>
我像这样抓取 php 文件中的帖子:
<?php
include("graphFunctions.php");
$graphContent = htmlspecialchars(trim($_POST['graphContent']));
$criteria = htmlspecialchars(trim($_POST['criteria']));
$ageLow = htmlspecialchars(trim($_POST['ageLow']));
$ageHigh = htmlspecialchars(trim($_POST['ageHigh']));
$tr = htmlspecialchars(trim($_POST['tr']));
$ro = htmlspecialchars(trim($_POST['ro']));
$tilch = htmlspecialchars(trim($_POST['tilch']));
echo $graphContent." ".$criteria." ".$ageLow;
?>
html 文件如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="graph.css" rel="stylesheet" type="text/css" />
<?php include("scripts.php"); ?>
</head>
<body>
<form id="update" method"post">
<div id="leftnav" align="center">
<div id="title" align="center">
<select id="graphContent" style="width:150px">
<option value="Age Distribution">Age Distribution</option>
<!-- <option value="sex">Sex Distribution</option>
<option value="volvloc">Volume vs. Location</option>
<option value="treatment">Treatment Distibution</option> -->
</select>
<br />
<br />
</div>
<div id="criteria" align="right">
<br />
<div class="criteria" id="sex" style="float:left"> By Sex </div>
<div class="criteria" id="loc" style="float:left"> By Location </div>
<div class="criteria" id="type" style="float:left"> In/Out Patient </div><br />
<br />
</div>
<div id="constraints" align="left">
<br />
Age Range :  
<input type="text" value="Low" style="width:30px" id="ageLow" />
to 
<input type="text" value="High" style="width:30px" id="ageHigh" />
<br />
<br />
Location :
<input type="checkbox" value="TR" id="tr" />TR
<input type="checkbox" value="RO" id="ro" />RO
<input type="checkbox" value="Tilch" id="tilch" />Tilch<br />
</div>
<div class="submit" align="left" style="padding-top:100px">
<button type="submit" name="submit">Update</button>
</form>
</div>
</div>
<div class="graph" style="display:none">
</div>
</body>
</html>
谢谢!
【问题讨论】:
-
在你的情况下,你真的不需要任何 ajax
-
只在你的图表类中从表单中传输数据,也不需要重新加载,它会更快
-
我最终将使用 php 函数中的数据,该函数将根据 mysql 查询绘制图形。
-
嗯,好的,我明白了,你遇到了什么错误?
-
不明白为什么要将表单数据序列化为一个值,我会在 ajax 查询中单独编写每个 POST 参数。没有使用 Nick E. 在他的回答中谈到的方法,但我认为这更好,可以很好地缩短 JS 代码。