我正在尝试基于Zendesk API构建联系表单。我想要实现的目标很简单。如果未设置$ _POST ['submit'](如果未提交表单),则显示表单;如果设置了$ _POST ['submit'],则显示成功消息。我正在尝试使用以下代码,但它无法正常工作。我做错了什么?
<?php
define("ZDAPIKEY", "");
define("ZDUSER", "");
define("ZDURL", "");
function curlWrap($url, $json, $action)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
switch($action){
case "POST":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "GET":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
break;
default:
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
}
if (!isset($_POST['submit'])) {
?>
<html>
<div id="box_form">
<form id="zFormer" method="POST" action="contact.php" name="former">
<p>
Your Name:<input type="text" value="James Duh" name="z_name">
</p>
<p>
Your Email Address: <input type="text" value="duh@domain.com" name="z_requester">
</p>
<p>
Subject: <input type="text" value="My Subject Here" name="z_subject">
</p>
<p>
Description: <textarea name="z_description">My Description Here</textarea>
</p>
<p>
<input type="submit" value="submit" id="submitter">
</p>
</form>
</div>
</html>
<?
} elseif (isset($_POST['submit'])) {
foreach($_POST as $key => $value){
if(preg_match('/^z_/i',$key)){
$arr[strip_tags($key)] = strip_tags($value);
}
}
$create = json_encode(array('ticket' => array('subject' => $arr['z_subject'], 'comment' => array( "body"=> $arr['z_description']), 'requester' => array('name' => $arr['z_name'], 'email' => $arr['z_requester']))));
$return = curlWrap("/tickets.json", $create, "POST");
?>
The form has been submitted!
<?
}
?>
答案 0 :(得分:3)
您的提交按钮未命名
<input type="submit" value="submit" id="submitter">
做
<input type="submit" value="submit" id="submitter" name="submit">
因为你的两个条件语句都依赖它
if (!isset($_POST['submit']))
和
elseif (isset($_POST['submit']))
在打开<?php
标记后立即将错误报告添加到文件顶部:
error_reporting(E_ALL);
ini_set('display_errors', 1);
会产生未定义的索引错误。