简体   繁体   English

如何提取通过onclick函数事件传递的值?

[英]How to extract the value passed in through onclick function event?

I have this onclick="ShowSingleNew(299)" event for a link and it opens up a new popup, what I'd like to do is extract the 299 (which is an id of the given new item) and pass it in my php script so that that would be the item the user can read. 我有一个链接的onclick="ShowSingleNew(299)"事件,它打开了一个新的弹出窗口,我想做的是提取299 (这是给定新项目的ID)并将其传递给我php脚本,以便用户可以阅读该项目。

The php bit looks like so: php位看起来像这样:

if (empty($_GET['news_id'])) { sql select .. }
else { sql select where news_id == $_GET['news_id']; }

So how can I pass this ShowSingleNew id to the next page and give it to the $_GET variable? 那么如何将这个ShowSingleNew id传递给下一页并将其提供给$ _GET变量?

You have to pass the newsId using query string like url?newsId=104 format. 您必须使用查询字符串(例如url?newsId = 104格式)传递newsId。

The basic format should be: ?strID=XXXX&strName=yyyy&strDate=zzzzz

The sample code looks like: 示例代码如下:

function ShowSingleNew(newsId) {
    var url = "Your url goes here";
    window.open(url + "?newsId=" + newsId);
    // other code
}

On php side, you could use $_GET['newsId'] to fetch the news id. 在php方面,您可以使用$_GET['newsId']来获取新闻ID。

Note: 注意:

Check how to pass mutiple values in query string here: multivalue query string passing 在此处检查如何在查询字符串中传递多个值: 多值查询字符串传递

Reference: 参考:

QueryString Structure QueryString结构

Try to send like 尝试发送像

function ShowSingleNew(id) {
   url = desired_url + '?news_id=' + id; 
   window.open(url , "_blank");
}

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

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