【问题标题】:one form, two submit buttons for two different hidden values一个表单,两个提交按钮,用于两个不同的隐藏值
【发布时间】:2019-10-02 11:00:45
【问题描述】:

我有一个基本的发布表单,可以将一些数据提交到 SMS 网关。 隐藏字段之一“消息”是我希望发送出去的消息文本。 对于两个不同的位置(case1 和 case2),我需要两个提交按钮 - 每个按钮在“消息”值中传输不同的文本。 当数据被发送到第三方网关时,我无法在服务器端做任何事情。 有什么建议吗?

当前代码是:

我只需要两个提交按钮 - 一个用于 case1,一个用于 case2。

<form action="https://gatway.com/send.php" method="post">
<input name="key" type="hidden" value="APIKEYxxxxx" />
<input name="message" type="hidden" value="message to send in case 1" />
<input name="message" type="hidden" value="message to send in case 2" />
To: <input name="to" type="text" />
<input name="username" type="hidden" value="username" />
<input name="from" type="hidden" value="+44xxxxxxxxxx" />

<input type="submit" value="Submit" /></form>

【问题讨论】:

  • 如果您可以编辑表单,那么您可以使用javascript onSubmit 在表单参数中添加更改。编辑form.action,例如

标签: php html forms submit


【解决方案1】:

您可以使用具有相同名称的按钮。 他们将根据被点击的变量填充message 变量。

<form action="https://gatway.com/send.php" method="post">
    <input name="key" type="hidden" value="APIKEYxxxxx" />
    To: <input name="to" type="text" />
    <input name="username" type="hidden" value="username" />
    <input name="from" type="hidden" value="+44xxxxxxxxxx" />
    <button type="submit" name="message" value="message to send in case 1">Submit 1</button>
    <button type="submit" name="message" value="message to send in case 2">Submit 2</button>
</form>

【讨论】:

    【解决方案2】:

    不如有两个提交按钮,有一个然后有两个消息字段作为文本,然后用户可以根据任何情况键入消息。然后不要直接将表单提交到短信网关,而是将表单发送到服务器中的脚本以验证使用了哪种情况,然后使用 CURL 将发布数据发送到短信网关。

    <form action="smschecker.php" method="post">
        <input name="key" type="hidden" value="APIKEYxxxxx" />
        <input name="message1" type="text" value="message to send in case 1" />
        <input name="message2" type="text" value="message to send in case 2" />
        To: <input name="to" type="text" />
        <input type="submit" value="Submit" />
    </form>
    

    smschecker.php

    <?php
    define('SMS_SERVICE_URL', 'https://gatway.com/send.php');
    define('username', 'username_here');
    define('password', 'password_here');
    define('key', 'YOUR_KEY/SID_HERE');
    define('from', "+44xxxxxxxxxx");
    
    if (isset($_POST['message1']) && !empty($_POST['message1'])) {
    
        $message = $_POST['message1'];
    }
    
    if (isset($_POST['message2']) && empty($_POST['message2'])) {
    
        $message = $_POST['message2'];
    }
    
    $to   = $_POST['to'];
    $from = $_POST['from'];
    
    
    $sms = array(
        "to" => $to,
        "message" => $message,
        "from" => from
    );
    
    
    
    $post = array(
        'user' => username,
        'pass' => password,
        'key' => key,
        'sms' => $sms
    );
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, SMS_SERVICE_URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    
    curl_close($ch);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2013-08-31
      相关资源
      最近更新 更多