gpt4 book ai didi

javascript - Ajax Jquery 将变量传递给 PHP 文件

转载 作者:行者123 更新时间:2023-12-03 11:06:17 25 4
gpt4 key购买 nike

嘿,我在从 JQuery Ajax 访问变量时遇到问题。我已经尝试了一切。我什至向这两个文件添加了 cdn 脚本标签。但我不断收到未定义索引的错误

注意:未定义索引:头位于 C:\xampp\htdocs\Project\View.php 第 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>

最佳答案

在使用变量/数组索引之前始终检查它是否存在:

$head = isset($_POST['head']) ? $_POST['head'] : null;
//may check nullity of $head

关于javascript - Ajax Jquery 将变量传递给 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com