简体   繁体   English

无法解析来自 PHP 中 Python CGI 脚本的数据

[英]Unable to parse data from Python CGI script in PHP

I have a PHP script that has a form with two input fields.我有一个 PHP 脚本,它有一个带有两个输入字段的表单。 My page needs to pass these two input values back into my Python CGI script (calculate.py) when a user clicks the submit button, retrieve the result and prints it back onto the same PHP page below my input fields (without the page refreshing).当用户单击提交按钮时,我的页面需要将这两个输入值传递回我的Python CGI script (calculate.py),检索结果并将其打印回我的输入字段下方的同一 PHP 页面(不刷新页面) .

I tried simple form action calls to the script but they led me to a new page, ie my own script opening.我尝试对脚本进行简单的表单操作调用,但它们将我带到了一个新页面,即我自己的脚本打开。 I then read answers saying that for dynamic page updating, JS & AJAX need to be used, and thus created a JS function called python_process() in my PHP file based on those guidelines.然后,我阅读了答案,说对于动态页面更新,需要使用 JS 和 AJAX,因此根据这些指南在我的 Z2FEC392304A5C23AC138DA22847F9B7C 中创建了一个名为python_process()的 JS function。

However, when I currently click submit, nothing appears below my input fields.但是,当我当前单击提交时,我的输入字段下方不会出现任何内容。

I have also tried to debug this by viewing the developer console, but nothing appears there.我也尝试通过查看开发者控制台来调试它,但那里什么也没有。

Here is my PHP code:这是我的 PHP 代码:

    <?php

    print "<html>";
    print "<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.4.4.min.js\"></script>";
    print "<script>
    function python_process() {
        var cancellationrate = $('input[name=cancellationrate]').val();
        var waitingtime = $('input[name=waitingtime]').val();
        $.ajax({
            url: \"/cgi-bin/calculate.py\",
            type: \"POST\",
            contentType: \"application/x-www-form-urlencoded\",
            data: {\"cancellationrate\" : cancellationrate, \"waitingtime\": waitingtime },
            success: function(response){
                        $(\"#result\").html(response);
                    }
            });
    };
    </script>";
    print "<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">
    ";
    print "<br/>";
    print "<div class=\"container\">";
                print "<form name=\"calculate\" id=\"calculate\" method=\"post\">";
                    print "<div class=\"form-group row\">";
                        print "<label for=\"cancellationrate\" class=\"col-sm-2 col-form-label\">Cancellation Rate</label>";
                        print "<div class=\"col-lg-10\">";
                            print "<input type=\"text\" style=\"width: 400px\" class=\"form-control\" name=\"cancellationrate\" id=\"cancellationrate\" placeholder=\"Expected % Change in Cancellation Rate\">";
                        print "</div>";
                    print "</div>";
                    print "<div class=\"form-group row\">";
                        print "<label for=\"waitingtime\" class=\"col-sm-2 col-form-label\">Waiting Time</label>";
                        print "<div class=\"col-lg-10\">";
                            print "<input type=\"text\" style=\"width: 400px\" class=\"form-control\" name=\"waitingtime\" id=\"waitingtime\" placeholder=\"Expected % Change in Waiting Time\">";
                        print "</div>";
                    print "</div>";
                    print "<center><button type=\"button\" onclick=\"python_process()\" class=\"btn btn-primary\">Submit</button></center>";
                print "</form>";
    print "</div>";

    print "<div id=\"result\"></div>";


    ?>

And here is my calculate.py CGI script:这是我的 calculate.py CGI 脚本:

#!/anaconda3/bin/python3

import cgi
print ("Content-type: text/html\n\n")

data = cgi.FieldStorage()

cancellation_rate =  int(form.getvalue('cancellationrate'))
waitingtime = int(form.getvalue('waitingtime'))

result = cancellation_rate * waitingtime

print(result)

Turns out it was a very small error in my Python script where I define my CGI field storage as a variable called data and try to access its value using form.原来这是我的 Python 脚本中的一个非常小的错误,我将 CGI 字段存储定义为一个名为 data 的变量,并尝试使用表单访问它的值。

Also, this question helped answer my div value not updating!此外,这个问题有助于回答我的 div 值没有更新!

Update div with jQuery ajax response html 使用 jQuery ajax 响应 html 更新 div

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

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