Calling a PHP API from HTML Form?

nanothread

I've been developing iOS apps for a while now and have just started to get into designing my website. In one of my apps, I add data to my database by using:

let URL = NSURL(string: urlPath.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
let data = NSData(contentsOfURL: URL!)

var response = ""
if let data = data{
        response = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
}

The urlPath would look something like: http://mydomainname.com/folder/anotherFolder/theAPI.php?arg1=one&arg2=two&arg3=three and so forth.

What I really want to be able to do is call that API.php file with all the arguments where each one (arg1=, arg2=, etc) is a field in a HTML form. I've found a couple of tutorials that deal with HTML forms and validating data, and now my form looks like:

<form action="action.php" method="post">
        <div id="formtext">Name</div>
        <input type="text" name="Name">
        <div id="formtext"><br>Email Address:</div>
        <input type="text" name="Email"><br>
        <div id="formtext"><br>Password</div>
        <input type="text" name="Password"><br><br>
        <input type="submit" name="submit" value="Submit">
</form>

Apologies if the HTML is a little cringy, I'm not experienced enough to know what 'tidy'/conventional HTML code looks like and this is what I've managed to piece together from tutorials. I also know that in the action.php you can get the values in the forms like: $_POST['Name']. I feel like I'm really close - I just can't find anywhere that will tell me how to call this api.

The closest I can get is:

$name = $_POST['Name']
$email = $_POST['Email']
$password = $_POST['Password']
$response = file_get_contents('http://domain.com/folder/api.php?Name=' . $name . '&Email=' . $email . '&Password=' . $password);
echo $response

If you're a php expert, again, sorry for butchering your code :)

(Oh, and the result just says there was an error on line 26 in the where clause - the API's fine because I tested it from my app). Edit: Where clause used to be While Loop (sorry)

In conclusion, I'd greatly appreciate if someone showed me what to put in action.php (excluding verification - I'll get on to that later) and please do let me know if I'm doing anything ludicrously wrong.

Thanks :)

blazerunner44

In HTML the name="" part of the tag should correlate with your arg1, arg2, arg3. I don't know how much experience you have with GET and POST, but if you want the URL to contain all of the args like in your example, you should set the form method to get. The action attribute of the form tag is the page you want the values to be sent to. Try the following for your form:

<form action="http://mydomainname.com/folder/anotherFolder/theAPI.php" method="get">
        <div id="formtext">Name</div>
        <input type="text" name="arg1">
        <div id="formtext"><br>Email Address:</div>
        <input type="text" name="arg2"><br>
        <div id="formtext"><br>Password</div>
        <input type="text" name="arg3"><br><br>
        <input type="submit" name="submit" value="Submit">
</form>

After submitting this form you should be taken to the URL http://mydomainname.com/folder/anotherFolder/theAPI.php?arg1=inputfromfirstbox&arg2=inputfromfield2&arg3=inputfromfield3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Calling Javascript from a html form

From Java

Calling external api from spring boot with multipart/form-data

From Dev

Get ID from HTML form - PHP

From Dev

Calling Watson API from PHP code

From Dev

Send data from HTML form to php

From Dev

Calling PHP Web -Service from C# Windows Form

From Dev

Passing variables from a HTML form to PHP

From Dev

Editing SQL table with PHP from HTML form

From Dev

Ajax calling controller to send a html table of objects form from view

From Dev

NextJS calling API in a form submit

From Dev

html form email php "From" formation

From Dev

Create XML structure with PHP from HTML form

From Dev

Calling Form From VBA

From Dev

PHP,HTML Need to direct from one form to another form

From Dev

Not able to login from an html form through php

From Dev

PHP font-size from HTML Form

From Dev

Undefined value when calling an input from a HTML form from PHP

From Dev

Calling JIRA REST Api from Javascript in local HTML file

From Dev

calling php function from html page

From Dev

Sending email from html form using php

From Dev

PHP output from HTML input Form

From Dev

Access name attribute from html form with PHP

From Dev

HTML Form is not printing right from PHP page?

From Dev

From HTML form to SQL database using PHP

From Dev

Calling a PHP Function from a HTML <Select> option

From Dev

Display an HTML table with data from a PhP form

From Dev

Use output from "form" in HTML in an API link

From Dev

Pass PHP variable from HTML form to Javascript

From Dev

Why does PHP not GET the form input from this HTML form?