繁体   English   中英

为什么我会把“sd”作为我的文字? 使用Jquery和Post将变量传递给PHP

[英]Why am I getting “sd” as my text? using Jquery and Post to pass variable to PHP

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

注意:输入用户名获取一个值按钮,单击retrieveQ并调用head编写的post方法,然后在其结束时调用php中的getQuestion,用户通过数据库的值回显到另一个文本框的innerhtml

DATABASE,一切正常我只想让$ _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:你的代码很难阅读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM