PHP - echoing out POST variables
I have a problem with some POST variables that I cannot see anything wrong with so I was hoping someone would be able to spot what is wrong?
This is the 'simple search' form:
<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> </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> </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> </td>
<td><input type="submit" class="submit" value="Search"></td>
<td> </td>
</tr>
So, if I hit the submit button everything is fine and the search values are displayed and the form remembers the field values so you can tweak your search criteria if you want to.
However, I also have another 'advanced search' form that I am trying to do this with but it doesn't work for some reason:
<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>
When the advanced search form is submitted, the variables are set as follows:
// 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);
The simple search variables are done similarly, except they echo out properly:
/* 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);
Sorry for the heaps of code, didn't want to miss out anything important.
If anyone can spot what I'm doing wrong then I would love to know!
Thanks,
Martin
UPDATE
doPost method:
FUNCTION doPost($st, $default)
{
if (isset($_POST[$st]))
return $_POST[$st];
else
return $default;
}
The advanced search builds the query fine but the form fields aren't echo开发者_如何转开发ing the values that should be set to them.
Complete code can be seen here (without the massive JS files): http://pastebin.com/eydtyEYK
When trying to work out what is being passed between, HTML, JS and PHP in a POST method form then use one of these methods to test actual output vs your own expectations.
var_dump( $_POST );
var_export( $_POST );
print_r($_POST, 1);
The first one is usually the best to use.
Can it be as simple as follows: in this input
<input type="hidden" name="hiddenfiletype" id="hiddenfiletype"/>
there is no value. Or maybe im dumb :)
I eventually solved it by copying the values of the form inputs to the hidden inputs using javascript when the advanced search button was clicked, like so:
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);
}
In the form:
<div class="advancedsearchsubmit"><input type="submit" class="submit" onclick="copy();" value="Advanced Search"/></div>
精彩评论