简体   繁体   English

从HTML表单调用PHP API?

[英]Calling a PHP API from HTML Form?

I've been developing iOS apps for a while now and have just started to get into designing my website. 我已经开发了一段时间的iOS应用程序,并且刚开始着手设计我的网站。 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. urlPath类似于: http://mydomainname.com/folder/anotherFolder/theAPI.php?arg1=one&arg2=two&arg3=three : urlPath等。

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. 我真正想做的是调用带有所有参数的API.php文件,其中每个参数(arg1 =,arg2 =等)都是HTML格式的字段。 I've found a couple of tutorials that deal with HTML forms and validating data, and now my form looks like: 我找到了一些有关HTML表单和验证数据的教程,现在我的表单如下所示:

<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. 抱歉,如果HTML有点儿虚假,我没有足够的经验来了解“简洁” /传统的HTML代码是什么样子,而这正是我从教程中拼凑而成的。 I also know that in the action.php you can get the values in the forms like: $_POST['Name'] . 我也知道,在action.php您可以像$_POST['Name']这样的形式获取值。 I feel like I'm really close - I just can't find anywhere that will tell me how to call this api. 我觉得我真的很亲密-我找不到任何可以告诉我该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 :) 如果您是php专家,请再次申明:)

(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). (哦,结果只是说where子句的第26行有错误-API很好,因为我从我的应用程序对其进行了测试)。 Edit: Where clause used to be While Loop (sorry) 编辑:Where子句曾经是While循环(对不起)

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. 总之,如果有人向我展示了要放入action.php内容,我将不胜感激(不包括验证-我稍后会继续介绍),如果我做的任何可笑的错误,请让我知道。

Thanks :) 谢谢 :)

In HTML the name="" part of the tag should correlate with your arg1, arg2, arg3. 在HTML中,标记的name=""部分应与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. 我不知道您在GET和POST方面有多少经验,但是如果您希望URL像示例中那样包含所有参数,则应将form方法设置为get。 The action attribute of the form tag is the page you want the values to be sent to. 表单标记的action属性是您要将值发送到的页面。 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 提交此表格后,您应该转到URL http://mydomainname.com/folder/anotherFolder/theAPI.php?arg1=inputfromfirstbox&arg2=inputfromfield2&arg3=inputfromfield3

First of all you need to put your PHP instructions between php markups. 首先,您需要将PHP指令放在php标记之间。

<?php
 //You php script here
?>

Moreover, php instruction ended by ";", you did it well for $response. 此外,以“;”结尾的php指令,您对$ response做得很好。

<?php
$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;
?>

Then, it is not a good idea to use directly data from a form. 然后,直接使用表单中的数据不是一个好主意。 You should analyse them before (be sure that your email is a real one and not a random string etc.). 您应该先对其进行分析(确保您的电子邮件是真实的,而不是随机字符串等)。 To do that you can take a look at the regex : http://php.net/manual/en/function.preg-match.php 为此,您可以看一下正则表达式: http : //php.net/manual/en/function.preg-match.php

Let me know if you need more information. 如果您需要更多信息,请与我们联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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