为什么当我通过 Ajax 将下拉列表中选定的选项传递给 PHP 时,它总是给我第一个选项?

发布于 2024-09-28 15:43:06 字数 1808 浏览 1 评论 0原文

这是我的代码:

<html>
    <head>
        <script>
            comenzar = function(){
                document.getElementById('gene').onclick = xmlhttpPost("generador.php");
            }

            xmlhttpPost = function(strURL){
                xmlHttpReq = false;
                self = this;
                self.xmlHttpReq = new XMLHttpRequest();
                self.xmlHttpReq.open('POST', strURL, true);
                self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                self.xmlHttpReq.onreadystatechange = function(){
                    if(self.xmlHttpReq.readyState == 4){
                        update(self.xmlHttpReq.responseText);
                    }
                }
                self.xmlHttpReq.send(getquerystring());
            }

            getquerystring = function(){
                form = document.getElementById('formu');
                combi = document.getElementById('sele').options[document.getElementById('sele').selectedIndex].value;
                qstr = "tres=" + escape(combi);
                return qstr;
            }

            update = function(resu){
                document.getElementById('tag').innerHTML = resu;
            }
            window.onload = comenzar;
        </script>
    </head>

    <body>
        <form id=formu>
            <select id=sele>
                <option>C</option>
                <option>v</option>
            </select>
            <button id=gene><p>generate</p></button>
        </form>
        <p id=tag></p>
    </body>
</html>

//generador.php
<?php
    echo( "tu combinacion" . $_POST['tres'] );
?>

This is my code:

<html>
    <head>
        <script>
            comenzar = function(){
                document.getElementById('gene').onclick = xmlhttpPost("generador.php");
            }

            xmlhttpPost = function(strURL){
                xmlHttpReq = false;
                self = this;
                self.xmlHttpReq = new XMLHttpRequest();
                self.xmlHttpReq.open('POST', strURL, true);
                self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                self.xmlHttpReq.onreadystatechange = function(){
                    if(self.xmlHttpReq.readyState == 4){
                        update(self.xmlHttpReq.responseText);
                    }
                }
                self.xmlHttpReq.send(getquerystring());
            }

            getquerystring = function(){
                form = document.getElementById('formu');
                combi = document.getElementById('sele').options[document.getElementById('sele').selectedIndex].value;
                qstr = "tres=" + escape(combi);
                return qstr;
            }

            update = function(resu){
                document.getElementById('tag').innerHTML = resu;
            }
            window.onload = comenzar;
        </script>
    </head>

    <body>
        <form id=formu>
            <select id=sele>
                <option>C</option>
                <option>v</option>
            </select>
            <button id=gene><p>generate</p></button>
        </form>
        <p id=tag></p>
    </body>
</html>

//generador.php
<?php
    echo( "tu combinacion" . $_POST['tres'] );
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖粟与秋泊 2024-10-05 15:43:06

问题不在于价值的获取。挂钩是这两件事:

document.getElementById('gene').onclick = xmlhttpPost("generador.php");

...您可能认为您设置了要执行到按钮的 onclick-Event 的某些内容,但您没有。这个函数会立即执行,而不是onclick。

你应该这样写:

document.getElementById('gene').onclick = function(){xmlhttpPost("generador.php");}

另一个问题:

The problem is not the getting of the value. The hook are these two things:

document.getElementById('gene').onclick = xmlhttpPost("generador.php");

...you may think that you set something to be executed to the onclick-Event of the button, but you don’t. This function will be executed immediately, not onclick.

You should write it that way:

document.getElementById('gene').onclick = function(){xmlhttpPost("generador.php");}

The other problem:

The default type of a <button> is submit, so if you click on the button, the form will be sent.
You can either set the type of the button to "button" or cancel the submitting of the form via JavaScript, otherwise your Ajax request is useless, because the form will be sent (and the page reloaded).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文