单击按钮时执行php命令

单击按钮时执行php命令,php,html,forms,post,button,Php,Html,Forms,Post,Button,我该如何做这样有效的事情 <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" />

我该如何做这样有效的事情

<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是一种服务器端语言,而HTML不是。如果您不想提交表单,那么可以使用AJAX,但不能像您要求的那样。要么提交表单将数据发送到PHP,要么使用JavaScript,尤其是AJAX

不,它不能工作。PHP是一种服务器端语言,而HTML不是。如果您不想提交表单,那么可以使用AJAX,但不能像您要求的那样。要么提交表单将数据发送到PHP,要么使用JavaScript,尤其是AJAX

onclick事件在客户端执行。由于PHP是服务器端的,所以伪代码无法工作

更好的解决方案是通过链接或提交表单重定向用户


重定向应该指向将下载发送给用户的PHP脚本。这将使浏览器停留在当前页面上,其效果将类似于您的伪示例。

onclick事件将在客户端执行。由于PHP是服务器端的,所以伪代码无法工作

更好的解决方案是通过链接或提交表单重定向用户


重定向应该指向将下载发送给用户的PHP脚本。这将使浏览器停留在当前页面上,其效果将类似于您的伪示例。

好的,谢谢,我将检查ajax和表单提交。谢谢大家的帮助。好的,谢谢。我会检查ajax和表单提交。我感谢你们的帮助。
<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" />