gpt4 book ai didi

php - mysql选择js变量

转载 作者:行者123 更新时间:2023-11-29 13:41:47 26 4
gpt4 key购买 nike

嘿,我想知道这样的事情是否可能?

$result = mysql_query("SELECT * FROM posted_events WHERE Month_ = ".echo"monthS";."  );

显然语法是错误的(因为它不起作用),但基本上我想从数据库中选择一些数据,这些数据等于我的 JS 文件中恰好是 JS 变量的变量。我知道你不能将js变量分配给PHP,除非你使用AJAX,但是当我使用ajax时,它首先返回一个空字符串,运行php,然后它给我变量的值,但到那时我的php 脚本已经运行。所以想知道我是否可以回显该变量。

我没有使用任何提交或隐藏帖子方法,因为在我尝试创建的内容中没有必要。所以我需要找到一种方法让PHP识别变量并进行查询。请帮我。非常感谢。

我的JS:

   var date = new Date();
var month = date.getMonth();
var day = date.getDay();
var monthDate = date.getDate();
var monthNow= date.getMonth();
var monthNow2;
var current = 1;
var first_date = new Date(date.getFullYear(), date.getMonth(), 1);
var start_day = first_date.getDay();
var start_day2;
var first_date2;
var weekday;
var day_selected;
var day_selectedTxt;
var monthNowTxt;
var dayNum;
var out;
var monthS;

var calenderMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'];

var calenderDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ];

var daysInMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];




function currentDate(){
var first_day = first_date.getDay() +1;
document.getElementById("monthElement").innerHTML = calenderMonths[month];

for( var days = 1; days <= daysInMonths[month]; days++) {


document.getElementById("day"+ first_day).innerHTML = days;
first_day++;

}
//current date

if( month === monthNow) {
monthDateNow = start_day +monthDate;
var today = document.getElementById("day"+ monthDateNow);
today.setAttribute('class', today.getAttribute('class') + ' current');

}
start_day2 = start_day;
monthNow2 = monthNow;

monthS = calenderMonths[monthNow];
$.get('load2.php', {month:monthS} );
//where i made the ajax get

}

我的 PHP:

<?php

Header("content-type: application/x-javascript");
error_reporting(E_ERROR | E_WARNING | E_PARSE);



$monthS = $_GET['month'];



// Connect to MySQL
if ( !( $database = mysql_connect( "localhost",
"root", "" ) ) )
die( "Could not connect to database </body></html>" );

// open Events database
if ( !mysql_select_db( "Events", $database ) )
die( "Could not open Events database </body></html>" );


$result = mysql_query("SELECT * FROM posted_events WHERE Month_ = .'monthS'.")
or die ('Error updating database because: '.mysql_error());;


$daysArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$daysArray[] = $row['DayNum']; }
$length = count($daysArray);

echo "function test() { ";

for($i=0; $i < $length; $i++) {
echo "var active = document.getElementById('day'+".$daysArray[$i].");
active.setAttribute('class', active.getAttribute('class') + ' event'); ";
}
echo "}";



?>

在我的 HTML 中,我有 <body onload="currentDate(); test(); " class ="home">和其他代码ofc,我刚刚发布,因为其余的都是不必要的。

最佳答案

尝试下面的代码,

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

$monthS = $_GET['month'];

$result = mysql_query("SELECT * FROM posted_events WHERE Month_ = '$monthS'");


while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$daysArray[] = $row['DayNum'];
}

$length = count($daysArray);

for($i=0; $i < $length; $i++)
{
echo $daysArray[$i];
}

?>

不建议使用 MySql,建议您迁移到 mysqli 或 pdo

关于php - mysql选择js变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17979547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com