简体   繁体   English

单击锚标记后,如何从功能页面调用特定功能

[英]How do i call a specific function from a functions page when an anchor tag is clicked

Before i go on, I'm aware that this question has been asked a couple of times but it doesn't deal with specificity. 在我继续之前,我知道这个问题已经被问过几次了,但是并没有针对性。

I have a functions.php script which contains a couple of functions and i would like to call a specific function when the user clicks on an anchor tag. 我有一个functions.php脚本,其中包含几个函数,当用户单击锚标记时,我想调用一个特定的函数。

I have gone through most of the questions in this manner and i understand that this would be done through javascript and ajax load the page specified with the on-click attribute. 我已经通过这种方式解决了大多数问题,并且我了解这将通过javascript完成,并且ajax加载使用on-click属性指定的页面。

My question is when this happens(page is being loaded) how do I call a specific function out of the functions.php script and if I have required it on the current page where the anchor tag exists will it cause complications? 我的问题是这种情况发生时(正在加载页面)如何从functions.php脚本中调用特定函数,如果我在锚标记存在的当前页面上需要它,会引起复杂性吗?

To be more precise i have a register.php page which does the following; 更准确地说,我有一个register.php页面,它执行以下操作; take user data then validate, if validated insert into DB and send a mail to the user to verify his account then redirect to a registration_complete.php page which has the option of resending the link if user didn't receive it. 获取用户数据,然后进行验证,如果已验证,则插入到数据库中,然后向用户发送邮件以验证其帐户,然后重定向到registration_complete.php页面,该页面可以选择在用户未收到链接的情况下重新发送链接。 Hence clicking the link will run a specific mail function in the functions.php file. 因此,单击链接将在functions.php文件中运行特定的邮件功能。

The Code is written below 代码写在下面

register.php register.php

<?php 
 session_start();
 $_SESSION['name'] = hmtspecialchars($_POST['name']);

 //validation code goes here
  if (isset ($_POST)){ //check that fields are not empty etc...
                       // insert into db code...
                       // email the user code...
                       // redirect to registration_complete.php code..
  } 

?>
<form method='post' action="">
  <input type="text" name="name" id="name">
  <input type="text" name="email" id="email">
  <input type= "submit" value="submit">
</form>

registration_complete.php registration_complete.php

<?php
 require'functions.php'
 session_start();
 $Name = $_SESSION['name']
 $RegisterationComplete = "Thank you . ' ' . ' $Name' . ' ' . for registering pls click on the link in the email sent to the email address you provided to verify you account. If you didn't recieve the email click on the resend email link below to get on resent to you. Please make sure to check your spam folder if you did not see it in your inbox folder."
?>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
  function doSomething() {
    $.get("somepage.php");
    return false;
  }
</script>
<a href="#" onclick="do something">Resend Verification Link</a>

Please not that i have copied the js code from one of the answers related to my question 请不要因为我已经从与我的问题相关的答案之一中复制了js代码

functions.php functions.php

<?php 
  //connect to db code

  // insert into db code

  // send verification link code using PHP Mailer function..

?>

So when ajax loads the functions.php page how does javascript call the exact function(PHP Mailer). 因此,当ajax加载functions.php页面时,javascript如何调用确切的函数(PHP Mailer)。

I just want to state that i am new to programming i'm only a bit conversant with php. 我只想声明自己是编程的新手,但是我对php还是有点熟。 My knowledge of Javascript and Ajax can be said to be negligible. 我对Javascript和Ajax的了解可以说是微不足道的。 Also want to say a big thank you to all contributors. 还想对所有贡献者表示由衷的感谢。

Javascript will never call PHP functions, since PHP is running on the server and Javascript is running in the web-browser. Javascript永远不会调用PHP函数,因为PHP在服务器上运行,而Javascript在网络浏览器中运行。 The server and the web-browser are assumed to be different machines, the only exception being testing by developers. 假定服务器和Web浏览器是不同的机器,唯一的例外是开发人员进行的测试。 Therefore, if you have a function in functions.php called foo, Javascript will not be able to call it. 因此,如果在functions.php中有一个名为foo的函数,则Javascript将无法调用它。

As you have already mentioned in your question, this might involve AJAX, which is surely true, but let's be more exact: when your Javascript code intends to "execute" a PHP function, it needs to trigger a request to the server. 正如您在问题中已经提到的那样,这可能涉及到AJAX,这确实是正确的,但是更确切地说:当您的Javascript代码打算“执行” PHP函数时,它需要触发对服务器的请求。 Not necessarily with AJAX, as you can trigger form submission, or anchor click as well. 不一定要使用AJAX,因为您可以触发表单提交或锚定单击。 The request will reach the server, which will handle it. 该请求将到达服务器,服务器将对其进行处理。

Now, since we know that the life cycle is as follows: 现在,由于我们知道生命周期如下:

  • Javascript detects that foo has to be executed Javascript检测到必须执行foo
  • Javascript triggers a request to the server Javascript触发对服务器的请求
  • Server is requested 服务器被请求
  • Server handles the request 服务器处理请求
  • Server responds 服务器响应

The missing piece in the puzzle is to let the server know that it has to execute foo. 难题中缺少的部分是让服务器知道它必须执行foo。 To achieve this, the server has to determine somehow whether foo needs to be executed. 为此,服务器必须以某种方式确定是否需要执行foo。 This can be done with various way, including get params or post params. 这可以通过多种方式完成,包括获取参数或发布参数。 Next, you need to modify your Javascript code or html structure to let the server know that the function needs to be executed. 接下来,您需要修改Javascript代码或html结构,以使服务器知道该功能需要执行。

You can add a get parameter to the href of the anchor tag, for instance, but in general, you need to let the server know what the intention is and at server-side you need to handle that intention. 例如,您可以将一个get参数添加到锚标记的href中,但是通常,您需要让服务器知道意图是什么,并且在服务器端需要处理该意图。

Also, you are doing validation on the server. 另外,您正在服务器上进行验证。 This is ok, but may I advise you to validate the inputs on client-side and prevent posting if the input is invalid, to reduce server load... On server-side, if the post is valid, you need to execute the needed functions. 可以,但是我建议您在客户端上验证输入,并在输入无效时阻止发布,以减少服务器负载...在服务器端,如果发布有效,则需要执行所需的操作职能。

Also, this part is not exactly correct: 另外,这部分也不完全正确:

if (isset ($_POST)){

This is not the right approach to check whether this was a post request. 这不是检查此请求是否正确的方法。 You need 你需要

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

instead. 代替。

It is not important if the Request comes from javascript or a regular server Request (user clicks on link). 如果请求来自javascript或常规服务器请求(用户单击链接),这并不重要。 You need to check the GET or POST parameters and redirect the Request to a specific function. 您需要检查GET或POST参数,并将Request重定向到特定功能。

<?php
if( isset( $_GET['method'] ) ) {
    // NOTE: untested and REALY unsecure Code
    $method = $_GET['method'];

    if( function_exists( $method ) ) {
        call_user_func( $method );
    }
}
else {
    echo '<a id="link" href="?method=foo">klickme</a>';
}


function foo(){
    echo 'in method';
}

?>
<div id="answer"><!-- server answer here --></div>

when you now have a link 当您现在有链接时

http://yourSite.com?method=foo

and the function foo gets executed. 然后函数foo被执行。

Now the JS part you have to check if the user clicks on a link, or sends a form. 现在,您必须检查JS部分,用户是否单击链接或发送表单。 Then you have to send the request to server using Ajax and handle the result from the Server. 然后,您必须使用Ajax将请求发送到服务器,并处理来自服务器的结果。

// inject the serverData in DOM
function loadSuccess( e ) {
    document.getElementById( 'answer' ).innerHTML = e.target.response;
}
// handle click, open ajax request
function doClick( e ) {
    e.preventDefault();
    var ajax = new XMLHttpRequest();
    ajax.open("GET", e.target.href ,true);
    ajax.send();
    ajax.addEventListener('load', loadSuccess);
}

var link = document.getElementById( 'link' );
link.addEventListener( 'click', doClick );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击锚标记时如何显示和隐藏输入? - How do I show and hide input when anchor tag is clicked? 单击锚标记时如何验证表单中的数据 - How do I validate data in a form when an anchor tag is clicked 当我有一个锚标记引用同一页面中的另一个部分时,如何使它不立即显示我点击它 - When I have an anchor tag refrencing another section in the same page, how do I make it not to display immediately i have clicked it 单击锚标记时从不同页面的角度传递值 - angular passing value from to different page when anchor tag is clicked 如何从锚标记运行 javascript 函数? - How do I run a javascript function from an anchor tag? 单击锚标记和外部元素时,如何使用JQuery交换图像? - How do I use JQuery to swap images when clicked on anchor tag and outside element? 我想在单击该标签时传递锚标签内的文本并在定向页面上显示相同的文本 - I want to Pass the text inside anchor tag when clicked on that tag and to show the same text on the directed page 单击子锚点时,如何防止父项的 onclick 事件触发? - How do I prevent a parent's onclick event from firing when a child anchor is clicked? CSS 通过HTML锚标签登陆页面时不显示样式,如何“恢复”呢? - CSS style is not displayed when landing on page through HTML anchor tag, how do I "restore" it? 如何从锚标签onclick函数调用jQuery(this) - how to call jQuery(this) from anchor tag onclick function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM