首先,我将显示代码。这是两页,第一页我们称之为“form.php”,第二页我们称之为“display.php”。
表单.PHP
class display {
var $host;
var $username;
var $password;
var $table;
public function display_public() {
$q = "SELECT * FROM agend_items ORDER BY created DESC LIMIT 3";
$r = mysql_query($q);
if ( $r !== false && mysql_num_rows($r) > 0 ) {
while ( $a = mysql_fetch_assoc($r) ) {
$date = stripslashes($a['date']);
$bodytext = stripslashes($a['bodytext']);
$time = stripslashes($a['time']);
$entry_display .= <<<ENTRY_DISPLAY
<h1>00:59</h1>
<div class="post" name="post" >
<h2 name="date">
$date
</h2>
<h3 class="time">
$time
</h3>
<p id="agenda">
$bodytext
</p>
</div>
ENTRY_DISPLAY;
}
} else {
$entry_display = <<<ENTRY_DISPLAY
<h2> This Page Is Under Construction </h2>
<p>
No entries have been made on this page.
Please check back soon, or click the
link below to add an entry!
</p>
ENTRY_DISPLAY;
}
$entry_display .= <<<ADMIN_OPTION
<p class="admin_link">
<a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
</p>
ADMIN_OPTION;
return $entry_display;
}
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" method="post">
<label for="date">Date:</label><br />
<input name="date" id="date" type="date" maxlength="8" />
<div class="clear"></div>
<label for="time">Time:</label><br />
<input name="time" id="time" type="time" maxlength="8" />
<div class="clear"></div>
<label for="bodytext">Body Text:</label><br />
<textarea name="bodytext" id="bodytext"></textarea>
<div class="clear"></div>
<input type="submit" value="Input Agenda Item" />
</form>
<br />
<a href="display.php">Back to Home</a>
ADMIN_FORM;
}
public function write($p) {
if ( $_POST['date'] )
$date = mysql_real_escape_string($_POST['date']);
if ( $_POST['time'] )
$time = mysql_real_escape_string($_POST['time']);
if ( $_POST['bodytext'])
$bodytext = mysql_real_escape_string($_POST['bodytext']);
if ( $date && $bodytext && $time ) {
$created = time();
$sql = "INSERT INTO agend_items VALUES('$date','$bodytext','$created','$time')";
return mysql_query($sql);
} else {
return false;
}
}
public function connect() {
mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
return $this->buildDB();
}
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS agend_items (
date VARCHAR(12),
bodytext TEXT,
created VARCHAR(100),
time VARCHAR(8)
)
MySQL_QUERY;
return mysql_query($sql);
}
}
?>
显示.PHP
$(document).ready(function(){
$('button').mousedown(function(){
if ($('h3').text() == "00:59") {
alert("Yup"); return true;}
else { alert("Nope");return false;};
});
});
</script>
</head>
<body>
<?php
include_once('_class/form.php');
$obj = new form();
$obj->host = 'localhost';
$obj->username = '*****';
$obj->password = '*****';
$obj->table = 'agend_items';
$obj->connect();
if ( $_POST )
$obj->write($_POST);
echo ( $_GET['admin'] == 1 ) ? $obj->display_admin() : $obj->display_public();
?>
<?php
setlocale(LC_ALL,"hu_HU.UTF8");
echo(strftime("%Y. %B %d. %A. %X %Z"));
?>
<button>Check Work</button>
输出为: 00:59 (h1) 2013-05-24 (h2) 00:59 (h3) 这是文本 (p)
我遇到的问题是,如果我运行 jquery 脚本,它总是会在使用 $time、$date、$bodytext 创建的 h2、h3、p 上返回 false。
如果在 h1 中我只是简单地告诉 php h1 是“00:59”,则代码可以完美执行。
如果在 h3 中输出的时间是 00:59,我使用 jquery 搜索它每次都返回 false,如果我搜索日期或正文文本也是如此。
如果代码搜索由 php 变量 $time、$date、$bodytext 之一输出的文本,似乎每次都会失败。我无法弄清楚为什么它无法找到这个。DOM 元素都应该在那里找到..
您可以忽略 h1,它仅被插入以检查 jquery 脚本是否以最简单的形式工作,它确实如此。仅当针对使用从数据库中获取的信息创建的元素文本时