1

I have a simple PHP based client for a Web Service.
I want to pass the value that inputed by user to the url but with PUT metho because the POST method not supported it.

Here is my Client code

<!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>


If I delete the <input type="hidden" name="_METHOD" value="PUT">, the value will show in the url. But it can't be proceed because its using POST method.
But if we use <input type="hidden" name="_METHOD" value="PUT">, the value wont show in the url, but it use PUT method.

How can I pass that value to the url and using PUT Method?
Please help me.

2 Answers 2

2

Unfortunatly HTML forms don't support the PUT method, rather than submitting the form directly to the third party, you could POST to another PHP script on your server, and then do the PUT request from your server, something like:

$data = array("jumlah" => $_POST["jumlah"]);
$ch = curl_init($this->_serviceUrl . $id);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

curl_exec($ch);
Sign up to request clarification or add additional context in comments.

2 Comments

But I must know the id_PB and id_produk. Thats why each form action is different from others. (Every loop will get the different value of id_PB or id_produk)
That's fine, just post those values to your script, and then add them into my script above, eg: $data = ["id_PB" => $_POST["id_PB"], "id_produk" => $_POST["id_produk"]];
0

You can't because you either need to use POST which will send the form data to the server for processing, or GET which which will add it to the URL allowing you to use the URL parameters on the next page you go to. There isn't an in between POST and GET requests for forms and those two are the only two types you can use in forms.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.