使用Jquery来定位PHP输出的元素 [英] Using Jquery to target element outputted by PHP

查看:119
本文介绍了使用Jquery来定位PHP输出的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我会显示代码。这是两页,第一页我们称之为form.php第二页我们称之为display.php。






FORM.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 =postname =post>
< h2 name =date>
$ date
< / h2>
< h3 class =time>
$ time
< / h3>
< p id =议程>
$ bodytext
< / p>
< / div>

ENTRY_DISPLAY;
}
} else {
$ entry_display =<< ENTRY_DISPLAY

< h2>本页正在建设< / h2>
< p>
此页面上没有条目。
请稍后再回来查看,或点击下面的
链接添加一个条目!
< / p>

ENTRY_DISPLAY;
}
$ entry_display。=<<< ADMIN_OPTION

< p class =admin_link>
< a href ={$ _ SERVER ['PHP_SELF']}?admin = 1>添加新条目< / a>
< / p>

ADMIN_OPTION;

return $ entry_display;
}

public function display_admin(){
return<<< ADMIN_FORM

< form action ={$ _ SERVER [' PHP_SELF']}method =post>

< label for =date>日期:< / label>< br />
< input name =dateid =datetype =datemaxlength =8/>
< div class =clear>< / div>

< label for =time>时间:< / label>< br />
< input name =timeid =timetype =timemaxlength =8/>
< div class =clear>< / div>

< label for =bodytext>正文文本:< / label>< br />
< textarea name =bodytextid =bodytext>< / textarea>
< div class =clear>< / div>

< input type =submitvalue =输入议程项目/>
< / form>

< br />

< a href =display.php>返回首页< / 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 )或死(无法连接。mysql_error());
mysql_select_db($ this-> table)or die(Could not select database。mysql_error());

return $ this-> buildDB();
}

私有函数buildDB(){
$ sql =<<< MySQL_QUERY
CREATE TABLE IF NOT EXISTS agend_items(
date VARCHAR (12),
bodytext TEXT,
创建VARCHAR(100),
时间VARCHAR(8)

MySQL_QUERY;

返回mysql_query($ sql);
}

}

?>






DISPLAY.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>检查工作< / button>






输出是:
00: 59(h1)
2013-05-24(h2)
00:59(h3)
这是文本(p)






我遇到的问题是如果我运行jquery脚本,它将始终返回false,在h2,h3,p创建与$ time,$ date,$ body text。



如果在h1中,我只是简单地告诉php,h1是00:59,代码执行完美。



如果在h3中输出的时间是00:59,并且我使用jQuery来搜索它每次返回false,如果我搜索日期或bodytext,则相同。



似乎每次如果正在搜索由一个php变量$ time,$ date,$ bodytext输出的文本,代码将失败。我不知道为什么它找不到这个。 DOM元素都应该在那里找到..



你可以忽略h1,只是插入来检查jquery脚本是否以最简单的形式工作,它是做什么的。只有在通过使用从数据库获取的信息创建的元素文本时才是这样。

解决方案

尝试将每个元素放在一行,所以H2应该这样写成没有换行符:

 < h2 name =date> $ date< / H2> 

这可能有助于.text找到完整的日期字符串,而不会被额外的空格或行尾字符。



我还建议您使用console.log()查看实际看到的内容:

  console.log($('h2')。text()); 


First I'll display the code. It's 2 pages, the first page we'll call "form.php" the second page we'll call "display.php".


FORM.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);
  }

}

?>


DISPLAY.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>


The output is: 00:59 (h1) 2013-05-24 (h2) 00:59 (h3) This is text (p)


The issue I'm running into is if I run the jquery script, it will always return false on the h2, h3, p that were created with $time, $date, $bodytext.

If in the h1 I just simply tell the php the h1 is "00:59", the code executes perfectly.

If the time that was outputted in h3 is 00:59, and I use the jquery to search for that it returns false everytime, same if I search for the date or bodytext.

It seems like the code will fail every time if it's searching for the text outputted by one of the php variables $time, $date, $bodytext. I cannot figure out why it is unable to find this. The DOM elements should all be there for it to find..

You can ignore the h1, it was only inserted to check if the jquery script was working in the plainest form, which it does. It's only when targeting the elements text that was created by using the information taken from the database

解决方案

Try putting each element in one line, so H2 should be written like this without line breaks:

<h2 name="date">$date</h2>

This might help with .text finding exactly the date string without being tripped by extra spaces or end of line character.

I also suggest you use console.log() to see what it's actually seeing:

console.log( $('h2').text() );

这篇关于使用Jquery来定位PHP输出的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆