input type =“image”在使用AJAX的PHP中不起作用

时间:2012-03-24 16:56:38

标签: php ajax image types hidden

在我的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>

现在,如果您可以看到,搜索结果会显示在div id="search_profile result"的{​​{1}}内。

事实是,搜索结果显示为所需的方式,但现在 正如你所看到的,在search.php中我打印结果以及我正在使用search_profile.php,它应该像提交按钮一样,以便在点击它时,分配给的用户名input type="image"将发布到文件input type="hidden"

但结果我的意思是输入类型=“隐藏”的值没有被发布,或者只发布了blaNK值。

这是profile / exp.php:

"profile/exp.php"

它应该显示结果,即用户名,但不显示。我找不到答案为什么;我之前在另一个专辑创建应用程序中使用过这种方法,但它有效。我的搜索查询和程序是完美的,否则它不会显示结果,但不知道为什么输入类型=“图像”不起作用。同样地,我也可以看到提交按钮,但是提交按钮也无效。

更新

以下行不起作用,即输入类型=“图像”和输入类型=“提交”

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

1 个答案:

答案 0 :(得分:0)

我希望得到答案,我自己找到了......

我通过在标签下添加标签来做错了,这正在创建问题。 它需要添加到标签下。所以我将代码更改为以下内容: -

  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>";
    }