如何将数据传回索引文件?

时间:2014-05-13 03:44:31

标签: javascript php mysql ajax

我有一个index.php文件来包含我网站的布局。在索引文件中,我有一个搜索表单,它将数据发送到另一个php文件进行处理(action =“search.php”)。我的问题是:我可以选择适合用户关键字的数据并输出,但输出不是布局,它只是白色背景上的文字。我需要知道的是:我如何将我选择的数据传递回index.php,以便它可以进行布局?我已经尝试过window.location并包含但它没有用。这是我的代码:

<?php
$search=$_GET['search'];// name of the text field is "search"
include('connectdb.php');
$result=mysql_query("SELECT * FROM article WHERE header LIKE '%".$search."%'");
while($row=mysql_fetch_array($result))
{?>        
 <?php   echo $row['header'];
echo $row['content'];
}
   ?>   

3 个答案:

答案 0 :(得分:0)

使用php Header函数返回index.php。这只是一个演示。你只需要想法并设计你想要的东西。

<?php
$search=$_GET['search'];// name of the text field is "search"
include('connectdb.php');
$result=mysql_query("SELECT * FROM article WHERE header LIKE '%".$search."%'");
while($row=mysql_fetch_array($result))
{?>        

header("index.php?header=".$row['header']."&content=".$row['content']);
}
?>  

然后在索引文件中捕获这些详细信息

<?php
$header=$_GET['header'];
$content=$_GET['content'];
?>

答案 1 :(得分:0)

你可以做这样的事情:

var text = $(this).val();
        $.ajax({
        url: "search.php",
        type: "POST",
        data: {search: text},
        datatype: 'json',
        success: function(data){
        if(data){
        //here you are parsing json object
        var data = JSON.parse(data);
        //0: Object
    //header: "blablabla"
    //content: "blablabla"
   //1: Object
        //header: "wwwwwws"
        //content: "wewesssss"
        //length will be equal of results amount
        var length = data.length;
        var li = '';
        //here you are making li
        for(var i = 0; i < length; i++){
        li += '<li>';
        li += data[i]['header'] . data[i]['content'];
        li += '</li>';
        }
        //appending element in dom
        $('ul').html(li);
        }
        }
        });

这个ajax ......

$row = mysql_fetch_array($result);
$row = json_encode($row);
eco $row;
//this will return json object 
//[{"header":"blablabla","content":"blablabla"},{"header":"wwwwwws","content":"wewesssss"}] 

这是你的php

答案 2 :(得分:0)

这是一个简单的例子,你只需复制完全相同的index.php布局,并在search.php上实现它,如果你从index.php上的数据库中选择数据然后在search.php上做同样的查询

的style.css

.center_content{
 width:500px;
 height:300px;
 background:yellow;
 border:1px solid;
 }
 .right_column{
 width:200px;
 height:300px;
 background:Red;
 display:inline-block;
  }
  .right_column{
  width:200px;
  height:300px;
  background:green;
  display:inline-block;
   }

的index.php

<link type="text/css" rel="stylesheet" href="style.css">

<form method="get" action="send.php">
<input type="text" name="search"  placeholder="search here">
<input type="submit" value="submit">
</form>

<div class="left_column">blah blah</div>
<div class="center_content">
put whatever you want here
</div>
<div class="right_column">blah blah</div>

的search.php

<form method="get" action="">
<input type="text" name="search"  placeholder="search here">
<input type="submit" name="submit" value="submit">
</form>

<div class="left_column">blah blah</div>
<div class="center_content">
 <?php
if(isset($_GET['submit'])){
 $search=$_GET['search'];// name of the text field is "search"
include('connectdb.php');
$result=mysql_query("SELECT * FROM article WHERE header LIKE '%".$search."%'");
while($row=mysql_fetch_array($result))
{?>        
 <?php   echo $row['header'];
echo $row['content'];
}}

 ?> 
</div>
<div class="right_column">blah blah</div>