如何将在文本框中输入的值传递给API请求值?

时间:2019-02-08 16:25:56

标签: php api

我正在使用API​​,我总是对通过的值进行硬编码,但是现在我希望该值来自文本框中输入的值。

这是我要提交的示例API请求:

    $billTo = new stdClass();
$billTo->firstName = "John";
$billTo->lastName = "Doe";
$billTo->street1 = "1st street";
$billTo->city = "Bangalore";
$billTo->state = "KA";
$billTo->postalCode = "94043";
$billTo->country = "IN";
$billTo->email = "null@gmail.com";
$request->billTo = $billTo;

这就是我要在文本框中获取值的方法。

我的Form.php:

    <form id="Form" name="Form" action="api.php" 
    method="post" target="paInlineFrame">

    <input type="text" name="firstName" value="<?php $billTo_firstName;?>" />
    <input type="text" name="lastName" value="<?php $billTo_lastName;?>" />
    <input type="text" name="street1" value="<?php $billTo_street1;?>" />
    <input type="text" name="city" value="<?php $billTo_city;?>" />
    <input type="text" name="state" value="<?php $billTo_state;?>" />
    <input type="text" name="postalCode" value="<?php $billTo_postalCode;?>" />
    <input type="text" name="country" value="<?php $billTo_country;?>" />
    <input type="text" name="email" value="<?php $billTo_email;?>" />  

   <input type="submit" id="submit" value="Submit"/>
   </form>  

我的api.php:

    $billTo = new stdClass();
    $billTo->firstName = $_POST["billTo_firstName"];
    $billTo->lastName = $_POST["billTo_lastName"];
    $billTo->street1 = $_POST["billTo_street1"];
    $billTo->city = $_POST["billTo_city"];
    $billTo->state = $_POST["billTo_state"];
    $billTo->postalCode = $_POST["billTo_postalCode"];
    $billTo->country = $_POST["billTo_country"];
    $billTo->email = $_POST["billTo_email"];
    $request->billTo = $billTo;

我仅收到以下错误:注意:未定义的索引:3行的/storage/ssd4/317/1753317/public_html/api.php中的billTo_firstName等。 除此以外,我还尝试了其他方法,但都失败了。

希望你能帮助我,我真的迷路了。谢谢!

1 个答案:

答案 0 :(得分:0)

在PHP的name中使用文本区域的$_POST属性应该可以。

您应该将api.php代码片段替换为以下内容:

$billTo = new stdClass();
$billTo->firstName  = isset($_POST["firstName"]) ? $_POST["firstName"] : "";
$billTo->lastName   = isset($_POST["lastName"]) ? $_POST["lastName"] : "";
$billTo->street1    = isset($_POST["street1"]) ? $_POST["street1"] : "";
$billTo->city       = isset($_POST["city"]) ? $_POST["city"] : "";
$billTo->state      = isset($_POST["state"]) ? $_POST["state"] : "";
$billTo->postalCode = isset($_POST["postalCode"]) ? $_POST["postalCode"] : "";
$billTo->country    = isset($_POST["country"]) ? $_POST["country"] : "";
$billTo->email      = isset($_POST["email"]) ? $_POST["email"] : "";

$request->billTo = $billTo;

编辑:

如注释中所述,空合并是PHP7中的一项新功能,并且根据PHP手册,

The null coalescing operator(??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise returns its second operand.

要将其应用到我上面的答案中,您可以将上述代码段替换为:

$billTo = new stdClass();
$billTo->firstName  = $_POST["firstName"] ?? "";
$billTo->lastName   = $_POST["lastName"] ?? "";
$billTo->street1    = $_POST["street1"] ?? "";
$billTo->city       = $_POST["city"] ?? "";
$billTo->state      = $_POST["state"] ?? "";
$billTo->postalCode = $_POST["postalCode"] ?? "";
$billTo->country    = $_POST["country"] ?? "";
$billTo->email      = $_POST["email"] ?? "";

$request->billTo = $billTo;

上面的两个代码段将具有相同的输出。

仅清洁MyForm.php,您可以删除所有文本区域的value属性。