0

我之前有一个自定义表单,用户可以填写该表单来下工作订单,一旦他们点击提交,它就会创建一个包含所有信息的新工单并将其添加到一个 RT 队列中。

我们以前使用 Mason 来执行此操作,但现在我们已迁移到 WordPress,并希望使用 PHP 以更清洁的方式重做此操作。

我通读了 API 文档并查看了这个帖子以及在 Stack Overflow 上发布的许多其他帖子。我知道如何通过命令行和 cURL 连接到 RT 并创建新票证,但我似乎无法弄清楚如何使用提交时的 Web 界面来执行此操作。如果有人能给我一些关于从哪里开始的指示,我将不胜感激。

谢谢

编辑: 感谢您的回复。下面是我制作的表单,它与我们的 SQL 数据库交互以提取一些信息,我需要它来创建一个包含所有提交信息的新工单。我应该创建一个类似于 [this][2] 的新 php 文件并将其包含为表单操作吗?

<form action="<?php echo $_SELF; ?>";

      method="post"

          id="woForm"

        name="woForm"

     enctype="multipart/form-data"
     >
      <input type="hidden" name="session_id" value="<?php echo session_id(); ?>">

      <input type="hidden" name="Queue"  value="<?php echo $queue; ?>">

      <input type="hidden" name="id"     value="new">

      <input type="hidden" name="Status" value="new">

      <input type="hidden" name="Owner"  value="10">



      <table width="450" align="center" border="0" cellpadding="0" cellspacing="5">


      <tr><td align="left" colspan="2">

             <h2><?php echo $name; ?></h2>

             <p>Please note that all fields except for <b>Ext:</b>, <b>CC:</b> and <b>Attachments:</b> are <span class="required">required</span>.

               You cannot submit a request for assistance using this form unless all the required

               fields have been completed.</p>

             <h2 style="color:red;">Please enter information for the INDIVIDUAL needing assistance</h2>

          </td>

      </tr>

      <?php
      // Get all of the customFields
      $query1 = "select * from CustomFields where disabled='0' and sortOrder != 0 order by sortOrder ASC;";
      $result1 = mysql_query($query1) or die ("dead3: ".mysql_error());

      // Go through each custom field
      while($row1 = mysql_fetch_array($result1)) {

         // Get the information about that field
         $count = 0;
         $fieldId = $row1['id'];
         $name = $row1['Name'];

         // $postname is in a very specific format, and will become the name of the field in the form 
         // where the data for this custom field is entered. In order to submit a ticket into rt, the  
         // name of the field MUST be in this format.
         $postName = "Object-RT::Ticket--CustomField-".$fieldId."-Values";
      ?>

         <!-- Create a row in the table for this custom field -->
         <tr>
              <!-- Create a column with the name of the custom field -->
              <td align="right" class="requestformlabel"><label class="required"><?php echo $name; ?>:</label></td>
      <!-- Create a column for the input field -->
      <td class = "requestformtd">

      <?php   

      // If the custom field is department or building, we need a pull-down menu
      if($name=="Department" || $name=="Building") { ?>

      <!-- start of the pull-down menu -->
      <select name="<?php echo $postName; ?>">

       <?php   

         // Get all of the possible values for the customField from the database
         // Added option to exclude sort order 9999.  See ticket #40665 for more info.
         $query3 = "SELECT * FROM CustomFieldValues WHERE CustomField='$fieldId' AND SortOrder != '9999' ORDER BY SortOrder ASC";
         $result3 = mysql_query($query3) or die ("dead4: ".mysql_error());

         // Go through each possible value for the custom field
         while($row3 = mysql_fetch_array($result3)) {      

           // Get the information on the custom field value from the database
           $tmp = $row3['Name'];
           $description = $row3['Description'];

           // If the custom field value was already selected
           if($tmp == $_POST["$postName"]) {

             // Insert the option into the pull-down menu and mark it as selected in the form
             echo "<option value='$tmp' selected='selected'>$description</option>";

           // otherwise
           } else {

             // Only insert it as an option in the pull-down menu
             echo "<option value='$tmp'>$description</option>";
           } 
        } 
      ?> 

      </td></tr> 
      <?php 

      // If the name of the custom field is operating system, we want radio buttons
      } else if ($name == "Operating System") {   

         // Get all the possible values for this field form the database
         $query4 = "select * from CustomFieldValues where CustomField='$fieldId' order by sortorder asc";
         $result4 = mysql_query($query4) or die ("dead5: ".mysql_error());

         // For each customfield value
         while($row4 = mysql_fetch_array($result4)) {      

            // Get the description of the customfieldvalue from the database
            $osName = $row4['Description']; 

            // If the customfieldvalue has already been selected
            if ($osName == $_POST["$postName"]) {

                // Put the radio button into the form and mark it as checked
                echo "<input type='radio' name='$postName' value='$osName' checked='checked'>$osName";

            // Otherwise
            } else {

                // Put the radio button into the form
                echo "<input type='radio' name='$postName' value='$osName'>$osName";
            }
         } ?> 

         </td></tr>

      <?php 

      // If the name of the custom field is ip adress, we want a disbaled text box. This is because while we want the user to see their ip adress, we do not want them to be able to change it.
      } else if ($name == "IP_Address"){ 

      ?>

              <input name="<?php echo $postName; ?>" size="40" value='<?php 
      echo $_SERVER['REMOTE_ADDR']; ?>' readonly></td></tr>         

      <?php  

      // If it's the hostname variable
      } else if ($name == "Host_Name"){

      ?>

              <input name="<?php echo $postName; ?>" size="40" value='<?php echo gethostbyaddr($_SERVER['REMOTE_ADDR']); ?>' readonly></td></tr>

      <?php  

      // Otherwise, create a text box for the custom field.
      } else { 

      ?>

              <input name="<?php echo $postName; ?>" size="40" value='<?php echo $_POST["$postName"]; ?>'></td></tr>

      <?php  } // end else statement

      } // end while loop

      ?>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Your E-mail Address:</label></td>

            <td  align="left" class="requestformtd"><input name="Requestors" size=40 value="<?php echo $_POST['Requestors']; ?>"></td>



      </tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Confirm Your E-mail Address:</label></td>

            <td align="left" class="requestformtd"><input name="Requestors_2" size=40 value="<?php echo $_POST['Requestors_2']; ?>"></td>

      </tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="fields">Cc:</label></td>

            <td  align="left" class="requestformtd"><input name="Cc" size=40 value="<?php echo $_POST['Cc']; ?>"></td>

         </tr>



      <tr>

             <td align="right"><p>&nbsp;<br/>&nbsp;</p></td>

      <td align="right"><span class="ccnote">(Separate multiple email addresses with commas.)<br/>&nbsp;</span></td>

      </tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Short Problem Summary:</label></td>

            <td align="left" class="requestformtd"><input name="Subject" size=40 maxsize=100  value="<?php echo $_POST['Subject']; ?>"></td></tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Decribe the issue below:</label></td>

         <td  align="left" class="requestformtd"><textarea

           class="messagebox" cols=35 rows=15 wrap="hard" name="Content"><?php echo $_POST['Content']; ?></textarea>

         </td>



      </tr>



      <?php 



      //if session has attachments

      if($_SESSION['attach'] != '') {

      ?>

      <!-- row for existing attahcments -->

      <tr>
          <!-- column that states these are the current attachments, and tells the user what to do if 
      they wish to remove an attachment. -->

          <td class="requestformlabel" align="right">Current Attachments:<br/>

              <span class="ccnote">(Check box to delete)</span>

          </td>

          <!-- coulmn that lists the attachments -->

          <td class="requestformtd" align="right">

              <?php

                 // Go through each file in $_SESSION['attach']

                 while (list($key, $val) = each($_SESSION['attach'])) {

                    // Get the name of the file

                    $attName = $val['name'];

                    // Create a checkbox to mark the file as needing to be removed from the list

                    echo "<input type='checkbox' name='DeleteAttach-$attName' value='1'>$attName<br/>";


                 } // end while loop

              ?>

          </td>

      </tr>



      <?php // end if for attachments

      }

      ?>



      <tr>

          <td class="requestformlabel" align="right"><label class="fields">Attachments:</label></br>

              <span class="ccnote">Max. attachment size: 50MB.</span></td>

          <td align="right" colspan="2"  class="requestformtd">

               <input type="file" name="Attach">

               <br/>

               <input type="submit" name="AddMoreAttach" value="Add More Files">

          </td>

      </tr>



      <tr>

          <td align="left"><input type="submit" name="submit" value="Submit Request"></td>

          <td>&nbsp;</td>

      </tr>



      </table>

</form>

编辑2: 谢谢。使用此 repo中的文档和代码,我创建了一个名为 new_ticket.php 的新文件,其中包含以下内容:

<?php

if($_POST['action'] == 'call_this') {
    require_once 'RequestTracker.php';
    $url = "www.test.com/rt/REST/1.0/";
    $user = "user";
    $pass = "password";

    $rt = new RequestTracker($url, $user, $pass);

    $content = array(
        'Queue'=>'9',
        'Requestor'=>'test@example.com',
        'Subject'=>'Lorem Ipsum',
        'Text'=>'dolor sit amet'
    );
$response = $rt->createTicket($content);
print_r($response);
}
?>

我还从同一个 Github 存储库中复制了 RequestTracker.php。

在表单所在的文件中,我添加了以下脚本并将 create_ticket() 作为操作添加到提交按钮的 onclick 属性中。但这似乎不起作用。我尝试将某些内容记录到控制台以查看代码有多远, create_ticket() 函数被正确调用,但 $.ajax({ ... 上面的任何内容都不会出现在控制台上。我还尝试放一些控制台登录我的 new_ticket.php 文件,但这也没有记录任何内容,那么我做错了什么?

<script>
 function create_ticket() {
      $.ajax({
        url:"new_ticket.php", //the page containing php script
        type: "POST", //request type
        data:{action:'call_this'},
        success:function(result){
         alert(result);
       }
     });
 }
</script>

PS:我使用 ajax 是因为我需要在点击时运行 PHP 代码,而这不能像在 Javascript 中那样直接完成。

4

1 回答 1

0

可能最简单的方法是查看Request Tracker wiki上 REST 文档中的 PHP 示例。您没有提及您正在使用的 RT 版本,但 REST 接口已经稳定,因此这应该适用于大多数版本。

于 2015-12-21T13:45:10.983 回答