我试图将点击的图片的ID传递到下一页。当我开发我的代码时,它没有把我重定向到下一页。当我点击F12并检查网络中的POST时,它显示变量被正确地传递到下一页,如附图所示,但它没有将我重定向到下一页。所以现在我知道变量在下一页中被正确地传递和接收了,但我需要在我的代码中引导我到下一页,注意:当我试图把我的代码放在 window.location.href = 'userevent.php';
它重定向我,但没有变量,并给我错误(注意:Undefined index: clicked in)此外,当我使用 url: '@userevent.php.Action("delivery", "service")',
它给我的网络状态403这是我的代码。
<?php
session_start();
require "init.php";
login();
?>
<html>
<!--...-->
<head>
<meta charset="utf-8">
<title> Ghost </title>
<!-- <link rel="Stylesheet" href="css/style1.css">-->
<link rel="stylesheet" href="css/style2.css" media="screen" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script >
function myFunction(clicked) {
document.getElementById('test').innerHTML = clicked;
$.ajax({
type: "POST",
//url: '@userevent.php.Action("delivery", "service")',
url: 'userevent.php',
dataType:'json',
data: {"clicked": clicked},
success: function(data){
}
});
window.location.href = 'userevent.php';
}
</script>
</head>
<body>
<div class="sidenav">
<a href="#Main Page">Main Page</a>
<a href="#About Us">About Us</a>
</div>
<div class="login-box">
<h1>Add Event</h1>
<div>
<p id="test"> </p>
<?php
// LOGIN USER
function login(){
global $con;
global $counter;
echo "<table align='center' >";
//$email = $mysqli->escape_string('');
$query="SELECT * FROM events ORDER BY ID ASC";
$result=mysqli_query($con,$query);
if ( $result->num_rows == 0 ) // User doesn't exist
echo "User with that ID doesn't exist!";
else { // User exists
$counter = 0;
$emptyArray = [];
while($row = $result->fetch_assoc())
{
$emptyArray[$counter]= $row["ID"];
if($counter == 0)
{
echo '<tr>';
}
echo '<td><img id=' . $row["ID"]. ' onClick="myFunction(this.id)" src="images/' . $row["photo"]. '" width="250px" height= "250px" alt="Avatar" >
<h1 id = "GFG_DOWN" style =
"color:white;text-align:center; font-size: 20px; font-weight: bold;"> '.$emptyArray[$counter].'
</h1> </td>';
$counter++;
if($counter == 3)
{
echo "</tr>";
$counter = 0;
}
}
}
}
mysqli_close($con);
?>
这是第二页的代码。
<div class='textbox'>
<label> ID: ".$_POST['clicked']."</label>
</div>
整个Ajax的意义在于,该请求是用JavaScript进行的,而响应是 由JavaScript处理 橆 导航到一个新的页面。
如果你想提出一个POST请求并导航到结果页面,那么使用一个 <form>
window.location.href = 'userevent.php';
它重定向我,但没有变量
将一个URL分配给 location.href
触发浏览器导航(通过GET请求)到该URL。
它与Ajax请求是完全不同的请求,所以它没有该请求的数据。
再次强调。使用一个表单来处理这个问题。
我从数据库中读取数据,我得到图片的点击id。
把你要发送的数据放在一个提交按钮中代替。
<form method="POST" action="userevent.php">
<?php while($row = $result->fetch_assoc()) ?>
<button name="clicked" value="<?php echo htmlspecialchars($row["ID"]); ?>">
<img src="images/<?php echo htmlspecialchars($row["photo"]); ?> alt="Put useful and unique alt text so people know what they are selecting if they can't see the image">
</button>
<?php } ?>
</form>
我认为你最好做一个小表单,因为你想在点击时将用户发送到一个新的页面。
<?php while($row = $result->fetch_assoc()) ?>
<form action="userevent.php" method="POST">
<input type="hidden" name="clicked" value="$row["ID"]">
<img src="images/{{ $row['photo'] }}" class="img">
</form>
<?php } ?>
$('.img').click(function() {
this.form.submit();
});
*经过编辑以反映你目前的编辑情况。