2016-11-28 246 views
-1

我很難嘗試將php代碼集成到我在wordpress上創建的表單中。我嘗試了3個不同的PHP插件無濟於事。這裏是html和css。有沒有人有任何想法,我應該把WordPress的WordPress,我應該如何通過形式中的「行動」正確地稱呼它?如何將php添加到wordpress表單

CSS:

<style> 
    input[type=text], input[type=number], select[name=province]{ font-family: arial; width:100%; 
    border: 1px solid #E5E5E5; padding: 10px 20px;} 
input[name=ffirstname] {width:49%; margin-right:1%; } 
input[name=lastname] {width:49%; margin-left:1%; } 
input[name=address] {width:65.6667%; margin-right:1%; } 
input[name=unit] {width:32.3337%; margin-left:1%; } 
input[name=city] {width:49%; margin-right:1%; } 
select[name=province] {width:24%; margin-left:1%;} 
input[name=postal] {width:24%; margin-left:1%; } 
input[name=email] {width:49%; margin-right:1%; } 
input[name=phone] {width:49%; margin-left:1%;} 
input[class=submit] { 
    background-color: #f05a28; 
    color: white; 
    padding: 8px 20px; 
    margin-left: 85%; 
    border-radius: 4px; 
    border: none; !important; 
    outline: none; !important ; 
    cursor: pointer; 
    box-shadow: none; !important; 
} 
</style> 

HTML:

<form name"infoform" method="post" action="form-to-email.php" > 
    <br><input type="text" name="ffirstname" placeholder="First Name"/><input type="text" name="lastname" placeholder="Last Name"/> 
    <br><br><input type="text" name="address" placeholder="Address"/><input type="text" name="unit" placeholder="Unit"/></br> 
    <br><input type="text" name="city" placeholder="City"/><select name="province" form="form1"> 
<option value="ab">AB</option> 
    <option value="BC">BC</option> 
    <option value="BC">MB</option> 
    <option value="NB">NB</option> 
<option value="NL">NL</option> 
<option value="NS">NS</option> 
<option value="ON">ON</option> 
<option value="PE">PE</option> 
<option value="QC">QC</option> 
<option value="SK">SK</option> 
</select><input type="text" name="postal" placeholder="Postal Code"/></br> 
<br><input type="text" name="country" placeholder="Country"/></br> 
<Br><input type="text" name="email" placeholder="Email"/><input type="number" name="phone" placeholder="Phone#"/></br> 
<br> <br><input type="submit" value="Next"/> 

PHP:

<?php 

$name = $_POST['ffirstname']; 
$email = $_POST['lastname']; 
$message = $_POST['address']; 

$to = "[email protected]"; 
$subject = "My contact form"; 
$body = " You have received a new"; 
mail($to,$subject,$body); 

?> 

回答

0

我通常使用footer.php只是身體的結束標記前添加JS和PHP代碼, 。但如果這不起作用,請嘗試使用header.php。也許你的代碼需要在結束之前被包含。 您可以在頁面或帖子內放入HTML部分,而不會出現問題。

如果您想使用插件,您可以使用Contact form 7輕鬆做到這一點。

查看herehere的教程。

更新 - 添加PHP代碼到WordPress:

您可以添加<?php標籤之間的代碼,所以打開標籤

<?php 

//PHP Code here 

?> 

這樣裏面的header.phpfooter.phppage.php和編寫代碼你會在你網站的每個Wordpress頁面上看到這個代碼。

頁眉和頁腳始終包含在所有頁面上。 如果你只需要它的文章或網頁使用page.phppost.php

+0

我不想使用聯繫人7或任何其他插件,我希望能夠以非常特別設置表的格式。如何將php放入wordpress的頁眉或頁腳? –

+0

我更新了答案。 –

+0

我在哪裏可以找到這些PHP文件完全在WordPress?我是否通過cPanel並在文件夾中找到它?如果是的話,它將位於何處。 –

0

如果您想對PHP的WordPress的頁面中放置控制,然後看看page templates。您可以創建一個表單頁面模板,該模板具有表單和必要的php來處理它,然後將其應用於給定的URL,例如/sign-up。這樣你就不必將表格代碼弄亂了每個頁面的header.phpfooter.php

對於表單本身,您可以自己創建它post,然後檢查當使用isset($_POST['field_name'])加載頁面時是否設置表單變量。如果他們在場,那麼你可以假設表格已經發布,因此你需要發送電子郵件,否則如果他們不存在,那麼你可以假設你需要顯示錶格。

例如

<?php if(isset($_POST['ffirstname'])){ 
    // Do some appropriate sanitization for your use case 
    $ffirstname = htmlspecialchars(trim($_POST['ffirstname'])); 
    //.. repeat for other fields, and then send email 
    // could drop out of php here and show a thank you message 
    ?> 
    <p>Thank you for submitting the form</p> 
    <?php 
} else { 
    // $_POST['ffirstname'] not set, so show form ?> 
    <form method="post" action=""> 
     <input type="text" name="ffirstname" /> 
     <input type="submit" value="Submit" /> 
    </form> 
<?php } ?> 

如果你要掛鉤到WordPress了這一點,那麼做到這一點的方法之一是它的價值在functions.php文件中添加一個隱藏的輸入稱爲動作,並設置爲一個函數的名稱。您還需要添加一些javascript,以便將ajax提交給可以執行邏輯和電子郵件發送的功能:

// Form Page 
<form method="post" action="" id="emailForm"> 
    <input type="text" name="ffirstname" /> 
    <input type="hidden" name="action" value="sendEmail" /> 
    <input type="submit" value="Submit" /> 
</form> 

// Somewhere further down the same page as the form 
<script type="text/javascript"> 
    jQuery('#emailForm').on('submit', function() { 
     e.preventDefault(); 
     jQuery.ajax({ 
      type: 'POST', 
      url: '/wp-admin/admin-ajax.php', 
      data: jQuery('#emailForm').serialize(), 
      success: function(data) { 
       // Perhaps some UI update here to let the user know 
       // the submission was successful 
      }, 
      error: function(error) { 
       // Do something with the error 
      } 
     }); 
    }); 
</script> 

然後在你的主題functions.php文件:

// Functions.php 
function sendEmail(){ 
    // Do some appropriate sanitization for your use case 
    $ffirstname = htmlspecialchars(trim($_POST['ffirstname'])); 
    //.. repeat for other fields as necessary, and then send email 
    return wp_mail($recipient, $subject, $body, $headers); 
} 
add_action('wp_ajax_sendEmail', 'sendEmail'); 
相關問題