0

我正在尝试设置一个表单以将数据发布到一个 php 文件,然后该文件将进行 API 调用,我非常接近,我认为任何帮助都会受到赞赏。

当我一直在进行测试时,表单似乎已发布到 php 文件,但从那时起似乎没有任何事情发生。我打算尝试打印出数据字符串,看看是否正确??

唯一缺少的部分是我拥有但无法在此处发布的网址的第一部分

这是我的html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
label, input, textarea { display: block; float: left; width: 150px; margin: 5px; }
label { clear: left; text-align: right; }
input[type="submit"] { width: 60px; margin: 10px 0 0 165px; clear: left; }
</style>
</head>
<body>
<h2>New Account Creation</h2>
<form action="sonian.php" method="post">
<label>User name (required)</label> <input type="text" name="username" value="" />
<label>Password</label> <input type="text" name="password" value="" />
<label>First Name</label> <input type="text" name="first_name" value="" />
<label>Last Name</label> <input type="text" name="last_name" value="" />
<label>Email</label> <input type="text" name="email" value="" />
<label>Address Line 1</label> <input type="text" name="address1" value="" />
<label>Address Line 2</label> <input type="text" name="address2" value="" />
<label>City</label> <input type="text" name="city" value="" />
<label>County</label> <input type="text" name="state" value="" />
<label>Postcode</label> <input type="text" name="zip" value="" />
<label>Phone Number</label> <input type="text" name="phone" value="" />
<label>Sub Domain</label> <input type="text" name="sub_domain" value="" />
<label>Account Name</label> <input type="text" name="name" value="" />
<label>Number of Users</label> <input type="text" name="number_of_users" value="" />
<label>Account Type</label> <input type="text" name="organization_type_id" value="" />
<div class="field required clear">

<label class="_">Account type</label>
<select id="account_organization_type_id" name="account[organization_type_id]"><option value="1500">Commercial</option>
<option value="1501">Non-Profit</option>
<option value="1502">Education</option>
<option value="1503">Government</option></select>
</div>
<div class="field clear">
<label class="inline"><input name="account[uses_exchange]" type="hidden" value="0" /><input id="account_uses_exchange" name="account[uses_exchange]" type="checkbox" value="1" />&nbsp;Exchange
</label>
<label class="inline"><input name="account[uses_groupwise]" type="hidden" value="0" /><input id="account_uses_groupwise" name="account[uses_groupwise]" type="checkbox" value="1" />&nbsp;GroupWise
</label>
<label class="inline"><input name="account[uses_notes]" type="hidden" value="0" /><input id="account_uses_notes" name="account[uses_notes]" type="checkbox" value="1" />&nbsp;Noteworthy
</label>
<label class="inline"><input name="account[uses_zimbra]" type="hidden" value="0" /><input id="account_uses_zimbra" name="account[uses_zimbra]" type="checkbox" value="1" />&nbsp;Zimbra
</label>

 邮件服务器

</label>
<label class="inline"><input name="account[uses_scalix]" type="hidden" value="0" /><input id="account_uses_scalix" name="account[uses_scalix]" type="checkbox" value="1" />&nbsp;Scalix
</label>
<label class="inline"><input name="account[uses_gmail]" type="hidden" value="0" /><input id="account_uses_gmail" name="account[uses_gmail]" type="checkbox" value="1" />&nbsp;Gmail Apps
</label>
<label class="inline"><input name="account[uses_sunone]" type="hidden" value="0" /><input id="account_uses_sunone" name="account[uses_sunone]" type="checkbox" value="1" />&nbsp;SunONE
</label>
<label class="inline"><input name="account[uses_hosted_exchange]" type="hidden" value="0" /><input id="account_uses_hosted_exchange" name="account[uses_hosted_exchange]" type="checkbox" value="1" />&nbsp;Hosted Exchange
</label>
<label class="inline"><input name="account[uses_kerio]" type="hidden" value="0" /><input id="account_uses_kerio" name="account[uses_kerio]" type="checkbox" value="1" />&nbsp;Kerio
</label>
<label class="inline"><input name="account[uses_other]" type="hidden" value="0" /><input id="account_uses_other" name="account[uses_other]" type="checkbox" value="1" />&nbsp;Other
</label>
</div>
<input type="submit" value="Save" />
</form>
</body>
</html>

这是php代码

    <?php
    if ($_SERVER['REQUEST_METHOD'] == "POST")
    {
    $strPost = "";
    //create string with form POST data
    $strPost = "Username=" . urlencode($_POST['username'])
    . "&Password=" . urlencode($_POST['password'])
    . "&First Name=" . urlencode($_POST['first_name'])
    . "&Last Name=" . urlencode($_POST['last_name'])
    . "&Email=" . urlencode($_POST['email'])
    . "&Address Line 1=" . urlencode($_POST['address1'])
    . "&Address Line 2=" . urlencode($_POST['address2'])
    . "&City=" . urlencode($_POST['city'])
    . "&County=" . urlencode($_POST['state'])
    . "&Postcode=" . urlencode($_POST['zip'])
    . "&Phone Number=" . urlencode($_POST['phone'])
    . "&Sub Domain=" . urlencode($_POST['sub_domain'])
    . "&Account Name=" . urlencode($_POST['name'])
    . "&Number of Users=" . urlencode($_POST['number_of_users'])
    . "&Account Type=" . urlencode($_POST['organization_type_id'])
    . "&Email System=" . urlencode($_POST['uses_exchange']);
    //set POST URL
    $url = "https://url/api/accounts.xml";
    //intialize cURL and send POST data
    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_POST, true);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost);
    @curl_setopt($ch, CURLOPT_URL, $url);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    @curl_exec($ch);
    @curl_close($ch);
    }
    ?>
4

1 回答 1

0

我认为表格有些混乱。

在同一行有一个隐藏的输入,其名称与旁边的复选框相同。

<input name="account[uses_exchange]" type="hidden" value="0" />
<input id="account_uses_exchange" name="account[uses_exchange]" type="checkbox" value="1" />&nbsp;Exchange

php 文件如何理解这一点?

此外,在 php 处理中,您有 var

. "&Email System=" . urlencode($_POST['uses_exchange']);

但我认为您将这个东西的名称作为数组值发送。

 name="account[uses_exchange]"

因此,我想您应该决定是要使用隐藏值还是要使用复选框数组。

然后在 php 处理中循环遍历另一端的数组。

例子

 foreach($_POST['account'] as $k => $v)
    echo "<br>$k => $v";

产生:

organization_type_id => 1500
uses_exchange => 1
uses_groupwise => 1
uses_notes => 1
uses_zimbra => 1
uses_scalix => 1
uses_gmail => 1
uses_sunone => 1
uses_hosted_exchange => 1
uses_kerio => 1
uses_other => 1
于 2013-03-27T20:29:34.530 回答