Ajax Jquery 传递变量到 PHP 文件
Ajax Jquery Passing variable to PHP file
嘿,我在从 JQuery Ajax 访问变量时遇到问题。我已经尝试了一切。我什至在两个文件中都添加了 cdn 脚本标签。但我一直收到未定义索引的错误
注意:未定义索引:head in C:\xampp\htdocs\Project\View.php on line 20
任何人都知道语法有什么问题。我在下面附上了我的两个文件。
SearchProjects.php
<html>
<head>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function() {
$('#assign tr').click(function() {
var href = $(this).find("a").attr("href");
if(href) {
window.location = href;
}
});
});
$('td').on('click',function() {
var heading=$(this).text();
alert(heading);
$.ajax({
url: "View.php",
type: "POST",
data: {head: heading},
success: function(data){
alert("success");
}
});
});
</script>
</head>
<body>
<div>
<div class="col-md-12"><br/></div>
<?php
session_start();
$No="Not Assigned";
require("DB.php");
$query="SELECT `ID`, `Assigned_By`, `name`, `path`, `heading`,`Assigned_to`, `Completed`, `Date`, `Due_Date`, `Price` FROM `assign` where `Assigned_to`='Not Assigned' order by Date Desc";
$result=mysqli_query($PDB,$query);
if ($result->num_rows > 0) {
echo "<table class=table table-hover id=assign>"
."<thead>"
. "<tr> "
. "<th>ID</th>"
. "<th>Assigned_By</th>"
. "<th>Name</th>"
. "<th>Path</th>"
. "<th>Heading</th>"
. "<th>Assigned_To</th>"
. "<th>Completed</th>"
. "<th>Due_Date</th>"
. "<th>Submission_Date</th>"
. "<th>Price</th>"
. "</tr> </thead> ";
while($row = $result->fetch_assoc()) {
echo "<tr>"
. "<td>".$row["ID"]."</td>"
. "<td>".$row["Assigned_By"]."</td>"
. "<td>".$row['name']."</td>"
. "<td>".$row['path']."</td>"
. "<td>".$row['heading']."</td>"
. "<td>".$row['Assigned_to']."</td>"
. "<td>".$row['Completed']."</td>"
. "<td>".$row['Date']."</td>"
. "<td>".$row['Due_Date']."</td>"
. "<td>".$row['Price']."</td>"
. "<td><a class=btn btn-default href=View.php role=button>Show More!</a></td>"
. "</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
?>
</div>
</body>
View.php
<html>
<head>
<title>TODO supply a title</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<?php
session_start();
if(isset($_POST['head'])){
echo $_POST['head'];
}
$filename="CV.docx";
// $filename=$_SESSION['filename'];
//echo $filename."<br/>";
require("DB.php");
$query="SELECT `heading` FROM `assign` where `name`='$filename'";
$result=mysqli_query($PDB,$query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$heading=$row['heading'];
$_SESSION['heading']=$heading;
}
}
else {
echo "0 results";
}
$dir = "Assigns/";
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if(!($file=="." || $file=="..")){
$f=explode(".",$file);
if(strcmp($f[0],$heading)){
if(!file_exists("Assigns/$file")) {
die("File not found");
}
else{
$my_file = "Assigns/$file";
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));
}
}
}
}
closedir($dh);
}
}
if($_SERVER['REQUEST_METHOD']== 'POST'){
if (isset($_POST['save'])) {
$assigned_to=$_SESSION['username'];
echo $assigned_to;
echo $filename;
$query="UPDATE `assign` SET `Assigned_to`='$assigned_to' WHERE `name`='$filename'";
$result=mysqli_query($PDB,$query);
if($result){
echo "wohooo";
}
else{
echo "nooo";
}
}
else if (isset($_POST['submit'])) {
// echo "submit";
header('location: Solution.php');
}
}
?>
<div class="container">
<form action="View.php" method="post">
<h1 style="clear:both;"><?php echo $heading."<br/>" ?> </h1>
<div class="form-group">
<?php echo $data; ?>
</div>
<div class="col-md-12 col-md-offset-6">
<input type="submit" name="submit" value="Submit Solution">
<input type="submit" name="save" value="Save Changes">
</div>
</form>
</div>
</body>
在使用前总是检查 variable/array 索引是否存在:
$head = isset($_POST['head']) ? $_POST['head'] : null;
//may check nullity of $head
似乎有两件事正在发生:首先,td 的点击处理程序未附加,因为 DOM 尚未准备好,将您的 jQuery 代码包装在
$(function() {
//your code
})
此外,tr
和 td
上的点击事件都被触发...
嘿,我在从 JQuery Ajax 访问变量时遇到问题。我已经尝试了一切。我什至在两个文件中都添加了 cdn 脚本标签。但我一直收到未定义索引的错误
注意:未定义索引:head in C:\xampp\htdocs\Project\View.php on line 20
任何人都知道语法有什么问题。我在下面附上了我的两个文件。
SearchProjects.php
<html>
<head>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function() {
$('#assign tr').click(function() {
var href = $(this).find("a").attr("href");
if(href) {
window.location = href;
}
});
});
$('td').on('click',function() {
var heading=$(this).text();
alert(heading);
$.ajax({
url: "View.php",
type: "POST",
data: {head: heading},
success: function(data){
alert("success");
}
});
});
</script>
</head>
<body>
<div>
<div class="col-md-12"><br/></div>
<?php
session_start();
$No="Not Assigned";
require("DB.php");
$query="SELECT `ID`, `Assigned_By`, `name`, `path`, `heading`,`Assigned_to`, `Completed`, `Date`, `Due_Date`, `Price` FROM `assign` where `Assigned_to`='Not Assigned' order by Date Desc";
$result=mysqli_query($PDB,$query);
if ($result->num_rows > 0) {
echo "<table class=table table-hover id=assign>"
."<thead>"
. "<tr> "
. "<th>ID</th>"
. "<th>Assigned_By</th>"
. "<th>Name</th>"
. "<th>Path</th>"
. "<th>Heading</th>"
. "<th>Assigned_To</th>"
. "<th>Completed</th>"
. "<th>Due_Date</th>"
. "<th>Submission_Date</th>"
. "<th>Price</th>"
. "</tr> </thead> ";
while($row = $result->fetch_assoc()) {
echo "<tr>"
. "<td>".$row["ID"]."</td>"
. "<td>".$row["Assigned_By"]."</td>"
. "<td>".$row['name']."</td>"
. "<td>".$row['path']."</td>"
. "<td>".$row['heading']."</td>"
. "<td>".$row['Assigned_to']."</td>"
. "<td>".$row['Completed']."</td>"
. "<td>".$row['Date']."</td>"
. "<td>".$row['Due_Date']."</td>"
. "<td>".$row['Price']."</td>"
. "<td><a class=btn btn-default href=View.php role=button>Show More!</a></td>"
. "</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
?>
</div>
</body>
View.php
<html>
<head>
<title>TODO supply a title</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<?php
session_start();
if(isset($_POST['head'])){
echo $_POST['head'];
}
$filename="CV.docx";
// $filename=$_SESSION['filename'];
//echo $filename."<br/>";
require("DB.php");
$query="SELECT `heading` FROM `assign` where `name`='$filename'";
$result=mysqli_query($PDB,$query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$heading=$row['heading'];
$_SESSION['heading']=$heading;
}
}
else {
echo "0 results";
}
$dir = "Assigns/";
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if(!($file=="." || $file=="..")){
$f=explode(".",$file);
if(strcmp($f[0],$heading)){
if(!file_exists("Assigns/$file")) {
die("File not found");
}
else{
$my_file = "Assigns/$file";
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));
}
}
}
}
closedir($dh);
}
}
if($_SERVER['REQUEST_METHOD']== 'POST'){
if (isset($_POST['save'])) {
$assigned_to=$_SESSION['username'];
echo $assigned_to;
echo $filename;
$query="UPDATE `assign` SET `Assigned_to`='$assigned_to' WHERE `name`='$filename'";
$result=mysqli_query($PDB,$query);
if($result){
echo "wohooo";
}
else{
echo "nooo";
}
}
else if (isset($_POST['submit'])) {
// echo "submit";
header('location: Solution.php');
}
}
?>
<div class="container">
<form action="View.php" method="post">
<h1 style="clear:both;"><?php echo $heading."<br/>" ?> </h1>
<div class="form-group">
<?php echo $data; ?>
</div>
<div class="col-md-12 col-md-offset-6">
<input type="submit" name="submit" value="Submit Solution">
<input type="submit" name="save" value="Save Changes">
</div>
</form>
</div>
</body>
在使用前总是检查 variable/array 索引是否存在:
$head = isset($_POST['head']) ? $_POST['head'] : null;
//may check nullity of $head
似乎有两件事正在发生:首先,td 的点击处理程序未附加,因为 DOM 尚未准备好,将您的 jQuery 代码包装在
$(function() {
//your code
})
此外,tr
和 td
上的点击事件都被触发...