在我的PHP應用程序中,我使用AJAX以便在div中顯示搜索結果,而無需刷新頁面。input type =「image」在使用AJAX的PHP中不起作用
這裏是我的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"
這應該像行爲一個提交按鈕,這樣當它被點擊時,分配給input type="hidden"
的用戶名將被髮布到文件"profile/exp.php"
。
但結果我的意思是輸入類型=「隱藏」的價值沒有得到發佈或只有blaNK值發佈。
這裏的輪廓/ exp.php:
<?php
session_start();
ob_start();
$showpro=$_POST['pro_username'];
echo $showpro;
?>
它應該即用戶名顯示的結果,但它不顯示。我找不到答案爲什麼;我早些時候在另一張專輯創作應用程序中使用了此方法,但它很有效。我的搜索查詢和過程是完美的,否則它不會顯示結果,但不知道爲什麼輸入類型=「圖像」不起作用。同樣,我也嘗試了一個提交按鈕,你可以看到,但提交按鈕也不起作用。
更新
這下面的行不工作,即輸入型=「圖像」,並輸入類型=「提交」
$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());
我很感謝您找到自己的答案併發布它。 HOwever,你能否詳細說明......「我在標籤下添加標籤,做了一件錯誤的事情,它需要在標籤下添加」Im現在卡住了.. :) – bart2puck 2014-01-21 21:15:05