【问题标题】:Perform php commands when button is clicked html单击按钮时执行 php 命令 html
【发布时间】:2013-01-23 21:18:44
【问题描述】:

我将如何做这样有效的事情?

<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" />

我认为我必须让它处理涉及文件 PHP 的所有事情,因为我想让这些文件不能通过网络访问,这样只有在以前的表单中输入序列代码的人才能访问它。

【问题讨论】:

    标签: php html forms post button


    【解决方案1】:

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

    【讨论】:

    • 好的,谢谢,我会检查 ajax 和表单提交。感谢各位的帮助。
    【解决方案2】:

    onclick 事件被客户端执行。而且由于 PHP 是服务器端的,所以你的伪代码不能工作。

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

    重定向应该指向您的 PHP 脚本,该脚本会将下载内容发送给您的用户。这将使浏览器停留在当前页面上,效果就像你的伪样本一样。

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2018-08-11
      相关资源
      最近更新 更多