使用 PHP 自动提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11131603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:49:09  来源:igfitidea点击:

Auto-submit form using PHP

phpformssubmit

提问by eozzy

I currently have this:

我目前有这个:

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>

it works on PC, but for some reason, javascript is not execute from iPhone so I'm wondering if theres a way to auto-submit the form using PHP instead of JS?

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

Thanks

谢谢

回答by esqew

There's no way to trigger a form submission server-side. You'd have to use a language that works in the DOM like JavaScript for this one. From what you've given us, I don't see why it wouldn'twork with the way you have it set up now.

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

Check your code and if it still doesn't work, I'd suggest asking this question in a different context; something along the lines of getting your JavaScript to work on the iPhone instead of dumping it altogether.

检查您的代码,如果它仍然不起作用,我建议在不同的上下文中询问这个问题;让你的 JavaScript 在 iPhone 上工作而不是完全抛弃它。

回答by John C

As esqew points out, you can't perform a client side action from the server. Your options are to rework your function so it doesn't need the autosubmit (maybe you could use a GET variable rather than posting) or finding a work around for the iPhone.

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

For a workaround - the .click()function doesn't work on iPhones. You could try one of the solutions that have come up from this problem before such as using tapor this larger touch handler function.

解决方法 - 该.click()功能在 iPhone 上不起作用。您可以尝试之前从这个问题中提出的解决方案之一,例如使用tap或这个更大的触摸处理程序功能

回答by jmdeldin

No, PHP cannot do that, but your problem is due to the way the iPhone handles click events. Here's background info and a workaround. It seems like all you need is an empty onclick function to trigger it, so:

不,PHP 不能这样做,但您的问题是由于 iPhone 处理点击事件的方式。这是背景信息和解决方法。看起来你只需要一个空的 onclick 函数来触发它,所以:

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

You might want to think of the experience for a user though -- why would clicking inside a form automatically submit the form? What's wrong with a submit button?

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

回答by user2737761

Here is a minimal javascript answer from a PHP Programmer like myself:

这是来自像我这样的 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" ;

This is the code for one button. The button will redraw the current page to go from purchasing to pre-approval AND also open a new window, give the new window focus and pass the new focused window to the payment provider.

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

This also prevents Chrome from blocking the new page from getting focus.

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

回答by Cristian Fonseca

You can do this in deed.

你可以这样做。

There's an example how to, and even you can do it using cURL

有一个如何做的例子,甚至你也可以使用 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);

?>