dpaal28266 2015-08-25 17:54
浏览 48

使用$ _GET和Action进行Onclick重定向

I have 2 radio buttons, and each does an action and redirects to that action. But I need it to keep my current page (or week in this case) so it redirects to the correct page.

I think I'm not using the php syntax correctly inside of the onclick.

echo ' <input type="radio" ' . (($spreads == 'yes') ? ' checked="checked"' : '') . ' onclick="window.location=\'' . ((!empty($_GET['week'])) ? '?week=' . (int)$_GET['week'] : '') . '\'&action=spreads\'"/><label>Show</label>' . "
";

So it should get the week that the user is on, and redirect. So the URL would be something like: http://www.testsite.com/page.php?week=4?action=spreads

EDIT: I should have mentioned it was all inside of an echo to begin with.

EDIT 2: If i use a variable instead, it works just like I want it to. Very odd... I still would like to know how to use it without a variable. Works:

$week_number = ((!empty($_GET['week'])) ? '?week=' . (int)$_GET['week'] : '');

//radio button
echo ' <td><input class="radio" type="radio" id="Show" ' . (($spreads == 'yes') ? ' checked="checked"' : '') . ' value="Show" onclick="location.href=\''.$week_number.'&action=spreads\'"/><label class="choice" for="Show">Show</label>' . "
";
  • 写回答

1条回答 默认 最新

  • dougan1884 2015-08-25 17:55
    关注

    You concat url-params with & and it must be window.location instead of location.href. So your code should be the following:

    value="Show" onclick="window.location=\'' . ((!empty($_GET['week'])) ? '?week=' . (int)$_GET['week'] : '') . '\'&action=spreads\'"/><label class="choice" for="Show">
    

    And make sure to prepend the baseurl if you want to redirect to another url with given params.

    EDIT

    I've edited my code again, because it seems that you are missing the opening single-quote before the url. If this does not work too, please make sure to update your question with the full php echo for this specific line.

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退