输入类型=“;图像“;在使用AJAX的PHP中无法工作


input type="image" is not working in PHP using AJAX

在我的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">&nbsp 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">&nbsp  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">&nbsp  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">&nbsp  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">&nbsp  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">&nbsp 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">&nbsp  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">&nbsp
         </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>

现在,如果您可以看到,搜索结果将显示在search_profile.phpdiv id="search_profile result"内部。

事实上,搜索结果是按照期望的方式显示的,但现在正如你所看到的,在search.php中,我正在打印结果,同时我正在使用一个input type="image",它应该像一个提交按钮一样,这样当它被点击时,分配给input type="hidden"的用户名将被发布到文件"profile/exp.php"中。

但结果我的意思是输入type="hidden"的值没有被发布,或者只发布了blaNK值。

这是配置文件/exp.php:

<?php
session_start();
ob_start();
$showpro=$_POST['pro_username'];
echo $showpro;
?>

它应该显示结果,即用户名,但不显示。我找不到原因的答案;我之前在另一个相册cretain应用程序中使用过这种方法,但它确实有效。我的搜索查询和过程是完美的,否则它不会显示结果,但不知道为什么输入type="image"不起作用。类似地,我也尝试了一个提交按钮,正如你所看到的,但提交按钮也不起作用。

更新

以下几行不起作用,即输入类型="image"和输入类型="submit"

$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());

我希望得到一个答案,但我自己找到了。。。

我做了一件错误的事情,在标签下面添加了标签,这就造成了问题。它需要添加到标签下。所以我把我的代码改成了以下代码:-

  echo "<table border='0' cellspacing='0' cellpadding='0' width='650px' align='center'>";
       while($row=mysql_fetch_array($result_search))
          {
             echo "<tr>";
             echo "<td>";
             echo "<form method='post' action='profile/id.php' name='showid' id='showid'>
             <input name='pro_username' name='pro_username' type='hidden' value='";echo $row['username']; echo "'/>
             <input type='image' src='" . $row['propic'] ."' style='width:30px;'/></form></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>";
    }