douyao3895
2017-09-13 22:39
浏览 67

在JavaScript函数的函数文件中执行PHP函数

I want to call a PHP function from a JavaScript function.

"Why?" you might ask?

I have a series of PHP functions organized in a separate file custom_functions.php

I am slowly learning how to build with JavaScript and jQuery so this is how I am bridging the gap. I am more familiar with PHP, so I am finding my way through that window.

I have a list of elements constructed in index.php. A list of items is constructed on the fly from the database and an id attribute is created based on the id of the record:

echo "<span id="content_" . $row{'unique_id'} . "\" onClick=\"displayDetails(" . $row{'unique_id'} . ")\">";

displayDetails(record_no) is a JavaScript function in an external JavaScript file.

For now I tried this:

function displayDetails(record){
    data = {};
    //data = <?php retrieveContent(content_id); ?>
    $('.output').html("Testing Function - Section " + section_number);
}

But of course this does not work because PHP is Server Side action and JavaScript is a client-side action.

My particular problem is just enough different from this one to leave me stumped.

I have a PHP file and within it I want AJAX to call a specific function from a range of functions in the file.

What's not clear is how to call the function in the file with do that with AJAX. Do I pass in the URL like this: "custom_functions.php?functionname=displaydetails&record=<some number>" ?

For now, I do not want to use jQuery AJAX, I'd like to understand the fundamentals of XMLHttpRequest purely from a pure JavaScript/DOM perspective.

图片转代码服务由CSDN问答提供 功能建议

我想从JavaScript函数调用PHP函数。

“ 为什么?” 您可能会问?

我在单独的文件中组织了一系列PHP函数custom_functions.php

我正在慢慢学习如何使用JavaScript构建 和jQuery所以这就是我如何弥合差距。 我对PHP更熟悉,所以我找到了通过该窗口的方法。

我有一个在index.php中构造的元素列表。 项目列表是动态构建的 从数据库中,根据记录的id创建一个id属性:

  echo“&lt; span id =”content_“。$ row {'unique_id'}。  “\”onClick = \“displayDetails(”。$ row {'unique_id'}。“)\”&gt;“; 
   
 
 

displayDetails(record_no )是外部JavaScript文件中的JavaScript函数。

现在我试过这个:

  function displayDetails(record  ){
 data = {}; 
 // data =&lt;?php retrieveContent(content_id);  ?&gt; 
 $('。output')。html(“Testing Function  -  Section”+ section_number); 
} 
   
 
 

但当然这样做 不起作用,因为PHP是服务器端操作,JavaScript是客户端行动

我的特殊问题与这个让我感到难过。

我有一个PHP文件,在其中我希望AJAX从一系列的范围内调用特定的函数 文件中的函数。

目前还不清楚如何使用AJAX调用文件中的函数。 我是否传递了这样的URL: “ custom_functions.php?functionname = displaydetails&amp; record =&lt; some number&gt; “?

现在,我不想使用jQuery AJAX,我想了解 XMLHttpRequest的基本原理纯粹来自纯Java 脚本/ DOM透视。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqintong8972 2017-09-13 23:42
    已采纳

    Yes, You have to call your PHP file like you mentioned.

    When calling PHP script by AJAX, return data is generally JSON formation. What you echo in PHP will become AJAX Result.

    And Javascript do the job creating elements to show and inject the data.

    However, you can also inject the result HTML of PHP script directly to the DOM.

    Example1: Injecting ajax response data directly to the DOM

    Javascript

    axios.get('/example_api.php').then(resp => {
        // in this case, you gonna just put response data into innerHTML where you wanna show.
        someDomeElement.innerHTML = resp.data;
    });
    

    PHP (example_api.php)

    echo "<p>hello world</p>";
    

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

    Example2: When ajax response data is json

    Javascript

    axios.get('/example_api.php').then(resp => {
        resp.data.forEach(function(item) {
           // In this case, you have to create DOM elements to show data 
           createElement(item); 
        });
    });
    

    PHP(example_api.php)

    echo json_encode(array('data1', 'data2'));
    

    update

    I will update answer here if you comment more questions.

    点赞 评论

相关推荐 更多相似问题