2013-08-31 82 views
-2

我正在處理將插入數據到數據庫但不使用按鈕提交的表單。取而代之的是一個樣式化爲用作按鈕的ahref標籤。我無法獲得正確的代碼。這裏是我的代碼: HTML:ahref標籤按鈕將數據插入數據庫

<form name="eventform" id="eventform" method="get"> 
    <p> Title: <input type="text" name="title" id="title" required 
         pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,80}" 
         title="Please enter valid title of you event."/></p> 
    <p> Where: <input type="text" name="place" id="place" required 
         pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,100}"/></p> 
    <p>When: <input type="date" name="date" id="date" required/></p> 
    <p>People Involved: <input type="text" name="people" id="people" required 
           pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,100}"/></p> 
    <p>Content:</p> 
    <textarea rows="4" cols="50" required name="content"></textarea> 
    <a href="events.php" id="btnend">Cancel</a> 
    <a href="events.php?/add-event/success" id="btnfinish" 
     name="btnfinish">Done</a> 
</form> 

PHP:

if (isset($_GET['eventform'])) { 
    $title = $_POST['title']; 
    $place = $_POST['place']; 
    $date = $_POST['date']; 
    $people = $_POST['people']; 
    $content = $_POST['content']; 
    $object->addEvent($title, $place, $date, $people, $content); 
} 

PHP在其他文件:

<?php 
class Dbother 
{ 
    private $host = 'localhost'; 
    private $user = 'root'; 
    private $password = ''; 
    private $dbname = 'pcnl'; 
    private $conn = ''; 

    function connect() 
    { 
     $this->conn = mysql_connect(
      $this->host, $this->user, $this->password 
     ) or die (mysql_error()); 
     mysql_select_db($this->dbname, $this->conn) or die(mysql_error()); 
    } 

    //end of connect 
    function addEvent($title, $place, $date, $people, $content) 
    { 
     $sql = " 
      INSERT INTO tbl_event 
      VALUES 
       ('$title', '$place', '$date', '$people', '$content', null, null, null) 
     "; 
     mysql_query($sql) or die (mysql_error()); 
     mysql_close(); 
    }//end of academic 
+1

我的意思是期望什麼?你可以提交表單而不用提交按鈕嗎?爲什麼不能設計提交輸入標籤? – meda

+0

像@meda說的,除非你使用一些javascript,否則你不能輕易地提交一個帶有鏈接的表單。在這種情況下,您可以使用我推薦的ajax。除非我不瞭解這個問題,否則你應該真正設計你的聯繫。 –

+0

啊,我很抱歉,我只是想,如果有可能的話。好吧,至少我現在知道了:)謝謝 – user2706335

回答

0

你應該只是樣式輸入標籤

<form name="eventform" id="eventform" method="get" action="events.php?/add-event/success"> 
     <p> Title:   <input type="text" name="title" id="title" required pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,80}" title="Please enter valid title of you event."/></p> 
     <p> Where:  <input type="text" name="place" id="place" required pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,100}"/></p> 
     <p>When:   <input type="date" name="date" id="date" required /></p> 
     <p>People Involved: <input type="text" name="people" id="people" required pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,100}"/></p> 
     <p>Content:</p> 
     <textarea rows="4" cols="50" required name="content"></textarea>  
     <a href="events.php" id="btnend">Cancel</a> 
     <input id="btnfinish" type="submit" value="Done"> 
</form> 

PS:你可以提交表格programtica lly但你不必

+0

非常感謝你@meda,我會採納你的建議:D – user2706335

+0

接受我的問題,如果它對你有幫助;) – meda

+0

你也可以使用'