我正在处理将插入数据到数据库但不使用按钮提交的表单。取而代之的是一个样式化为用作按钮的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
我的意思是期望什么?你可以提交表单而不用提交按钮吗?为什么不能设计提交输入标签? – meda
像@meda说的,除非你使用一些javascript,否则你不能轻易地提交一个带有链接的表单。在这种情况下,您可以使用我推荐的ajax。除非我不了解这个问题,否则你应该真正设计你的联系。 –
啊,我很抱歉,我只是想,如果有可能的话。好吧,至少我现在知道了:)谢谢 – user2706335