简体   繁体   English

通过HTML按钮传递PHP函数(带有参数)

[英]Passing a PHP Function (With parameters) through an HTML Button

Basically, I'm trying to create a login system, and I'm using it what I call "Dynamically" meaning it's included from my other files, and if I wanted to use a different database I would simply pass that database to the login function. 基本上,我正在尝试创建一个登录系统,并且使用的是我所谓的“动态”,这意味着它已包含在其他文件中,如果我想使用其他数据库,则只需将该数据库传递给登录名即可功能。 I know how to do this by default, but as soon as using a button came in I got a little confused. 我知道默认情况下该怎么做,但是一旦使用按钮,我就会有些困惑。

Here's what I have in it's most basic form. 这就是我最基本的形式。

<?php
    createLogin('test', 'test2');

    function createLogin($SQLConnection, $SQLConfig) { 
        echo "<h1> You are currently not logged in!</h1>";
        echo "<form action='handleLogin(".$SQLConnection.",".$SQLConfig.") method='post'>";
        echo "<div align='center'>";
        echo "<table style='width: 475px'>";
        echo "<thead>";
        echo "<th>";
        echo "<tr>Enter your e-mail and password.</tr>";
        echo "</th>";
        echo "</thead>";
        echo "</table>";
        echo "<input type='submit' value='Login' />";
        echo "</form>";
    }

    function handleLogin($foo, $bar) {
        echo $foo . " || " . $bar;
    }
?>

When I click the submit button however, it simply takes me here... 但是,当我单击“提交”按钮时,只需将我带到这里...

http://localhost/handleLogin%28test,test2%29%20method=

Now, I read about using Javascript to do this, and to do something like 现在,我阅读了有关使用Javascript来执行此操作以及执行类似操作的信息

<script>
    function processLoginRequest($SQLConnection, $SQLConfig) {
        alert("<?php handleLogin($SQLConnection, $SQLConfig) ?>");
    }
</script>

Then I could use 那我可以用

echo "<form action='processLoginRequest(".$SQLConnection.",".$SQLConfig.") method='post'>";

However, the code causes the entire php script to die. 但是,该代码导致整个php脚本死亡。 (Without error?) (没有错误?)

You do NOT want to pass your SQL Configuration parameters back to JavaScript, because anyone can look at your JavaScript code when they browse your page, and then they'll have everything they need to connect and play around in your database. 您不希望将SQL Configuration参数传递回JavaScript,因为任何人在浏览您的页面时都可以查看您的JavaScript代码,然后他们将拥有连接和在数据库中玩耍所需的一切。

You will have to pass some kind of flag to your form, to let your PHP code know (when it receives the form's data later) what kind of SQL settings it should use. 您将必须向表单传递某种类型的标志,以使您的PHP代码(稍后接收到表单数据时)知道应使用哪种SQL设置。

Example: 例:

<form method="POST" ...>
    <type input="hidden" name="loginMode" value="<?php echo $loginMode; ?>" />
</form>

Again, don't pass any sensitive data in there, just have some kind of unique value like "mySql" for $loginMode or the other options. 同样,不要在其中传递任何敏感数据,只需为$ loginMode或其他选项使用某种独特的值,例如“ mySql”。

And then, when you're handling the HTTP POST in your PHP: 然后,当您在PHP中处理HTTP POST时:

if ($_POST['loginMode'] == 'mySql') 
{ 
    // ... create connection based on $SQLConnection, $SQLConfig
}
else if ($_POST['loginMode'] == 'otherMethod') ...

You're using action incorrectly, and the result is as expected. 您使用的action不正确,结果与预期的一样。 action stores the page to which the form will be submitted. action存储表单将提交到的页面。 So, yes, when you hit submit it is trying to take you to a page called handleLogin%28test,test2%29%20method= because that is what your action says to do. 因此,是的,当您单击“提交”时,它试图将您带到一个名为handleLogin%28test,test2%29%20method=因为这是您的操作要执行的操作。

What you can do is simply leave the action blank, which will submit the form to the current page. 您可以做的就是简单地将操作保留为空白,这会将表单提交到当前页面。 Then, on that page, check if the form has been submitted, and if so, call your function. 然后,在该页面上,检查是否已提交表单,如果已提交,则调用您的函数。

Inside the function that creates the form make these changes: 在创建表单的函数内进行以下更改:

function createLogin() { 
    ...
    echo "<form action='' method='post'>";
    ....
    echo "<input type='submit' value='Login' name='login'/>";
}

Then, at the top of the page that renders the form, add something like this: 然后,在呈现表单的页面顶部,添加如下内容:

// Check if login form has been submitted - if so, handle
if (isset($_POST['login'])) {
    handleLogin($SQLConnection, $SQLConfig);
}

// Render login form. No need to pass config parameters here.
createLogin();

If you really want to keep everything in a single function, I suppose you could also do it like this: 如果您真的想将所有功能都保留在一个函数中,那么我想您也可以这样做:

function createLogin($SQLConnection, $SQLConfig) { 
    if (isset($_POST['login'])) {
        handleLogin($SQLConnection, $SQLConfig);
    }
    else {
        echo "<h1> You are currently not logged in!</h1>";
        echo "<form action='' method='post'>";
        echo "<div align='center'>";
        echo "<table style='width: 475px'>";
        echo "<thead>";
        echo "<th>";
        echo "<tr>Enter your e-mail and password.</tr>";
        echo "</th>";
        echo "</thead>";
        echo "</table>";
        echo "<input type='submit' value='Login' name='login'/>";
        echo "</form>";
    }
}

Your JavaScript is probably failing because of the the contents of $SQLConnection and $SQLConfig . 您的JavaScript可能由于$SQLConnection$SQLConfig的内容而失败。 If you have a double quote in them it would fail. 如果您在其中使用双引号,则将失败。

Also designing and implementing a safe and robust login system is actually pretty difficult and you should opt using a framework that has been tested over time. 设计和实施安全可靠的登录系统实际上也非常困难,您应该选择使用经过一段时间测试的框架。

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

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