I have a HTML form:
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><br>
Order Number: <input type="number" name="orderNo" size="10"/>
<input type="submit" value="Download" />
</form>
and the PHP isset:
if(isset($_POST['Download'])){download();}
Which has the user enter a number, I then want it to run code which based on the number entered creates a file with data from a table and has the file download to the user. This works having the download code in a separate file and simply calling the file in the form action, however I need it to be ran in the file with the form.
I currently have another form on the same page which uses isset to run a function to upload a file to the server and this works fine, however trying it with the other code just results in nothing happening. I'm new to PHP and HTML when it comes to this interaction.
Here is the full code for the download script:
function download()
{
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('amazondb', $conn);
$result = mysql_query("SELECT ID, PurchaseDate, BuyerName, ShipCity, ShipState, ShipPostalCode, ShipCountry,
ShipAddress1, ShipAddress2, ShipAddress3 FROM imported_orders");
$orderNo = $_POST['orderNo'];
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if($orderNo>0&&$orderNo<=count($row))
{
$file = fopen('Order.txt', 'w');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if($row['ID']==($orderNo))
{
fwrite($file, $row['BuyerName'].PHP_EOL .$row['ShipAddress1'].PHP_EOL .$row['ShipAddress2'].PHP_EOL .$row['ShipAddress3'].PHP_EOL .$row['ShipCity'].PHP_EOL .$row['ShipState'].PHP_EOL .$row['ShipPostalCode'].PHP_EOL .$row['ShipCountry'].PHP_EOL);
}
}
fclose($file);
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
}
else{echo "Please enter a valid Order Number";}
}
It is probably something simple, thanks for any help.
Your checking for an attribute that you don't submit (your button has no name). Either give it a name, add an invisible input field or even better, check for the number attribute:
if ( isset($_POST['number']) && is_numeric($_POST['number']) ) {
download();
}
just add name in html file..
<input type="submit" name="download" value="Download" />
The name or ID needs to be 'Download' on an element for that check to work. You only have a button that has a value of download, it has no name or id. Try this:
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><br>
Order Number: <input type="number" name="orderNo" size="10"/>
<input type="submit" name="Download" value="Download" />
</form>