Javascript Php mysql未定义索引错误使用jquery将数据从一个页面传输到另一个页面
这是我使用Jquery的第一个项目。 有两页,第一页。listofleaders.php 2。leadersprofile.php 在第一页,即listofleaders.php 我有一个输入文本框,用户在其中输入LeaderName,我使用jQuery代码将文本框值传输到leaderprofile.php页面Javascript Php mysql未定义索引错误使用jquery将数据从一个页面传输到另一个页面,javascript,php,jquery,html,mysql,Javascript,Php,Jquery,Html,Mysql,这是我使用Jquery的第一个项目。 有两页,第一页。listofleaders.php 2。leadersprofile.php 在第一页,即listofleaders.php 我有一个输入文本框,用户在其中输入LeaderName,我使用jQuery代码将文本框值传输到leaderprofile.php页面 <html> <head> <script> function ls() { v
<html>
<head>
<script>
function ls()
{
var leaderdetails = "leaderprofile.php?lname="+$("#gopal").val();
$.get(leaderdetails, function( data ) {
//alert(leaderdetails);
location.href = "leaderprofile.php";
});
}
</script>
</head>
<body>
<input type="text" id="gopal" name="t" placeholder="Start Typing" size="50" />
<button onclick="ls();" type="button">Go!</button><br><br>
</body>
</html>
函数ls()
{
var leaderdetails=“leaderprofile.php?lname=“+$(“#gopal”).val();
$.get(leaderdetails,函数(数据){
//警报(领导详情);
location.href=“leaderprofile.php”;
});
}
走
在leadersprofile.php的第二页上,我写了这个
<?php
include "admin/includes/dbconfig.php";
$lname = $_GET['lname'];
echo $lname;
?>
但在第二页,即leaderprofile.php上,它向我显示了错误
未定义的索引:lname
我这样做对吗?
我错在哪里?
希望您能理解。因此,根据您对问题的描述,我在这里对您试图实现的目标进行了猜测 如果要将
值发送到另一个页面,最好使用经典POST请求(无需改进jQuery):
您似乎正确地使用了JQuery。提取值和发送GET请求的Javascript应该可以正常工作 您的误解在于如何检查PHP文件是否收到了请求。这个重定向
location.href = "leaderprofile.php";
不会向您提供有关您刚才发出的GET请求的任何信息。相反,您可以尝试:
location.href = "leaderprofile.php?lname=" + $("#gopal").val()
验证您的PHP和Javascript是否按预期运行。如果你看到了你期望的价值观,那么我相信你已经证实了两件事:
- 已成功从文本框中提取正确的值
- GET请求成功,正在调用成功回调
- 我理解您的问题。请尝试以下代码
listofleaders.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form>
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="name"></td>
</tr>
<tr>
<td></td>
<td><button id="submit">Submit</button></td>
</tr>
</table>
</form>
<script src = "jquery.js"></script>
<script src = "leader.js"></script>
</body>
</html>
现在,这个leader.js文件将名称键发送到liderprofile.php
在该php文件之后,witt将数据(名称)返回到js文件..,js文件将通知名称
leaderprofile.php
<?php
$name = $_POST['name'];
echo $name;
location.href=“leaderprofile.php”
将您重定向到leaderprofile.php
,不带参数。你期望什么?我想带参数去leaderprofile.php我应该在那里写什么?location.href=“leaderprofile.php?parameter=value”代码>?是否必须删除var leaderdetails=“leaderprofile.php?lname=“+$(“#gopal”).val();我不知道你该怎么处理你的代码。
location.href = "leaderprofile.php?lname=" + $("#gopal").val()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form>
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="name"></td>
</tr>
<tr>
<td></td>
<td><button id="submit">Submit</button></td>
</tr>
</table>
</form>
<script src = "jquery.js"></script>
<script src = "leader.js"></script>
</body>
</html>
$(document).ready(function() {
$('#submit').on('click', function(){
var name = $('#name').val();
$.ajax({
url:'leaderprofile.php',
type:'POST',
data:{'name':name},
success:function(data){
}
});
});
});
<?php
$name = $_POST['name'];
echo $name;