douyuanqia665858 2013-10-06 20:43
浏览 139
已采纳

将JS输出转换为PHP变量

Here is the code that I am currently using... (javascript injection)


Date.prototype.getWeek = function() {
  var onejan = new Date(this.getFullYear(),0,1);
  return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
var imgList = [
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',   'img/1.jpg',
  'img/1.jpg',  'img/1.jpg'   // Note: No comma after last entry
];

function showImage() {
  var today = new Date();
  var weekno = today.getWeek();
  document.getElementById('WeeklyImage').src = imgList[weekno];
  document.getElementById('WeeklyImage').alt = imgList[weekno];
//  alert(weekno+'\t'+imgList[weekno]);
}

I was wondering if someone can tell me how I convert the image src to a php variable like $weeklyImage or something like that so I can do the following...


echo "<img src='" . $weeklyImage . "'>";

I need the output to be in PHP so that I can use the variable for other areas of the site that require PHP functions.

If not possible, any other suggestions would be appreciated.

Thanks!

  • 写回答

2条回答 默认 最新

  • dongshi2141 2013-10-06 20:56
    关注

    It is impossible to directly pass values from JavaScript to PHP, because PHP runs on your server, then the result of your PHP script is sent to the client, and only then is JavaScript even run.

    For your problem, there is absolutely no need to use JavaScript at all. You can get the current week with date('W') and similarly store the entire list of images in a simple array:

    $images = ['image1.jpg', 'image2.jpg'...];
    echo '<img src="'.$images[date('W')].'">';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100