【发布时间】:2012-04-08 20:41:31
【问题描述】:
在我的 PHP 应用程序中,我使用 AJAX 以便在 div 中显示搜索结果,而无需刷新页面。
这是我的 search_profile.php
<?php
session_start();
ob_start();
include('connection.php');
$query_religion="SELECT DISTINCT religion FROM religion_caste_table";
$result_religion = mysql_query($query_religion, $con);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="AJAX/religion_caste.js"></script>
</head>
<body>
<div id="search">
<table width="650px" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="460px">
<div id="search_box">
<h2>Search With Filter</h2>
<form method="post" action="javascript:search_profile(document.getElementById('search_form'));"
id="search_form" name="search_form">
<table cellpadding="0" width="460px" cellspacing="0" border="0" align="center">
<tr>
<td width="11px" height="5"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="12px"/>
</tr>
<tr>
<td align="center" width="11px"/>
<td align="center" width="100px">
<label for="search">  Searching:</label>
</td>
<td align="center" width="11px"/>
<td align="center" colspan="5" width="322" bgcolor="">
<select name="search_gender" id="search_gender" style="width:322px;">
<option value="Female">Bride</option>
<option value="Male">Groom</option>
</select>
</td>
<td align="center" width="12"/>
</tr>
<tr>
<td width="11px" height="5px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="12px"/>
</tr>
<tr>
<td align="center" width="11px"/>
<td align="center" width="100px">
<label for="from_age">  From:</label>
</td>
<td align="center" width="11px"/>
<td align="center" width="100px">
<select name="from_age" id="from_age" class="dropdown" style="width: 100px">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
</select>
</td>
<td align="center" width="11px"/>
<td align="center" width="100px">
<label for="to_age">  To:</label>
<td align="center" width="11px"/>
<td align="center" width="100px">
<select name="to_age" id="to_age" class="dropdown" style="width: 100px">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
</select>
<td align="center" width="12px"/>
</tr>
<tr>
<td width="11px" height="5px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="12px"/>
</tr>
<tr>
<td align="center" width="11px"/>
<td align="center" width="100px">
<label for="religion">  Religion:</label>
</td>
<td align="center" width="11px"/>
<td align="center" width="100px">
<select name="religion" id="religion" class="dropdown"
onChange="showcaste(this.value)" style="width: 100px">
<?php
while($q_rel_data = mysql_fetch_array($result_religion))
{?>
<option value="<?php echo $q_rel_data[0]; ?>"<?php if($row_social[religion] == $q_rel_data[0]) echo ' selected="selected"'; ?>>
<?php echo $q_rel_data[0]; ?>
</option>
<?php }?>
</select>
</td>
<td align="center" width="11px"/>
<td align="center" width="100px">
<label for="caste">  Caste:</label>
<td align="center" width="11px"/>
<td align="center" width="100px" >
<select name="caste" id="caste" class="dropdown" style="width: 100px">
</select>
<td align="center" width="12px"/>
</tr>
<tr>
<td width="11px" height="4px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="12px"/>
</tr>
<tr>
<td align="center" width="11px"/>
<td align="center" width="100px">
<label for="community">  Comunity:</label>
</td>
<td align="center" width="11px"/>
<td align="center" width="100px">
<select name="community" id="community" class="dropdown" style="width: 100px">
<?php
$community=array("Assami","Bengali","Bihari","Gujarati","Marwari","Rajasthani","Tamil","Telugu");
foreach ($community as $value)
{?>
<option value="<?php echo $value; ?>"<?php if($row_social[community] == $value) echo ' selected="selected"'; ?>><?php echo $value; ?>
</option>
<?php } ?>
</select>
</td>
<td align="center" width="11px"/>
<td align="center" width="100px">
<label for="occupation">  Occupation:</label>
<td align="center" width="11px"/>
<td align="center" width="100px">
<select name="occupation" id="occupation" style="width: 100px">
<?php
$occupation=array("Doctor","Engineer","Dentist","Civil Service","Military","Police","Govt. Service","Judiciary","Business","Private Job", "IT Professional","Teacher","Faculty","Sportsperson");
foreach ($occupation as $value)
{?>
<option value="<?php echo $value; ?>"<?php if($row_professional[occupation] == $value) echo ' selected="selected"'; ?>><?php echo $value; ?>
</option>
<?php } ?>
</select>
<td align="center" width="12px"/>
</tr>
<tr>
<td width="11px" height="4px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="12px"/>
</tr>
<tr>
<td align="center" colspan="9">
<input type="submit" name="Search" value="Search" class="button" style="width: 250px;"/>
</td>
</tr>
<tr>
<td width="11px" height="4px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="11px"/>
<td width="100px"/>
<td width="12px"/>
</tr>
</table>
</form>
</div>
</td>
<td width="190px">
<div id="search_by_name">
<h2>Search By Name</h2>
<form method="post" action="">
<table width="190px" cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td align="center">Enter Name</td>
<td align="center">
<input type="text" id="name_to_search" name="name_to_search" class="textfield"
style="width: 100px;"/>
</td>
</tr>
<tr>
<td colspan="2"> 
</td>
<tr>
<td align="center" colspan="2">
<input type="submit" id="search_pro" name="search_pro" class="button"
style="width: 100px;" align="center" />
</td>
</tr>
</table>
</form>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div id="search_profile_result">
</div>
</td>
</tr>
</table>
</div>
</body>
</html>
现在这是我的 ajax 脚本 AjaxSearchProfile.js
var http_request = false;
function makePOSTRequestProfile(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertSearchContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertSearchContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('search_profile_result').innerHTML = result;
} else {
alert(http_request.status);
}
}
}
function search_profile(obj)
{
alert("huhu");
var poststr = "gender=" + encodeURI( document.getElementById("search_gender").value ) +
"&from_age=" + encodeURI( document.getElementById("from_age").value ) +
"&to_age=" + encodeURI( document.getElementById("to_age").value ) +
"&religion=" + encodeURI( document.getElementById("religion").value ) +
"&caste=" + encodeURI( document.getElementById("caste").value ) +
"&community=" + encodeURI( document.getElementById("community").value ) +
"&occupation=" + encodeURI( document.getElementById("occupation").value );
alert(poststr);
makePOSTRequestProfile('search.php', poststr);
}
以及最终处理查询的 search.php..
<?php
include('connection.php');
$sex=$_POST['gender'];
$from_age=$_POST['from_age'];
$to_age=$_POST['to_age'];
$religion=$_POST['religion'];
$caste=$_POST['caste'];
$community=$_POST['community'];
$occupation=$_POST['occupation'];
$sql_search="SELECT t1.username, t1.name, t1.age, t1.dob, t1.propic, t2.religion, t2.caste, t3.education, t3.occupation
FROM candidate_register_table as t1
LEFT JOIN candidate_social_table as t2 ON
t1.username = t2.username and t2.caste = '$caste' and t2.religion ='$religion' and t2.community ='$community'
LEFT JOIN candidate_professional_table as t3 ON
t1.username = t3.username and t3.occupation = '$occupation' WHERE t1.sex = '$sex'";
$result_search=mysql_query($sql_search,$con);
if($result_search)
{
echo "<table border='0' cellspacing='0' cellpadding='0' width='650px' align='center'>";
while($row=mysql_fetch_array($result_search))
{
echo "<tr>";
echo "<form method='post' action='profile/exp.php' name='showid' id='showid'>";
echo "<td><input type='hidden' name='pro_username' value='" . $row['username'] . "'/></td>";
echo "<td><input type='image' src='" . $row['propic'] ."' style='width:30px;'/></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['username'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['name'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['religion'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['caste'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['occupation'] . " ::" . "</font></td>";
echo "<td><input type='submit' name='submit' id='submit'/></td>";
echo "</form>";
echo"</tr>";
}
echo "</table>";
}
else
die('Error: ' . mysql_error());
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
现在如果可以看到,搜索结果显示在div id="search_profile result" 内search_profile.php。
事实是,搜索结果显示为所需的方式,但现在
如您所见,在 search.php 中,我正在打印结果,并且我正在使用 input type="image",它应该像提交按钮一样,以便在单击它时,分配给 @ 的用户名987654327@ 将发布到文件"profile/exp.php"。
但我的意思是输入 type="hidden" 的值没有被发布,或者更确切地说只有 blaNK 值被发布。
这是配置文件/exp.php:
<?php
session_start();
ob_start();
$showpro=$_POST['pro_username'];
echo $showpro;
?>
它应该显示结果,即用户名,但它不显示。我找不到答案为什么;我之前在另一个相册创建应用程序中使用过这种方法,但它确实有效。我的搜索查询和过程是完美的,否则它不会显示结果,但不知道为什么输入 type="image" 不起作用。同样,如您所见,我也尝试了一个提交按钮,但该提交按钮也不起作用。
更新
以下行不起作用,即输入类型=“图像”和输入类型=“提交”
$result_search=mysql_query($sql_search,$con);
if($result_search)
{
echo "<table border='0' cellspacing='0' cellpadding='0' width='650px' align='center'>";
while($row=mysql_fetch_array($result_search))
{
echo "<tr>";
echo "<form method='post' action='profile/exp.php' name='showid' id='showid'>";
echo "<td><input type='hidden' name='pro_username' value='" . $row['username'] . "'/></td>";
echo "<td><input type='image' src='" . $row['propic'] ."' style='width:30px;'/></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['username'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['name'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['religion'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['caste'] . " ::" . "</font></td>";
echo "<td><FONT COLOR=blue FACE='Geneva, Arial' SIZE=2>" . $row['occupation'] . " ::" . "</font></td>";
echo "<td><input type='submit' name='submit' id='submit'/></td>";
echo "</form>";
echo"</tr>";
}
echo "</table>";
}
else
die('Error: ' . mysql_error());
【问题讨论】:
标签: php ajax image types hidden