![](/img/trans.png)
[英]href dynamic generation of link not working with php. prints but doesnt work
[英]Dynamic href generation on link click based on radio button state : PHP interaction with javascript
我在codeigniter中使用MVC框架,并且有一个链接指向的视图(请注意, href
值是通过调用PHP函数生成的,而PHP函数又将'auth/mylogin'
作为参数:
<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin'); ?>"> CLICK ME</a>
现在,我在同一视图页面上有一个单选按钮。
<input id="ut1" name="usertype" type="radio" value="ut1"<?php echo set_radio('usertype', 'ut1', TRUE); ?> />
<label for="ut1" onclick="">User Type 1</label>
<input id="ut2" name="usertype" type="radio" value="ut2"<?php echo set_radio('usertype', 'ut2'); ?> />
<label for="ut2" onclick="">User Type 2</label>
现在我想要的是:我还想传递用户类型值。 我的意思是,取决于用户在单击链接之前选择了哪个单选按钮,我希望代码为:
单击“单击我之前”已选择“用户类型1”单选
<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/ut1'); ?>"> CLICK ME</a>
单击“单击我之前”已选择“用户类型2”单选
<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/ut2'); ?>"> CLICK ME</a>
由于没有提交,我假设我必须使用javascript来做到这一点。
但是由于javascript是客户端和PHP服务器端,所以我不确定如何完成此操作。
我该如何实现?
请注意,该代码已被剥离以仅显示相关部分。
<label for="ut2" onclick="messi_fan('ut2');">User Type 2</label>
<label for="ut1" onclick="messi_fan('ut1');">User Type 1</label>
<a id = "link" href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin'); ?>"> CLICK ME</a>
<script>
function messi_fan(type){
url = " <?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/'; ?>";
new_url = url+type;
$("#link").attr('href',new_url);
}
</script>
您可以像这样使用jQuery :
$('#container_id input[type=radio]').each(function(){
rad=$(this);
rad.click(function(e){
#get new url from PHP
$.get ("file_that_returns your_url.php?user_type="+rad.val(), function(data) {
$("#yourlinkid_here").attr("href", data);
});
# $("#yourlinkid_here").attr("href", $("#yourlinkid_here").attr("href")+rad.val());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.