如何在HTML / PHP表单中使用PUT方法?

时间:2018-05-04 07:44:32

标签: php html

我有一个基于PHP的简单客户端用于Web服务 我想将用户输入的值传递给url但是使用PUT方法,因为POST方法不支持它。

这是我的客户代码

<!DOCTYPE html>
<?php
    $ip = '172.27.40.113';
    $url = 'http://'.$ip.'/service/aca/public/all-produk-by-id/2';
    $string = file_get_contents($url);
    $json_a = json_decode($string, true);

    $req = 'http://'.$ip.'/service/aca/public/request-produk-branch/2';
?>

<table border="1">
    <th width="50">ID PB</th>
    <th width="100">ID Produk</th>
    <th width="100">Stok</th>
    <th width="100">Order</th>   
    <?php
        foreach($json_a['data'] as $item){
            echo '<tr>';
            echo '<td align="center">'.$item['id_PB'].'</td>';
            echo '<td align="center">'.$item['id_produk'].'</td>';
            echo '<td align="center">'.$item['stok'].'</td>';
            echo '<td align="center">'.
                    '<form method="POST" action="'. $req . '/' . $item['id_produk'] . '/?jumlah=">'.
                        '<input type="hidden" name="_METHOD" value="PUT">'.
                        '<input name="jumlah" type="number" min="1" max="'.$item['stok'].'" placeholder="Value">'.
                        '<input type="submit" value="Order">'.
                    '</form>'.
                '</td>';
            echo '</tr>';
        }
    ?> 
</table>


如果我删除了<input type="hidden" name="_METHOD" value="PUT">,则该值将显示在网址中。但它无法继续,因为它使用POST方法 但是如果我们使用<input type="hidden" name="_METHOD" value="PUT">,则值不会显示在网址中,但它会使用PUT方法
如何将该值传递给url并使用PUT方法?
请帮帮我。

2 个答案:

答案 0 :(得分:1)

不幸的HTML表单不支持PUT方法,而不是直接向第三方提交表单,您可以POST到服务器上的另一个PHP脚本,然后执行{{ 1}}来自您的服务器的请求,例如:

PUT

答案 1 :(得分:0)

您不能因为您需要使用POST将表单数据发送到服务器进行处理,或GET将其添加到URL以允许您使用URL你去的下一页上的参数。对表单的POST和GET请求之间没有,这两个是表单中可以使用的两种类型。