简体   繁体   English

将 php function 添加到 javascript

[英]adding a php function into javascript

I have a file for a calendar made with javascript called calendar_db.js, in it there is this if statment:我有一个使用 javascript 制作的日历文件,名为 calendar_db.js,其中有以下 if 语句:

        if (d_current.getDay() != 3 && d_current.getDay() != 6)
            a_class[a_class.length] = 'available';

it checks if the day is not the 3d or the 6th then.它会检查当天是否不是 3d 或第 6 天。 So my question is how to make a php mysql query to get those numbers (3 and 6) because I want to change them with mysql databse.所以我的问题是如何进行 php mysql 查询以获取这些数字(3 和 6),因为我想用 mysql 数据库更改它们。 What are your suggestions?你有什么建议?

Thank you for your time and help.感谢您的时间和帮助。

The short answer is: You can't简短的回答是:你不能

However, you can use Ajax (Google it) to make calls to an external php file, which will process your request for you.但是,您可以使用 Ajax (Google it) 调用外部 php 文件,该文件将为您处理您的请求。 Then, the php file can print out the result, which will send the information back to you.然后,php 文件可以打印出结果,这会将信息发送回给您。

Take a look here: http://www.w3schools.com/ajax/default.asp看看这里: http://www.w3schools.com/ajax/default.asp

EDIT:编辑:

Read the javascript file by using fopen(), file_get_contents(), or CURL:使用 fopen()、file_get_contents() 或 CURL 读取 javascript 文件:

http://www.php-mysql-tutorial.com/wikis/php-tutorial/reading-a-remote-file-using-php.aspx http://www.php-mysql-tutorial.com/wikis/php-tutorial/reading-a-remote-file-using-php.aspx

Use some kind of regex to parse the javascript file looking for the particular match of the line with your values.使用某种正则表达式来解析 javascript 文件,以查找与您的值匹配的行。 If your javascript file isn't going to change much, it might be easier to just count lines and characters and get the number at exactly some position.如果您的 javascript 文件不会发生太大变化,那么只计算行数和字符并获得恰好 position 的数字可能会更容易。 This assumes your numbers will always be single digits.这假设您的数字将始终是个位数。

Is this what you're after?这就是你所追求的吗?

You have to use AJAX to interact with the server via JavaScript.您必须使用AJAX通过 JavaScript 与服务器交互。

Since the code to setup an AJAX request is a bit long and tedious, I'll show you how to do it with the jQuery framework.由于设置 AJAX 请求的代码有点冗长乏味,我将向您展示如何使用jQuery框架来完成。

Basically, just make the server spit the two values out (imagine this being the output of foo.php ):基本上,只需让服务器吐出两个值(想象这是 foo.php 的output ):

12,19

Now, with AJAX you can read that output.现在,使用 AJAX,您可以读取 output。 There are two types of requests: GET and POST .有两种类型的请求: GETPOST If you're familiar with PHP, you can change this according to what your application uses:如果您熟悉 PHP,则可以根据您的应用程序使用的内容进行更改:

var day1, day2;

$.get('foo.php', function(data) {
  var split = data.split(',');

  day1 = parseInt(split[0]);
  day2 = parseInt(split[1]);
});

Now day1 and day2 hold your two dates.现在day1day2保存你的两个日期。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM