html超链接调用php,如何从HTML或Javascript调用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.

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

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中的操作.

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

用户将最终进入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):

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

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

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

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!

}

?>

Page title

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

我如何…

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

>如何将变量从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

}

?>

Page title

<?PHP if (isset($something)){ echo ''.$something.''}?>;

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

笔记:

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这个例子.

if ($_SERVER['REQUEST_METHOD'] === 'POST')

{

var_dump($_PSOT);

header('Location: ',303);

exit();

}

else

{

var_dump($_GET);

header('Content-Type: text/html; charset=utf-8');

}

?>

Page title

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

}

);

}

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

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

我如何…

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值