当前位置: 首页 > 知识库问答 >
问题:

为什么我的短信是“sd”?使用JQuery和Post将变量传递给PHP

公冶京
2023-03-14

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

注意:input username获取一个值按钮retrieveQ被单击并调用用head编写的post方法,然后在最后调用php中的getQuestion,用户的值通过数据库被回送到另一个文本框的innerhtml中

数据库和一切正常我只是想要$_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>

共有1个答案

闾丘康安
2023-03-14

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

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

附言:你的代码很难阅读。

 类似资料:
  • 问题内容: 我有一个用JS计算的值,希望将其作为输入表单的一部分传递给PHP脚本。我如何将JS值的JS值作为POST参数获取? 基本上,在提交时,我需要将var通过post传递到下一个脚本。 想到的第一个想法是创建一个不可见的输入表单,该表单中包含值并与该表单一起输入,这可能吗? 问题答案: 是的,您可以在JavaScript代码中使用和设置该隐藏字段的值,以便将其与其他表单数据一起发布。

  • 为什么第一个和第二个写工作,但不是最后一个?有没有办法我可以允许所有3个,并检测它是1,(int)1还是i传入?为什么只允许一个,而允许最后一个?第二个被允许,但不是最后一个,真的让我大吃一惊。 演示显示编译错误

  • 问题内容: 如何在不刷新页面的情况下将变量从jQuery传递到PHP?当我单击一个复选框时,我想将一个变量从jQuery传递到PHP。我也在使用formdialog。 我的PHP代码 我的JavaScript代码 问题答案: Ajax可以做到这一点。谷歌它,并检查api.jquery.com并查看ajax函数,.ajax(),.post(),.get(),.load()等。 关于您的特定问题,这是

  • 问题内容: 为什么此代码无法编译? 为什么我不能将类变量传递给? 问题答案: 该操作符对引用类型,像,而不是对象,如。您可能想要类似 旁注:如果编写,您的代码将更加简洁 但是,我不确定是否需要某种方法。

  • 问题内容: 目前,我在隐藏的输入字段中回显某些变量,并在需要时使用Javascript读取它们。 我和一个同事现在正在考虑使用PHP生成一个额外的Javascript文件,该文件仅包含Javascript的所有变量。这样,变量已经存在,HTML中没有多余的代码。 有什么好的方法可以将变量从PHP传递到Javascript?我们的解决方案听起来如何? 问题答案: 通用数据传递 JavaScript常

  • 问题内容: 好的,希望你们也能帮助我。我对jQuery和AJAX并不了解,也许我错过了一些非常简单的东西。我不能传递多个变量。 jQuery 因此,基本上,如果我运行此程序,它将运行正常,并且我的PHP脚本将对其进行处理。请注意,当我启用警报并显示加载的数据时,它会显示正确的信息(来自的信息),并且能够更新数据库。但是,一旦我添加了另一个变量,它就不会显示在警报框中加载的数据(如果我尝试显示来自两