【问题标题】:PHP get data from MySQL by clicking on Href link and showing results in the same urlPHP 通过单击 Href 链接并在同一 url 中显示结果从 MySQL 获取数据
【发布时间】:2015-06-14 19:53:40
【问题描述】:

我遇到了一种情况,我想点击一个链接并根据 $id 获取数据。我能够做到这一点,但我的问题是我希望结果显示在同一页面上而不是重定向到另一个页面

$con = dbConnect();
	$sql = $con->prepare("SELECT manuscript_id,authors,month,year,title,journal,pubmed_link FROM manuscript WHERE title like '%$term%'");
		$sql->execute();
		$sql->setFetchMode(PDO::FETCH_ASSOC);
//
		if ($sql->fetch() == 0){
		echo ('<h2>SORRY! No results found</h2>');
			}


		else {
		
		$x=1;		
		while ($row = $sql->fetch()){//three
				$manuscript_id = $row ['manuscript_id'];
				$author = $row ['authors'];
				$month = $row ['month'];
				$year = $row ['year'];
				$title = $row['title'];
				$journal = $row ['journal'];
				$pubmed_link = $row ['pubmed_link'];
		
		
		echo $x++;
		echo "<input type = 'checkbox' id='list' value='list'>";
		echo ('<a href="abstract.php?manuscript_id=' . $manuscript_id . '">' . $title . '</a>') . "</br>" . "\r\n" . $author . "</br>" . "\r\n" . "<u>" . $journal . "</u>" . "&nbsp" . $month . "\r\n" . "&nbsp" . $year . "\r\n" . "</br>" . "</br>";


		}

有没有办法避免重定向到“abstract.php”?

【问题讨论】:

  • 感谢 Fred,但在引入 Ajax 时寻求 php/html 解决方案可能意味着重写整个模块
  • 有人为您发布了答案。除了 iframe 之外,如果没有 JS/Ajax,我无法看到如何做到这一点。
  • 将其指向&lt;iframe&gt; - HTML 解决方案。
  • 感谢 Fred 的提醒...我是 Stackoverflow 上的绿角,但不是我无知的借口。将点标记为绿色

标签: php html


【解决方案1】:

您可以为此使用 javascript 和 jQuery。

<a href="abstract.php?manuscript_id=' . $manuscript_id . '" class="clickMe">
<div id="results"></div>

<script type="text/javascript">
$('a.clickMe').click(function(e){
   // Stop default click action in browser
   e.preventDefault();

   // Make ajax call
   $.ajax($(e.target).attr("href"), {
     cache:false,
     success:function(data){
         $("#results").html(data);
     }
   });
})
</script>

这样就可以了。 :)

【讨论】:

  • OP:"感谢 Fred,但寻求 php/html 解决方案作为 Ajax 的引入可能意味着重写整个模块 - Maina 4 分钟前" - $.ajax 仍然是"阿贾克斯”。
  • 这是唯一的方法,你也可以使用 json 来传输你的数据。这将花费你更少的字节,所以它会更快。 ;)
  • 谢谢马尔科..但仍然
  • 肯定有PHP/Html解决方案
  • 你可以试试 iframe 吗?
猜你喜欢
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多