以隐藏形式传递PHP变量输入

时间:2016-09-27 22:25:21

标签: php html forms

有点简单,我知道,但它让我感到沮丧。

我有以下表格的此页面。

<?php
$loc = ip_info("Visitor", "Country");
?>

<form id="contact" method="post" class="login">
<input type="hidden" id="type" name="type" value="note">
<input type="hidden" id="location" name="location" value="<?php echo $loc ?>">
<input type="text" name="title" onKeydown='Javascript: if (event.keyCode==13) SubmitFormData();' placeholder="Who are you?" id="title">
<input type="text" placeholder="What do you want?" name="message" id="message" onKeydown='Javascript: if (event.keyCode==13) SubmitFormData();' >
<input type="button" id="submit" onclick="SubmitFormData();" value="" />
</form>

此代码基本上采用表单数据并将其传递给名为post.php的php文件,该文件包含以下内容。

<?php
var_dump($_POST); 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['title'])) $tit=$_POST['title'];
echo $tit;
echo $_POST['title'];
echo $_POST['message'];
echo $_POST['type'];
echo $_POST['location'];
//create array of data to be posted
$post_data['title'] = $_POST['title'];
$post_data['body'] = $_POST['message'];
$post_data['type'] = $_POST['type'];
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = 
curl_init('https://api.pushbullet.com/v2/pushes');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, ['Authorization: Bearer']);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
curl_close($curl_connection);
var_dump($result); 
header("Location: http://www.example.com/pb/");
?>

我在这里尝试实现的是从HTML表单,标题,消息,类型和位置发布4个变量的形式。

除了位置字段外,它们都发布正常。当我将其设置为type =&#34; text&#34;在HTML方面,它正确地拉出位置并显示一个包含正确位置的字段。但是当它试图将数据发布到PHP端时它会失败,在error_log中只给出一个可见错误,这是第11行上位置变量的欠定索引。

我从HTML端正确地获取POST位置后,我想将它添加到传递给curl查询的title变量中,这样,使用pushbullets API发送的消息包含标题为&#34;位置:名称&#34; location是从第一页上的PHP方法中提取的位置,name是从表单传递的title字段。

哇,我开始迷惑自己了。无论如何,如果你能提供帮助,请告诉我。

0 个答案:

没有答案
相关问题