AJAX错误参数

时间:2018-01-04 08:50:26

标签: javascript php jquery html ajax

我是学习JavaScript和AJAX的新手。

由于某种原因,我在执行代码后遇到了问题。我运行代码后返回BAD PARAMETERS

我希望在使用单击按钮#&34;生成"之后显示随机和ascii生成的代码。

<html>
<head>
    <title>Pin Gen</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/pingen.css">
    <script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
    <div id="main">
        <h1>PIN Generator</h1>
        <!--input 1-->
        <input type="text" name="type" placeholder="Your Name">
        <!--input 2-->
        <select name="timezone">
            <option>Choose Method</option>
            <option>ASCII</option>
            <option>Random</option>
        </select>
        <input id="btn" type="button" value="generate">
    </div>
    <div id="pin">
        Your PIN Will Appear Here
    </div>
   <script>
        $(document).ready(function () { 

         var launchAjax = function () { // event handler for button click
             $.get(
            "php/pingen.php/",
            {
                name: $("input[name=timezone]").val(),
                method: $("input[name=type]").val()
            },
            function(data){
               $("#pin").html(data);
            }

           );
        };

         $("#btn").click(launchAjax); // adds the event handler
     });

      </script>
</body>

和我的php文件:

header("Content-type: text/plain");
sleep(rand(0,2)); // simulate slow connection

$name = filter_input(INPUT_GET, "name", FILTER_SANITIZE_STRING);
$method = filter_input(INPUT_GET, "method", FILTER_SANITIZE_STRING);

if ($name === null || $method === null || $method !== "ASCII" && 
        $method !== "Random" || $name === "") {
    echo "BAD PARAMETERS";
} else {
    if ($method === "Random") {
        echo rand(100001, 999999);
    } else {
        $pin = 100001;
        for ($i = 0; $i < strlen($name); $i++) {
            $pin += ord($name)*ord($name)*ord($name);
        }
        echo $pin % 1000000;
    }
}

3 个答案:

答案 0 :(得分:0)

你有选择器问题&#34;输入[name = timezone]&#34;。你没有这样的元素。 尝试使用

  

$(&#34;选择[名称=时区]&#34)

而不是

  

$(&#34;输入[名称=时区]&#34)

答案 1 :(得分:0)

首先在元素选择选项

中使用#ID
<select name="timezone" id="timezone">
        <option>Choose Method</option>
        <option>ASCII</option>
        <option>Random</option>
    </select>

并在Ajax中分配

 name: $("#timezone").val(),
在PHP文件中

你的if santx不是逻辑 用这个。

if ( (!$name  || !$method)  && ( $method !== "Random" ||  $method !== "ASCII" )  ) { /*code here*/ }

我希望这段代码可以帮到你 HTML

&#13;
&#13;
<html>
<head>
    <title>Pin Gen</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/pingen.css">
    <script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
    <div id="main">
        <h1>PIN Generator</h1>
        <!--input 1-->
        <input type="text" name="type" placeholder="Your Name">
        <!--input 2-->
        <select name="timezone" id="timezone">
            <option>Choose Method</option>
            <option>ASCII</option>
            <option>Random</option>
        </select>
        <input id="btn" type="button" value="generate">
    </div>
    <div id="pin">
        Your PIN Will Appear Here
    </div>
       <script type="text/javascript">

        $( document ).ready(function() { 

         var launchAjax = function () { // event handler for button click
             $.get(
            "php/pingen.php/",
            {
                name: $("#timezone").val(),
                method: $("input[name=type]").val()
            },
            function(data){
               $("#pin").html(data);
            }

           );
        };

         $("#btn").click(launchAjax); // adds the event handler
     });

      </script>
</body>
&#13;
&#13;
&#13;

PHP代码

<?php header("Content-type: text/plain"); sleep(rand(0,2)); // simulate slow connection $name = filter_input(INPUT_GET, "name", FILTER_SANITIZE_STRING); $method = filter_input(INPUT_GET, "method", FILTER_SANITIZE_STRING); if ( (!$name || !$method) && ( $method !== "Random" || $method !== "ASCII" ) ) { echo "BAD PARAMETERS"; } else { if ($method === "Random") { echo rand(100001, 999999); } else { $pin = 100001; for ($i = 0; $i < strlen($name); $i++) { $pin += ord($name)*ord($name)*ord($name); } echo $pin % 1000000; } }

答案 2 :(得分:0)

php和jquery代码中存在一些错误。找到下面的工作代码

档案1

<html>
        <head>
        <title>Pin Gen</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="css/pingen.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        </head>
        <body>
        <div id="main">
        <h1>PIN Generator</h1>
        <!--input 1-->
        <input type="text" name="type" id="type" placeholder="Your Name">
        <!--input 2-->
        <select name="timezone" id="timezone">
            <option value="">Choose Method</option>
            <option value="ASCII">ASCII</option>
            <option value="Random">Random</option>
        </select>
        <input id="btn" type="button" value="generate">
        </div>
        <div id="pin">
        Your PIN Will Appear Here
        </div>
        <script>
        $(document).ready(function () { 
            $("#pin").html('.....');
         var launchAjax = function () { // event handler for button click
            var t = $("#timezone").val();
            var n = $("#type").val();
            console.log(t);
            console.log(n);
             $.get(
            "my.php",
            {
                name: t,
                method: n
            },
            function(data){
               $("#pin").html(data);
            }

           ); 
        };

         $("#btn").click(launchAjax); // adds the event handler
        });

        </script>
        </body>

文件2(my.php)

  <?php
            header("Content-type: text/plain");
            sleep(rand(0,2)); // simulate slow connection

            $name = filter_input(INPUT_GET, "name", FILTER_SANITIZE_STRING);
            $method = filter_input(INPUT_GET, "method", FILTER_SANITIZE_STRING);

            if (($name === null || $method === null || $name === "") &&   ($method !== "ASCII" || 
                    $method !== "Random") ) {
                echo "BAD PARAMETERS";
            } else {
                if ($method === "Random") {
                    echo rand(100001, 999999);
                } else {
                    $pin = 100001;
                    for ($i = 0; $i < strlen($name); $i++) {
                        $pin += ord($name)*ord($name)*ord($name);
                    }
                    echo $pin % 1000000;
                }
            }

我希望这会对你有所帮助。