加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

如何从HTML或Javascript调用PHP文件

发布时间:2020-12-14 23:32:40 所属栏目:资源 来源:网络整理
导读:我现在已经看了很久了,我找不到能解决这个看似基本问题的地方.我不关心页面是否重新加载或立即显示结果;我只想在我的网站上有一个按钮来运行 PHP文件.而已.我发现所有网站都会告诉您如何直接在HTML中运行PHP代码或告诉您使用ajax.如果事实证明是必要的话我会
我现在已经看了很久了,我找不到能解决这个看似基本问题的地方.我不关心页面是否重新加载或立即显示结果;我只想在我的网站上有一个按钮来运行 PHP文件.而已.我发现所有网站都会告诉您如何直接在HTML中运行PHP代码或告诉您使用ajax.如果事实证明是必要的话我会使用ajax,但是在使用PHP之前有一段时间存在ajax,所以我认为有一种更简单的方法.如果我可以在HTML中编写代码就好了,为什么我不能在那里引用它的文件或者在 Javascript中对它进行简单的调用?

如果事实证明ajax是前进的唯一方法,那么我需要知道什么来调用一个PHP文件来创建一个按钮按下的文本文件?我正在为自己创建一个简单的博客网站,我已经获得了该网站的代码和javascript,可以将我在textarea中写的帖子立即显示出来.我只想将它链接到一个PHP文件,该文件将在服务器上创建永久博客帖子,这样当我重新加载页面时,帖子仍然存在.

感谢您抽出宝贵时间阅读和回答.

解决方法

如何打个按钮调用PHP?

I don’t care if the page reloads or displays the results immediately;

好!

注意:如果您不想刷新页面,请参阅“Ok …但是如何使用Ajax?”下面.

I just want to have a button on my website make a PHP file run.

这可以通过一个带有单个按钮的表单来完成:

<form action="">
  <input type="submit" value="my button"/>
</form>

That’s it.

差不多.另请注意,有些情况下ajax确实是要走的路.

这取决于你想要什么.一般而言,当您想要避免重写页面时,您只需要ajax.你还说你不关心那个.

为什么我不能直接从JavaScript调用PHP?

If I can write the code inside HTML just fine,why can’t I just reference the file for it in there or make a simple call for it in Javascript?

因为PHP代码不在HTML中就好了.这是大多数服务器端脚本语言工作方式(包括PHP,JSP和ASP)的错觉.该代码仅存在于服务器上,并且在没有某种远程调用的情况下,客户端(浏览器)无法访问该代码.

如果您要求浏览器显示页面的源代码,您可以看到此证据.那里你不会看到PHP代码,因为PHP代码没有发送到客户端,因此无法从客户端执行.这就是为什么你需要做一个远程调用才能让客户端触发PHP代码的执行.

如果您不使用表单(如上所示),您可以使用一个名为Ajax的小东西从JavaScript进行远程调用.您可能还想考虑是否可以直接在JavaScript中完成PHP中的操作.

如何调用另一个PHP文件?

使用表单进行通话.您可以让它将用户定向到特定文件:

<form action="myphpfile.php">
  <input type="submit" value="click on me!">
</form>

用户将最终进入myphpfile.php页面.要使其适用于当前页面,请将action设置为空字符串(这是我在早期给出的示例中所做的).

I just want to link it to a PHP file that will create the permanent blog post on the server so that when I reload the page,the post is still there.

你想在服务器上进行操作,你应该让你的表单有你需要的字段(即使type =“hidden”并使用POST):

<form action="" method="POST">
  <input type="text" value="default value,you can edit it" name="myfield">
  <input type="submit" value = "post">
</form>

What do I need to know about it to call a PHP file that will create a text file on a button press?

见:How to write into a file in PHP.

你如何从服务器上的POST接收数据?

我很高兴你问…因为你是一个新手,我会给你一个你可以遵循的模板:

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
        //Ok we got a POST,probably from a FORM,read from $_POST.
        var_dump($_PSOT); //Use this to see what info we got!
    }
    else
    {
       //You could assume you got a GET
       var_dump($_GET); //Use this to see what info we got!
    }
 ?>
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta char-set="utf-8">
     <title>Page title</title>
   </head>
   <body>
     <form action="" method="POST">
       <input type="text" value="default value,you can edit it" name="myfield">
       <input type="submit" value = "post">
     </form>
   </body>
 </html>

注意:您可以删除var_dump,它仅用于调试目的.

我如何…

我知道下一阶段,你会问如何:

>如何将变量从PHP文件传递到另一个?
>如何记住用户/登录?
>如何避免重新加载页面时出现的恼人信息?

对此有一个答案:会话.

我将为Post-Redirect-Get提供更广泛的模板

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
        var_dump($_PSOT);
        //Do stuff...
        //Write results to session
        session_start();
        $_SESSION['stuff'] = $something;
        //You can store stuff such as the user ID,so you can remeember him.
        //redirect:
        header('Location: ',true,303);
        //The redirection will cause the browser to request with GET
        //The results of the operation are in the session variable
        //It has empty location because we are redirecting to the same page
        //Otherwise use `header('Location: anotherpage.php',303);`
        exit();
    }
    else
    {
        //You could assume you got a GET
        var_dump($_GET); //Use this to see what info we got!
        //Get stuff from session
        session_start();
        if (array_key_exists('stuff',$_SESSION))
        {
           $something = $_SESSION['stuff'];
           //we got stuff
           //later use present the results of the operation to the user.
        }
        //clear stuff from session:
        unset($_SESSION['stuff']);
        //set headers
        header('Content-Type: text/html; charset=utf-8');
        //This header is telling the browser what are we sending.
        //And it says we are sending HTML in UTF-8 encoding
    }
 ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta char-set="utf-8">
    <title>Page title</title>
  </head>
  <body>
    <?php if (isset($something)){ echo '<span>'.$something.'</span>'}?>;
    <form action="" method="POST">
      <input type="text" value="default value,you can edit it" name="myfield">
      <input type="submit" value = "post">
    </form>
  </body>
</html>

请查看php.net,了解您无法识别的任何函数调用.另外 – 如果你还没有 – 获得关于HTML5的好教程.

另外,使用UTF-8 because UTF-8!

笔记:

I’m making a simple blog site for myself and I’ve got the code for the site and the javascript that can take the post I write in a textarea and display it immediately.

如果您使用的是CMS(Codepress,Joomla,Drupal等)?这使你对如何做事情产生了一些限制.

此外,如果您正在使用框架,您应该查看他们的文档或在他们的论坛/邮件列表/讨论页面/联系人询问或尝试询问作者.

好的……但是我怎么使用Ajax呢?

嗯……一些JavaScript库让Ajax变得简单.既然你是初学者,我会推荐jQuery.

所以,让我们通过Ajax使用jQuery向服务器发送一些东西,我将使用$.post代替$.ajax这个例子.

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
        var_dump($_PSOT);
        header('Location: ',303);
        exit();
    }
    else
    {
        var_dump($_GET);
        header('Content-Type: text/html; charset=utf-8');
    }
 ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta char-set="utf-8">
    <title>Page title</title>
    <script>
        function ajaxmagic()
        {
            $.post(                             //call the server
                "test.php",//At this url
                {
                    field: "value",name: "John"
                }                               //And send this data to it
            ).done(                             //And when it's done
                function(data)
                {
                    $('#fromAjax').html(data);  //Update here with the response
                }
            );
        }
    </script>
  </head>
  <body>
    <input type="button" value = "use ajax",onclick="ajaxmagic()">
    <span id="fromAjax"></span>
  </body>
</html>

上面的代码将向页面test.php发送POST请求.

注意:如果需要,您可以将会话与ajax和东西混合在一起.

我如何…

> How do I connect to the database?
> How do I prevent SQL injection?
> Why shouldn’t I use Mysql_* functions?

…对于这些或任何其他,请提出另一个问题.这对于这个来说太过分了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读