我正在处理一个将数据插入数据库但不使用按钮提交的表单。相反,一个ahref标签,其样式用作按钮。我无法获得正确的代码。这是我的代码: HTML:
<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
答案 0 :(得分: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:您可以通过程序提交表单,但不必