gpt4 book ai didi

javascript - 为什么我的文本显示为 "sd"?使用 Jquery 和 Post 将变量传递给 PHP

转载 作者:行者123 更新时间:2023-11-28 05:17:53 25 4
gpt4 key购买 nike

我在带有方法发布和提交按钮的表单中有一个按钮类型的按钮。我想在单击按钮(而不是提交)时将用户名输入的值传递给 php,但它不起作用有什么问题请帮忙

注意:输入用户名得到一个值点击retrieveQ按钮,调用head中写的post方法然后在它的最后调用 php 中的 getQuestion 并通过数据库将用户的值回显到另一个文本框的 insidehtml

数据库一切正常,我只是想让 $_POST['uname'] 让我输入用户名框

<?php
include ('comp424.php');

session_start();
if(isset($_SESSION['users'])!="")
{
header("Location: home.php");
}
include_once 'comp424.php';

if(isset($_POST['btn-retrieveP']))
{
//SQL INJECTION
$uname = mysqli_real_escape_string($connection,strip_tags($_POST['username']));
$upass = mysqli_real_escape_string($connection,strip_tags($_POST['pass']));

$salt = '';
$key="ab01$%";

$res= mysqli_query($connection,"SELECT * FROM users WHERE username='$uname'");

$row=mysqli_fetch_array($res);
$salt = $row['salt'];
$hashPass = hash ('sha512', $salt . $key . $upass);

$count = mysqli_num_rows($res);

// if uname/pass correct it returns must be 1 row
if($count == 1 && $hashPass == $row['password'] )
{
if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
{
//Captcha code entered correctly
$_SESSION['user'] = $row['userid'];
header("Location: home.php");
}
else
{
?>
<script>alert('You have entered wrong captcha');</script>

<?php
}


}
else
{
?>
<script>alert('You have entered wrong username or password!');</script>

<?php
}

}


//php function to retrieve Question for the username
function getQuestion()
{

include ('comp424.php');

$unamePR = mysqli_real_escape_string($connection,strip_tags($_POST['username']));



global $question;
$question='sss';
$unamePR = mysqli_real_escape_string($connection,strip_tags($user));
$resPR= mysqli_query($connection,"SELECT * FROM users WHERE username='$unamePR'");

$rowPR=mysqli_fetch_array($resPR);
$countPR = mysqli_num_rows($resPR);
if ($countPR ==1){
$question = mysqli_fetch_array(mysqli_query($connection,"SELECT securityQuestion FROM users WHERE username='$unamePR'"));
$question = $question['securityQuestion'];
}else{
$question='No question is associated with the given Username!';
}
echo $_POST['uname'];
}
?>




<!DOCTYPE html >
<html >
<head>
<meta content="text/html; charset=utf-8" />
<title>Comp424 Password Retrieval</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>

$(document).ready(function () {
$("#btn-retrieveQ").click(function () {
alert("ssss");
var username = $('#uname').val();
$.post("passwordRetrieval.php", {uname: username}, function(data){
alert("data sent and received: "+data);
});





document.getElementById("question").innerHTML= "<?php getQuestion(); ?> ";
});
});



</script>

</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="50%" border="0">
<tr>
<td>
<input type="text" id="uname" name="uname" placeholder="Your Username" required />
</td>
</tr>
<tr>
<td>
<button id="btn-retrieveQ" type="button">Retrieve Question</button>
</td>
</tr>
<tr>
<td>
<p id="question">Type Your Username to Retrive Your Question</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="answer" placeholder="Your Answer" />
</td>
</tr>
<tr>
<td>
<input type="password" name="pass" id="password" placeholder="Your New Password" />
</td>
</tr>
<tr>
<td>
<input name="captcha" type="text" placeholder="Enter Captcha Below">
</td>
</tr>
<tr>
<td>
<img src="captcha.php" />
</td>
</tr>
<tr>
<td>
<button type="submit" name="btn-retrieveP">Retrieve Password</button>
</td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

最佳答案

您试图在您从未调用过的函数中回显该变量。在主 PHP 代码中,您使用 $_POST['username'] 但发布 uname

更改 POST 变量的对象以使用您实际发送的内容。

PS:你的代码很难阅读。

关于javascript - 为什么我的文本显示为 "sd"?使用 Jquery 和 Post 将变量传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40825823/

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