簡體   English   中英

如何使用PHP提交表單?

[英]How to submit a form with PHP?

<form style="text-align:center;" id="paypalform" action="https://www.paypal.com/cgi-bin/webscr" method="POST">
    <input type='hidden' name='cmd' value='_xclick'>
    <input type='hidden' name='business' value='payment@xxx.com'>
    <input type='hidden' name='item_name' value='201001114262121'>
    <input type='hidden' name='amount' id="amount" value='1.00'>
    <input type='hidden' name='currency_code' value='CAD'>
    <input type='hidden' name='return' value='http://www.xxx.com/paypal_process.php'>
    <input type='hidden' name='invoice' value='82'>
    <input type='hidden' name='charset' value='utf-8'>
    <input type='hidden' name='no_shipping' value='1'>
    <input type='hidden' name='no_note' value=''>
    <input type='hidden' name='notify_url' value='http://www.xxx.com/return.php'>
    <input type='hidden' name='rm' value='82'>
    <input type='hidden' name='cancel_return' value='http://www.xxx.com/index.html'>
</form>

有誰知道?

<?php
if(isset($_POST['submit']))
{
    function do_post_request($url, $data, $optional_headers = null)
    {
        $params = array('http' => array(
                        'method' => 'POST',
                        'content' => $data
                    ));
        if($optional_headers != null)
        {
            $params['http']['header'] = $optional_headers;
        }
        $ctx = stream_context_create($params);
        $fp = @fopen($url, 'rb', false, $ctx);
        if (!$fp)
        {
            throw new Exception("Problem with $url, $php_errormsg");
        }
        $response='';
        while (!feof($fp))
        {
            $response = $response.fgets($fp);
        }
        if ($response === false)
        {
            throw new Exception("Problem reading data from $url, $php_errormsg");
        }

        fclose($fp);
        return $response;
    }
    $host = 'http://mydomain.com';
    $url = 'http://mydomain.com/formHandler.php';
    $username = 'admin';
    $password = '123456';
    $data = array ('action' => 'login','lgname' => $username, 'lgpassword' => $password, 'format' => 'txt');
    $data = http_build_query($data);
    $reply = do_post_request($url, $data);
    echo "**********Response*********<pre>";
    echo var_dump($reply);
    #header('location:'.$host);
    #exit;

   } else {
       echo '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'"><input type="text" name="uname" /><br><input type="password" name="password" /><input type="submit" name="submit"></form>';
   }

 ?>

嘗試這個

如果您希望此表單在頁面加載后立即提交,如果確定使用PHP提交表單,則可以將其添加到頁面底部:

<?
echo "<script type=\"text/javascript\"> 
                window.onload=function(){
                    document.forms['paypalform'].submit();
                }
       </script>";
?>

但這似乎還有很長的路要走。 您可以只將javascript寫入頁面中...也許我不確定您要做什么,但是我想我應該把它扔掉。

使用javascript,您可以在PHP代碼中提交表單,如下所示:(技巧位於最后一行)

    $paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    $paypalID = 'xyz-merchant@gmail.com';
    $form = "<form id='paypal' action='". $paypalURL."' method='post'>";
    $form .='<input type="hidden" name="business" value="'. $paypalID.'">';
    $form .='<input type="hidden" name="cmd" value="_xclick">';
    $itemDetail = "Detail goes here";
    $orderId = 4444;
    $totalAmountWithFee = 55;
    $form .='<input type="hidden" name="item_name" value=" '.$itemDetail.'">
        <input type="hidden" name="item_number" value="'.$orderId.'">
        <input type="hidden" name="amount" value="'.$totalAmountWithFee.'">
        <input type="hidden" name="currency_code" value="USD">';

    $form .="<input type='hidden' name='cancel_return' value='http://localhost/public/cancel.php'> <input type='hidden' name='return' value='http://localhost/success.php'>";
    $form.="<script type=\"text/javascript\"> document.forms['paypal'].submit();</script>";
    echo $form;

如果有人有一個好主意,請分享。

您可以使用cURL或此庫(基於cURL和簡單html dom)從PHP腳本提交HTML表單。

您可以使用fsubmit PHP庫https://github.com/randomsymbols/fsubmit從php提交HTML表單

require_once 'fsubmit.php';
$html = '<form action="backend.aspx" method="post"><input type="text" name="question"></form>';
$form = new Fsubmit();
$form->url = 'http://the_url_must_contain_action_url_base'; // If form action is a relative link, then url is a required parameter, if form action is an absolute link, then this parameter can be omitted
$form->html = $html;
$form->params = ['question' => 'To code or not to code?', 'extra_question' => 'In what language?'];
$answer = $form->submit();
echo $answer['content'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM