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 :)
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.
Comments