2016-11-18 128 views
-1

此表單可以使用,但不能在Wordpress中使用。我是新來的Wordpress,所以我不知道該怎麼做。我創建了4個文件:提交按鈕在Wordpress中不可用

:在我創建的形式
  • contact.php
  • validator.js
  • contact.js
  • 這裏是表單代碼

    • <form class="form-horizontal" id="contact-form" method="post" action="contact.php" role="form"> 
                 <div class="messages"></div> 
      
                 <div class="controls"> 
      
                  <div class="col-lg-12"> 
                   <div class="form-group"> 
                    <input id="form_name" type="text" name="name" class="form-control" placeholder="Enter your full name*" required="required" data-error="Full name is required."> 
                    <div class="help-block with-errors"></div> 
                   </div> 
                  </div> 
      
                  <div class="col-lg-12"> 
                   <div class="form-group"> 
                    <input id="form_email" type="email" name="email" class="form-control" placeholder="Enter your email address*" required="required" data-error="Valid email is required."> 
                    <div class="help-block with-errors"></div> 
                   </div> 
                  </div> 
      
                  <div class="col-lg-12"> 
                   <div class="form-group"> 
                    <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Enter your phone number"> 
                    <input type="submit" class="btn modal-btn pull-right" value="send!" /> 
                   </div> 
                  </div> 
                 </div> 
                </form> 
      

      這是contact.php中的代碼:

      <?php 
      // configure 
      $from = 'codedstyles.com <[email protected]>'; 
      $sendTo = 'Contact Form admin <[email protected]>'; 
      $subject = 'Hi! You have a new message from a lead'; 
      $fields = array('name' => 'Name', 'phone' => 'Phone', 'email' => 'Email'); 
      // array variable name => Text to appear in email 
      $okMessage = 'Thank you! We will get in touch with you shortly.'; 
      $errorMessage = 'There was an error while submitting the form. Please try again later'; 
      // Send code 
      try 
      { 
      $emailText = "You have a message to respond\n=============================\n"; 
      
      foreach ($_POST as $key => $value) { 
      
          if (isset($fields[$key])) { 
           $emailText .= "$fields[$key]: $value\n"; 
          } 
      } 
      
      mail($sendTo, $subject, $emailText, "From: " . $from); 
      
      $responseArray = array('type' => 'success', 'message' => $okMessage); 
      } 
      catch (\Exception $e) 
      { 
      $responseArray = array('type' => 'danger', 'message' => $errorMessage); 
      } 
      
      if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
      $encoded = json_encode($responseArray); 
      
      header('Content-Type: application/json'); 
      
      echo $encoded; 
      } 
      else { 
          echo $responseArray['message']; 
      } 
      
    +0

    那怎麼包括JS-文件? 「不工作」在細節上有點模糊。不提交?它沒有js的工作嗎?你有任何控制檯錯誤?任何在Apache/PHP日誌? – junkfoodjunkie

    +0

    您是否在瀏覽器控制檯中看到任何錯誤? – Manikiran

    +0

    表單上的所有內容都適用,但提交按鈕。我點擊並沒有任何反應。我發現它與添加PHP代碼的行爲有關,但我只是學習PHP,所以我不知道如何使這項工作......?

    contact.php文件位於根目錄中。 – sanlorena

    回答

    0
    action="contact.php" 
    

    我認爲這個問題是contact.php位置它看起來像它可能無法找到PHP文件。

    在處理WordPress時,您需要使用絕對路徑。

    根據你上傳你的php文件的位置,WordPress有幾個函數可以直接訪問諸如WordPress根目錄或插件目錄或主題目錄。

    <?php $path = get_home_path(); ?> // root directory 
    <?php get_plugins($plugin_folder) ?> // plugin directory 
    <?php echo get_template_directory(); ?> // theme directory 
    

    如果您需要了解更多信息有關這些功能檢出 WordPress Codex

    +0

    contact.php文件位於根目錄中,但我應該如何將php代碼添加到操作中?我認爲以下,但它不起作用:」role =「形式「> – sanlorena