0

this question is rather trivial, but despite searching and trying out different approaches I cannot get it work.

I have the following content in my action.php

<?php
function sendCommand($item, $data) 
{
      $url = "https://host/rest/items/" . $item;
  $options = array(
    'http' => array(
        'header'  => "Content-type: text/plain\r\n",
        'method'  => 'POST',
        'content' => $data  //http_build_query($data),
    ),
  );

  $context  = stream_context_create($options);
  $result = file_get_contents($url, false, $context);

  return $result;
}
?>

This is part of my html page

<form action="action.php?item=CancelParty&data=ON" method="post" align="center" class="form-inline" role="form">
    <button type="submit" class="btn btn-warning btn-fill">Volume down</button>
</form>

But my request is not being processed. What am I missing? I usually don't do any php or REST calls, for a webdeveloper this one must be really easy I think :)

4Kings
  • 109
  • 1
  • 9

1 Answers1

1

You must actually call the function in the PHP file, add the following line after the last }

sendCommand($_POST['item'], $_POST['data']);

Remember not to trust input from public sources.

Also, change your form tag to:

<form action="action.php" method="post" align="center" class="form-inline" role="form">

Finally, you'll need to actually submit the data, I recommend adding the following within the form:

<input type="hidden" value="CancelParty" name="item">
<input type="hidden" value="ON" name="data">
Enstage
  • 2,106
  • 13
  • 20