【问题标题】:Auto-submit form using PHP使用 PHP 自动提交表单
【发布时间】:2012-06-21 04:43:00
【问题描述】:

我目前有这个:

function submit()
{
    document.getElementById("lostpasswordform").click(); // Simulates button click
    document.lostpasswordform.submit(); // Submits the form without the button
}

<body onload="submit()">

    <form name="lostpasswordform" id="lostpasswordform" action="/" method="post">
        <input type="hidden" name="user_login" id="user_login" class="input" value="<?php echo ($user_login); ?>" />
    </form>

</body>

它可以在 PC 上运行,但由于某种原因,javascript 不能从 iPhone 执行,所以我想知道是否有一种方法可以使用 PHP 而不是 JS 自动提交表单?

谢谢

【问题讨论】:

  • 有什么理由不使用body onload="document.lostpasswordform.submit()"?

标签: php forms submit


【解决方案1】:

无法在服务器端触发表单提交。您必须使用一种在 DOM 中工作的语言,例如 JavaScript。从您提供给我们的信息来看,我不明白为什么它不会以您现在的设置方式工作。

检查您的代码,如果仍然无法正常工作,我建议您在不同的上下文中提出这个问题;类似于让你的 JavaScript 在 iPhone 上运行而不是完全抛弃它。

【讨论】:

    【解决方案2】:

    正如 esqew 指出的那样,您无法从服务器执行客户端操作。您的选择是重新设计您的函数,使其不需要自动提交(也许您可以使用 GET 变量而不是发布)或为 iPhone 寻找解决方法。

    对于解决方法 - .click() 函数在 iPhone 上不起作用。您可以尝试以前从这个问题中提出的解决方案之一,例如使用tap 或更大的touch handler function

    【讨论】:

      【解决方案3】:

      不,PHP 不能这样做,但您的问题是由于 iPhone 处理点击事件的方式。这是background info and a workaround。似乎您只需要一个空的 onclick 函数来触发它,所以:

      // untested
      var f = document.getElementById('lostpasswordform');
      f.onclick = function () { };
      document.lostpasswordform.submit();
      

      您可能想考虑一下用户的体验——为什么在表单内部单击会自动提交表单?提交按钮有什么问题?

      【讨论】:

        【解决方案4】:

        这是像我这样的 PHP 程序员提供的最小 javascript 答案:

        /** This is the script that will redraw current screen and submit to bank. */
        echo '<script>'."\n" ;
        echo 'function serverNotifySelected()'."\n" ;
        echo '{'."\n" ;
        echo '    window.open(\'\', \'BankPaymentScreen\');'."\n" ;
        echo '    document.forms[\'bank_form\'].submit();'."\n" ;
        echo '    document.forms[\'server_responder\'].submit();'."\n" ;
        echo '}'."\n" ;
        echo '</script>'."\n" ;
        
        /** This form will be opened in a new window called BankPaymentScreen. */
        echo '<form action="https://www.sandbox.bank.com/cgi-bin/webscr" name="bank_form" method="post" target="BankPaymentScreen">'."\n" ;
        echo '<input type="hidden" name="cmd" value="_s-xclick">'."\n" ;
        echo '<input type="hidden" name="custom" value="'.$transaction_start.'">'."\n" ;
        echo '<input type="hidden" name="hosted_button_id" value="'.$single_product->hosted_button_id.'">'."\n" ;
        echo '<table>'."\n" ;
        echo '    <tr>'."\n";
        echo '        <td><input type="hidden" name="'.$single_product->hide_name_a.'" value="'.$single_product->hide_value_a.'">Local</td>'."\n" ;
        echo '    </tr>'."\n" ;
        echo '    <tr>'."\n" ;
        echo '        <td>'."\n" ;
        echo '        <input type="hidden" name="'.$single_product->hide_name_b.'" value="'.$single_product->hide_value_b.'" />'.$single_product->short_desc.' $'.$adj_price.' USD'."\n" ;
        echo '        </td>'."\n" ;
        echo '    </tr>'."\n" ;
        echo '</table>'."\n" ;
        echo '<input type="hidden" name="currency_code" value="USD">'."\n" ;
        echo '</form>'."\n" ;
        
        /** This form will redraw the current page for approval. */
        echo '<form action="ProductApprove.php" name="server_responder" method="post" target="_top">'."\n" ;
        echo '<input type="hidden" name="trans" value="'.$transaction_start.'">'."\n" ;
        echo '<input type="hidden" name="prod_id" value="'.$this->product_id.'">'."\n" ;
        echo '</form>'."\n" ;
        
        /** No form here just an input and a button.  onClick will handle all the forms */
        echo '<input type="image" src="https://www.sandbox.bank.com/en_US/i/btn/btn_purchaseimmediateCC_LG.gif" border="0" alt="This Bank - The safer, easier way to pay!" onclick="serverNotifySelected()">'."\n" ;
        echo '<img alt="" border="0" src="https://www.sandbox.bank.com/en_US/i/scr/pixel.gif" width="1" height="1">'."\n" ;
        

        这是一个按钮的代码。该按钮将重绘当前页面以从购买到预先批准并打开一个新窗口,为新窗口提供焦点并将新的焦点窗口传递给支付提供商。

        这也可以防止 Chrome 阻止新页面获得焦点。

        【讨论】:

          【解决方案5】:

          你可以这样做。

          有一个例子,你甚至可以使用 cURL 来做到这一点

          <?php
          
          //create array of data to be posted
          $post_data['firstName'] = 'Name';
          $post_data['action'] = 'Register';
          
          //traverse array and prepare data for posting (key1=value1)
          foreach ( $post_data as $key => $value) {
              $post_items[] = $key . '=' . $value;
          }
          
          //create the final string to be posted using implode()
          $post_string = implode ('&', $post_items);
          
          //we also need to add a question mark at the beginning of the string
          $post_string = '?' . $post_string;
          
          //we are going to need the length of the data string
          $data_length = strlen($post_string);
          
          //let's open the connection
          $connection = fsockopen('www.domainname.com', 80);
          
          //sending the data
          fputs($connection, "POST  /target_url.php  HTTP/1.1\r\n");
          fputs($connection, "Host:  www.domainname.com \r\n");
          fputs($connection,
              "Content-Type: application/x-www-form-urlencoded\r\n");
          fputs($connection, "Content-Length: $data_length\r\n");
          fputs($connection, "Connection: close\r\n\r\n");
          fputs($connection, $post_string);
          
          //closing the connection
          fclose($connection);
          
          ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-02-09
            • 2012-09-04
            • 1970-01-01
            • 2013-09-07
            • 1970-01-01
            • 2015-09-24
            • 1970-01-01
            相关资源
            最近更新 更多