简体   繁体   English

使用Ajax将提示数据从JavaScript传递到PHP

[英]Passing prompt data from javascript to php using ajax

First of all i know there are multiple topics about this on stackoverflow, i read most of them but i still cant figure out why the following is not working. 首先,我知道有很多关于stackoverflow的主题,我阅读了其中的大部分内容,但仍然无法弄清以下内容为何无效。

So i have a form like this: 所以我有这样的形式:

echo "<td> <form action=\"admin.php\" method=\"GET\" onsubmit=\" modifyPassword();\">

this is the modifyPassword function: 这是ModifyPassword函数:

function modifyPassword(){
   var newpw=prompt("Enter a new password");
   if(newpw !== null){
   $.ajax({
           type: "GET",
           url: "admin.php",
           data: newpw,
           success: function(data)
            {
              console.log(data);
            }
            });
            }}

And when the form is actually submitted i want to get the value from what is typed in like this: 当实际提交表单时,我想从这样输入的内容中获取值:

echo $_GET['data'];

This is all in the same file. 全部都在同一个文件中。 The output of $_GET['data'] does not show anything. $ _GET ['data']的输出不显示任何内容。

Can someone tell me what i am doing wrong? 有人可以告诉我我在做什么错吗?

//edit, more code: I am using multiple forms, so here is the code that handles the form: //编辑,更多代码:我正在使用多种形式,因此这里是处理该形式的代码:

}elseif (isset($_GET['Modify'])){


        echo $_GET['data'];

Form itself: 形式本身:

echo "<td> <form action=\"admin.php\" method=\"GET\" onsubmit=\" modifyPassword();\">
                         <input type='hidden' name='counter' value=\"$count\"/> 
                         <input type=\"submit\" value=\"Modify\" name=\"Modify\"/>

Function that is provided: 提供的功能:

<script type="text/javascript">
                        function modifyPassword(){

                            var newpw=prompt("Enter a new password");
                            if(newpw !== null){
                                $.ajax({
                                    type: "GET",
                                    url: "admin.php",
                                    data: {data: newpw}, // passing a key/value pair
                                    success: function(data)
                                    {
                                        console.log(data);
                                    }
                                });
                            }}
                        </script>

data: newpw, should be data: {data: newpw}, This will result in $_GET['data'] being populated. data: newpw,应为data: {data: newpw},这将导致填充$_GET['data'] In this case 'data' becomes the key, while 'newpw' is the value. 在这种情况下,“数据”成为关键,而“ newpw”成为关键。

function modifyPassword(){
   var newpw=prompt("Enter a new password");
   if(newpw !== null){
       $.ajax({
           type: "GET",
           url: "admin.php",
           data: {data: newpw}, // passing a key/value pair
           success: function(data)
            {
              console.log(data);
            }
       });
   }}

I will argue that you should not use so many variables with the same name - just to make things less confusing during bug hunting. 我将争辩说,您不应使用太多同名的变量-只是为了减少在寻找错误时造成的混乱。

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

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