简体   繁体   中英

HTML Form PHP script action for API call

I'm to use a submission form to my API call a little more user friendly. I wish to do this via a simple HTML form which is as such:

input type="text" name="ID:" placeholder="ID">

Upon hitting the submit button it will call my PHP file which makes the API call.

Normally I would just amend my PHP Script to suit the information i'm calling but as stated want to make this easier for a user to submit. Here is a snippit from the PHP:

<?php
$id = "123456";
$apiurl = "http://url.co.uk/api/information.json?id=" . $id;

Instead of me manually entering "123456" on the php how can I use the form to submit the data.

I tried adding $ID = $_POST["ID"];

But this only fails for me.

Theres a typo in your input. Instead of name " ID: " you should have just ID, thats why it doesnt work - because theres no element with index 'ID' in POST array.

So simply: input type="text" name="ID" placeholder="ID" />

And then , in PHP

<?php
$id = $POST['ID'];
$apiurl = "http://url.co.uk/api/information.json?id=" . $id;
?>

Either change the $POST['ID'] to $POST['ID:'] or change the name of the input

Remember to specify the method of posting your form to the php file.

http://www.w3schools.com/html/html_forms.asp

instead of $ID = $_POST["ID"]; use $ID = $_POST["ID:"];

Check out PHP's documentation about curl. It might help you.

<?php 
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM