2011-04-22 320 views
0

我有一些POST變量的問題,我看不出有什麼問題,所以我希望有人能夠發現哪些問題?PHP - 回顯POST變量

這是「簡單搜索」形式:

<div id ="form"> 
<form enctype="multipart/form-data" method="post" action="testdocs-db.php" name="search" id="search" class="search"> 
<input type="hidden" name="dosearch" value="true"> 
<table border=0> 
<tr> 
    <td class="label">File Type:</td> 
    <td> 
<?php  doSelectMultiple("filetype", $options_filetype, $filetype, ""); ?> 
    </td> 
</tr> 
<tr> 
    <td class="label">File Name:</td> 
    <td> 
    <input id="filename" name="filename" class="text" value="<?php echo($filename); ?>"> 
    </td> 
    <td>&nbsp;</td> 
</tr> 
<tr> 
    <td class="label">File Size:</td> 
    <td> 
    <input id="filesize_min" name="filesize_min" class="text" value="<?php echo($filesize_min); ?>"><div id="kb">(kb min)</div> 
    </td> 
    <td> 
    <input id="filesize_max" name="filesize_max" class="text" value="<?php echo($filesize_max); ?>"><div id="kb">(kb max)</div> 
    </td> 
</tr> 
<tr> 
    <td class="label"># Results:</td> 
    <td> 
    <input id="numresults" name="numresults" class="text" 
    <?php 
    if (strlen($numresults) >= 1) 
    { 
     echo ("value=\"$numresults\""); 
    } 
    else 
    { 
     echo ("value=\"10\""); 
    } 
    ?> > 
    </td> 
    <td>&nbsp;</td> 
</tr> 
<tr> 
    <td class="label">Order By:</td> 
    <td> 
<?php  doSelect("orderby", 1, $options_orderby, $orderby, ""); ?> 
    </td> 
    <td> 
<?php  doSelect("orderbyad", 1, $options_orderbyAD, $orderbyAD, ""); ?> 
    </td> 
</tr> 
<tr> 
    <td class="label">Output Format:</td> 
    <td> 
<?php  doSelect("outputformat", 1, $options_outputformat, $outputformat, ""); ?> 
    </td> 
    <td> 
<?php  doSelect("outputlocation", 1, $options_outputlocation, $outputLocation, ""); ?> 
    </td> 
</tr> 
<tr> 
    <td>&nbsp;</td> 
    <td><input type="submit" class="submit" value="Search"></td> 
    <td>&nbsp;</td> 
</tr> 

所以,如果我點擊提交按鈕,一切都很好,並顯示搜索值和形式記住的字段值等等你可以調整你的搜索條件,如果你想。

不過,我也有,我想用這樣做的另一個「高級搜索」的形式,但它並不適用於某些原因:

<div id="advancedsearch" class="advancedsearch"> 
<form enctype="multipart/form-data" class="advancedsearchform" action="testdocs-db.php" method="post"> 
<input type="hidden" name="advancedsearchsubmit" value="1"/> 
<input type="hidden" name="hiddenfiletype" id="hiddenfiletype"/> 
<input type="hidden" name="hiddenfilename" id="hiddenfilename"/> 
<input type="hidden" name="hiddenfilesize_min" id="hiddenfilesize_min"/> 
<input type="hidden" name="hiddenfilesize_max" id="hiddenfilesize_max"/> 
<input type="hidden" name="hiddennumresults" id="hiddennumresults"/> 
<input type="hidden" name="hiddenorderby" id="hiddenorderby"/> 
<input type="hidden" name="hiddenorderbyad" id="hiddenorderbyad"/> 
<input type="hidden" name="hiddenoutputformat" id="hiddenoutputformat"/> 
<input type="hidden" name="hiddenoutputlocation" id="hiddenoutputlocation"/> 

<script type="text/javascript"> 
$("#filetype").change(copyFiletype); 
$("#filename").change(copyFilename); 
$("#filesize_min").change(copyFileSizeMin); 
$("#filesize_max").change(copyFileSizeMax); 
$("#numresults").change(copyNumResults); 
$("#orderby").change(copyOrderBy); 
$("#orderbyad").change(copyOrderByAD); 
$("#outputformat").change(copyOutputFormat); 
$("#outputlocation").change(copyOutputLocation); 

function copyFiletype() 
{ 
    var valueToCopy=$("#filetype option:selected").val(); 
    $("#hiddenfiletype").val(valueToCopy); 
} 

function copyFilename() 
{ 
    var valueToCopy=$("#filename").val(); 
    $("#hiddenfilename").val(valueToCopy); 
} 

function copyFileSizeMin() 
{ 
    var valueToCopy=$("#filesize_min").val(); 
    $("#hiddenfilesize_min").val(valueToCopy); 
} 

function copyFileSizeMax() 
{ 
    var valueToCopy=$("#filesize_max").val(); 
    $("#hiddenfilesize_max").val(valueToCopy); 
} 

function copyNumResults() 
{ 
    var valueToCopy=$("#numresults").val(); 
    $("#hiddennumresults").val(valueToCopy); 
} 

function copyOrderBy() 
{ 
    var valueToCopy=$("#orderby option:selected").val(); 
    $("#hiddenorderby").val(valueToCopy); 
} 

function copyOrderByAD() 
{ 
    var valueToCopy=$("#orderbyad option:selected").val(); 
    $("#hiddenorderbyad").val(valueToCopy); 
} 

function copyOutputFormat() 
{ 
    var valueToCopy=$("#outputformat option:selected").val(); 
    $("#hiddenoutputformat").val(valueToCopy); 
} 

function copyOutputLocation() 
{ 
    var valueToCopy=$("#outputlocation option:selected").val(); 
    $("#hiddenoutputlocation").val(valueToCopy); 
} 
</script> 

<p> 
<input type="radio" name="andor" value="AND" checked /> match <span class="all">all</span> of these | 
match <span class="any">any</span> of these <input type="radio" name="andor" value="OR" /> 
</p> 

<div class="dropdown"> 
<select name="tags[]" class="tags"> 
<option value="tags" selected="selected">tags</option> 
<option value="agent">agent</option> 
<option value="extension">extension</option> 
</select> 

<select name="operands[]" class="operands"> 
<option>please select a tag</option> 
</select> 

<select name="values[]" class="values"> 
<option>please select a tag</option> 
</select> 

<img class="addButton" src="images/blank.gif" alt="add" onclick="addNew();"/> 
<img class="deleteButton" alt="delete" src="images/delete1.png" onclick="remove(this)" onmouseover="this.src='images/delete.png'" onmouseout="this.src='images/delete1.png'"/> 
<div class="clear"></div> 
</div> 

<div class="clonecontainer"></div> 

<div class="advancedsearchsubmit"><input type="submit" class="submit" value="Advanced Search"/></div> 
</form> 
</div> 

當高級搜索表單提交,變量設置如下:

// hidden input values from simple search 
    $filetype= doPost('hiddenfiletype', $filetype); 
    $filename= doPost('hiddenfilename', $filename); 
    $filesize_min= doPost('hiddenfilesize_min', $filesize_min); 
    $filesize_max= doPost('hiddenfilesize_max', $filesize_max); 
    $numresults= doPost('hiddennumresults', $numresults); 
    $orderby= doPost('hiddenorderby', $orderby); 
    $orderbyAD= doPost('hiddenorderbyad', $orderbyAD); 
    $outputformat= doPost('hiddenoutputformat', $outputformat); 
    $outputLocation= doPost('hiddenoutputlocation', $outputLocation); 

簡單的搜索變量同樣做,除非他們回聲出正確:

/* do the search */ 
    $filetype  = doPost('filetype', $filetype); 
    $filename  = doPost('filename', $filename); 
    $filesize_min = doPost('filesize_min', $filesize_min); 
    $filesize_max = doPost('filesize_max', $filesize_max); 
    $numresults  = doPost('numresults', $numresults); 
    $outputformat = doPost('outputformat', $outputformat); 
    $orderby  = doPost('orderby',  $orderby); 
    $orderbyAD  = doPost('orderbyad', $orderbyAD); 
    $outputLocation = doPost('outputlocation', $outputLocation); 

對不起,我不想錯過任何重要的東西。

如果任何人都可以發現我做錯了什麼,那麼我很想知道!

感謝,

馬丁

UPDATE

doPost方法:

FUNCTION doPost($st, $default) 
{ 
    if (isset($_POST[$st])) 
     return $_POST[$st]; 
    else 
     return $default; 
} 

高級搜索建立查詢不錯,但表單字段不附和值是應該設置給他們。

完整代碼可以在這裏看到(不包括大量的JS文件):http://pastebin.com/eydtyEYK

+1

你可以發佈'doPost()'的代碼嗎? – 2011-04-22 12:21:48

+0

什麼不工作?高級搜索中的字段沒有被髮布/回顯? – 2011-04-22 12:24:34

+0

不知道這是一個問題,但你錯過了:$(document).ready(function(){...你在這裏的功能...}); – 2011-04-22 12:26:52

回答

0

我最終被複制的形式投入使用JavaScript隱藏的輸入值解決它被點擊時的高級搜索按鈕,就像這樣:

function copy() { 

var filetype=$("#filetype option:selected").val(); 
$("#hiddenfiletype").val(filetype); 

var filename=$("#filename").val(); 
$("#hiddenfilename").val(filename); 

var filesize_min=$("#filesize_min").val(); 
$("#hiddenfilesize_min").val(filesize_min); 

var filesize_max=$("#filesize_max").val(); 
$("#hiddenfilesize_max").val(filesize_max); 

var numresults=$("#numresults").val(); 
("#hiddennumresults").val(numresults); 

var orderby=$("#orderby option:selected").val(); 
$("#hiddenorderby").val(orderby); 

var orderbyad=$("#orderbyad option:selected").val(); 
$("#hiddenorderbyad").val(orderbyad); 

var outputformat=$("#outputformat option:selected").val(); 
$("#hiddenoutputformat").val(outputformat); 

var outputlocation=$("#outputlocation option:selected").val(); 
$("#hiddenoutputlocation").val(outputlocation); 
} 

形式:

<div class="advancedsearchsubmit"><input type="submit" class="submit" onclick="copy();" value="Advanced Search"/></div> 
0

它可以如下簡單:在此輸入

<input type="hidden" name="hiddenfiletype" id="hiddenfiletype"/> 

沒有價值。或者,也許我是愚蠢的:)

+0

那不會是。如果該字段沒有值(或空字段)並不重要,它仍然會提交。 – 2011-04-22 12:37:58

+0

我認爲這些字段在帖子中回覆時有錯誤的值。 – k102 2011-04-22 12:40:25

+0

只是空的,我想。進行測試... – 2011-04-22 12:41:30

1

當試圖找出什麼是通過之間,HTML,JS和PHP在POST方法形式然後使用這些方法之一來測試實際輸出與您自己的期望。

var_dump($_POST); 
var_export($_POST); 
print_r($_POST, 1); 

第一個通常是最好的使用。

+0

謝謝,我發現當提交高級搜索時,它沿着'array(14){[「advancedsearchsubmit」] => string(1)「1」[「hiddenfiletype」] => string(5)「excel」[「hiddenfilename」] => string(4)「test」[「hiddenfilesize_min」] => string(3)「123」[「hiddenfilesize_max」] => string(3)「456 「[」hiddennumresults「] =>字符串(2)」12「...' 但我設置這些POST變量與PHP變量相同的名稱,他們不顯示? – martincarlin87 2011-04-22 14:31:13

+0

那麼如果你採取hiddenfiletype(「excel」)doPost()的結果是什麼?再次使用var_dump(doPost());並追蹤這個變量,直到你找到錯誤的地方。 Echo和var_dump()變量出現在頁面上 - 即使它意味着只需製作一個迷你應用程序,直到它被調試完畢。 – Cups 2011-04-22 14:39:23

+0

問題是,當提交高級搜索表單時,我將這些帖子值和設置爲變量並將這些變量作爲文本輸入值回顯出來。使用相同的值來建立一個可以正常工作的mysql查詢,它只是記住了搜索表單值,但它不起作用,我看不出如何解決它:S – martincarlin87 2011-04-22 14:47:29