0

I've been asking a lot of questions as of late related to creating RESTful services with PHP. My question is this:
Can all services (GET, POST, PUT, DELETE) be done from a single web form using radio buttons?
This is what I am picturing in my head:

<form action="MyService.php" method="GET">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" /><br />
        <label for="password">Password</label>
        <input type="password" name="password" id="password" /><br />
        <label for="id">Task ID</label>
        <input type="text" name="id" id="id"/><br />
        <label for="desc">Task Description</label>
        <input type="text" name="desc" id="desc"/><br />
        <label>
            <input type="radio" name="service" value="getRadio" checked/> GET
        </label>
        <label>
            <input type="radio" name="service" value="postRadio" /> POST<br />
        </label>
        <input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
        <input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
        <input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
        <input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
        <input type="submit" name="submit" value="ACTION"/>
</form>

It's incomplete so far, but what I'm thinking of trying to do is have radio button selections for each service, then outline my php file like this:

$request = $_SERVER["REQUEST_METHOD"]
switch($request) {
    case 'GET':
        // logic for GET based on radio button selected
        break;
    case 'POST':
        // logic for POST based on radio button selected
        break;
   // and then PUT and DELETE
}

Is this doable? If so, am I on the right track, or do I need to make changes?

kmancusi
  • 591
  • 3
  • 20
  • 1
    Unfortunately, HTML forms only support GET and POST. http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers. Otherwise you will have to use AJAX to get other types of methods work. – Imesha Sudasingha Apr 23 '16 at 03:55
  • there are notes for it in the working group https://www.w3.org/TR/form-http-extensions/#http-delete-form – Jeff Puckett Apr 23 '16 at 03:58
  • I would ditch the forms ( submitting it anyway ) and use serialize and ajax to do the submission. https://api.jquery.com/serialize/ – ArtisticPhoenix Apr 23 '16 at 04:21

1 Answers1

0

if you want to use the forms, and providing it does work as seems by this spec, then you can use some javascript to change the method of your form onsubmit.

<form action="MyService.php" method="GET" onsubmit='this.method = this.service.value'>
        <label for="username">Username</label>
        <input type="text" name="username" id="username" /><br />
        <label for="password">Password</label>
        <input type="password" name="password" id="password" /><br />
        <label for="id">Task ID</label>
        <input type="text" name="id" id="id"/><br />
        <label for="desc">Task Description</label>
        <input type="text" name="desc" id="desc"/><br />
        <label>
            <input type="radio" name="service" value="GET" checked/> GET
        </label>
        <label>
            <input type="radio" name="service" value="POST" /> POST<br />
        </label>
        <label>
            <input type="radio" name="service" value="PUT" /> PUT
        </label>
        <label>
            <input type="radio" name="service" value="DELETE" /> DELETE<br />
        </label>
        <input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
        <input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
        <input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
        <input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
        <input type="submit" name="submit" value="ACTION"/>
</form>
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167