2014-12-11 72 views
1

我是新來的PHP。我正在嘗試按鈕提交保存表單。但是它在頁面呈現時保存表單。我試過這個。如何在按鈕中提交表單保存在php

PHP代碼

<?php 
    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 
     $this->model_catalog_contactus->saveContact($this->request->post); 
    } 
?> 

HTML代碼

<form action="/common/contactus" method="post" class="smart-green"> 
    <h2>Contact Us</h2> 
    <p><label><span>Name : 
      </span><input id="name" type="text" name="contactname" placeholder="Your Name"> 
     </label><label><span>Email :</span> 
      <input id="email" type="text" name="email" placeholder="Your Email"> 
     </label> 
     <label><span>Mobile :</span> 
      <input id="email" type="text" name="mobile" placeholder="Your Mobile Number"> 
     </label> 
     <label><span>Message :</span> 
      <textarea rows="" cols="" placeholder="Your message here" name="message"></textarea> 
     </label> 
     <label><span>&nbsp;</span> 
      <input type="submit" class="btn btn-primary" value="save" style="margin-top:15px;" name="submit-btn"/> 
     </label></p> 
</form> 
+0

發表您的HTML。希望它來自Opencart – ashokhein 2014-12-11 05:24:19

+0

是的,它來自opencart和MVC框架 – 2014-12-11 05:45:55

回答

1

只要確保提交表單,像這樣:

if (!empty($_POST)) { 

    if ($this->validate()) { 
     $this->model_catalog_contactus->saveContact($this->request->post); 
    } 
} 
+0

非常感謝你,現在它的工作 – 2014-12-11 05:52:42

+0

OpenCart使用'$ this-> request-> post'而不是'$ _POST',你的兩個嵌套'if'條件應該被捆綁成一個'if'使用'&&'... – shadyyx 2014-12-11 11:43:31

+0

@shady是的,但是爲了使它更清楚,最好爲**初學者**分開步驟,然後開始考慮改進, $ this-> request-> post'或'$ _POST'並不是什麼大事,因爲開放式車的代碼庫本身很糟糕。 – Yang 2014-12-11 11:47:32