2013-02-08 30 views
0

我該如何做這樣的工作?單擊按鈕時執行php命令html

<input name="downvid2" type="button" id="downvid2" onclick=" 
    <?php 
header('Content-disposition: attachment; filename=file.pdf'); 
header('Content-type: application/pdf'); 
readfile('file.pdf'); 
?>" value="Download Story" /> 

事情是我會做一個表格並提交,但我需要一些PHP變量的值,我不想失去他們,當我移動頁面。

完整的代碼是:

<title>Legendmaker - Your Legendmaker Adventure Starring You:</title> 

<?php 
if($_POST) 
{ 
$username="***"; 
$password="*****"; 
    $con = mysqli_connect("storycodes.db.10339998.hostedresource.com",$username,$password); 

    if (!$con) 
    { 
     die('Could not connect: ' . mysqli_error()); 
    } 

    mysqli_select_db($con, "storycodes"); 

$code = $_POST['codeInput']; 
$code = mysqli_escape_string($con, htmlspecialchars($code)); //May not acually need htmlspecialchars 
$query = "SELECT story,video FROM `storycodes` WHERE `code` = '$code'"; 
$result = mysqli_query($con, $query); 

    if (mysqli_num_rows($result)) 
    { 
    $row = mysqli_fetch_assoc($result); 
    mysqli_free_result($result); 
    extract($row); 
    echo $story . $video; 


    } 
    else 
    { 
    echo "No Data Found. Please check your serial code to ensure that you have not incorrectly entered it. If the code is correct please email the website administrator for further assistance"; 
    }  

mysqli_close($con); 
} 
?> 
<div align="center"> 
    <p><span class="linkText"><a href="/index.html">Home</a> <a href="/contact-us.php">Contact Us</a> <a href="/payments.html">Products</a><a href="/products.html"></a></span> </p> 
    <p>&nbsp;</p> 
    <h2 class="headingText"><img alt="legendmaker - makes legends: banner" width="728" height="90" /></h2> 
    <h2 class="headingText">&nbsp;</h2> 
    <h2 class="headingText">Your story</h2> 
</div> 
<p>&nbsp;</p> 

    <label> 
    <input type="button" name="downvid" id="downvid" value="Download Video" /> 
    </label> 
    <input name="downvid2" type="button" id="downvid2" onclick=" 
    <?php 
header('Content-disposition: attachment; filename=file.pdf'); 
header('Content-type: application/pdf'); 
readfile('file.pdf'); 
?>" value="Download Story" /> 

我想我必須有,因爲我想使文件不是web入店它照顧涉及文件PHP的一切,這樣只有那些誰輸入序列碼放入以前的表單可以訪問它。

回答

3

不,它不能工作。 PHP是服務器端語言,HTML不是。如果你不想提交表單,那麼你可以使用AJAX來達到這個目的,但不是像你問的那樣。您可以提交表單以將數據發送到PHP或使用JavaScript,AJAX。

+0

好的,謝謝我將檢查ajax和表單提交。我很感謝幫助的人。 –

1

onclick事件是執行客戶端。由於PHP是服務器端,您的僞代碼無法工作。

更好的解決方案是通過鏈接或提交表單來重定向用戶。

重定向應該指向您的PHP腳本,它將下載發送給您的用戶。這將使瀏覽器停留在當前頁面上,並且效果將像您的僞樣本一樣。