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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度