最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - specifying relative file paths in WP with jQuery requests not working

programmeradmin0浏览0评论

Why does it seem that WP requires me to utilize the entire and true file path instead of being able to use a relative file path? I know for safety its better not to use a true file path. In one of my functions, I am specifying a .php file located. Seems no matter what I do, I can not get my function to point to find the file.

The full directory that the php file is in domain_name/public_html/wp-content/mythemename/assets/php/select-change.php

A portion of the code that needs to locate my php file:

jQuery.post("assets/php/select-change.php"); When using the above I get a 404 error and it tells me it can not find the file using - domain_name/page_name/php/select-change.php . It seems insistent upon putting the page name in there.

Even when I place the select-change.php file directly into the same directory that contains the calling .js file (assets/js), then it still cant find it since it is looking for mydomain_name/page_name/select-change.php which is not the path. Solution?

(ive also tried things like):

`jQuery.post("select-change.php");
jQuery.post("../select-change.php");
jQuery.post("/select-change.php");
jQuery.post("php/select-change.php");
jQuery.post("../../select-change.php");`

The PHP file:

function change_post() {
if($_POST && $_POST['change']){
$wpdb->get_results($wpdb->prepare('UPDATE Staff_Connector_Table SET Staff_List_Staff_Member_Id =  "'.$_POST['change'].'" Table_id ='.$tbl_id));
}
}

Why does it seem that WP requires me to utilize the entire and true file path instead of being able to use a relative file path? I know for safety its better not to use a true file path. In one of my functions, I am specifying a .php file located. Seems no matter what I do, I can not get my function to point to find the file.

The full directory that the php file is in domain_name/public_html/wp-content/mythemename/assets/php/select-change.php

A portion of the code that needs to locate my php file:

jQuery.post("assets/php/select-change.php"); When using the above I get a 404 error and it tells me it can not find the file using - domain_name/page_name/php/select-change.php . It seems insistent upon putting the page name in there.

Even when I place the select-change.php file directly into the same directory that contains the calling .js file (assets/js), then it still cant find it since it is looking for mydomain_name/page_name/select-change.php which is not the path. Solution?

(ive also tried things like):

`jQuery.post("select-change.php");
jQuery.post("../select-change.php");
jQuery.post("/select-change.php");
jQuery.post("php/select-change.php");
jQuery.post("../../select-change.php");`

The PHP file:

function change_post() {
if($_POST && $_POST['change']){
$wpdb->get_results($wpdb->prepare('UPDATE Staff_Connector_Table SET Staff_List_Staff_Member_Id =  "'.$_POST['change'].'" Table_id ='.$tbl_id));
}
}
Share Improve this question edited Jan 3, 2021 at 18:04 Kris asked Jan 3, 2021 at 17:29 KrisKris 111 bronze badge 9
  • What happens in select-change.php - what do you need to post to it? – Q Studio Commented Jan 3, 2021 at 17:33
  • It is a db update that comes off the button click. – Kris Commented Jan 3, 2021 at 17:56
  • Can you share it, so we can understand it better? – Q Studio Commented Jan 3, 2021 at 18:00
  • 2 You should not be making direct requests to PHP files in your theme, it is extreme bad practice, insecure, and has lots of issues ( such as the problem you're having ). If you want to do things with AJAX, add a REST API endpoint instead. Look into developer.wordpress/reference/functions/register_rest_route you'll get a pretty URL to make requests against – Tom J Nowell Commented Jan 3, 2021 at 18:20
  • 1 Also, relative URLs are relative to the current URL in the browser, not the location of the file the code runs in – Tom J Nowell Commented Jan 3, 2021 at 18:22
 |  Show 4 more comments

1 Answer 1

Reset to default 1

In PHP, it's not necessary to post to a PHP file, it is only required to include ( or require ) files and then use checks and validations to ensure that code is only processed when required.

You simply need to include the PHP file as part of your application - it will process the PHP is the conditions are met.

发布评论

评论列表(0)

  1. 暂无评论