简体   繁体   English

基于单选按钮状态的链接点击动态生成href:PHP与javascript交互

[英]Dynamic href generation on link click based on radio button state : PHP interaction with javascript

I am using MVC framework in codeigniter and I have a view where I have this link (please note that href value is generated by call to a PHP function which in turn takes 'auth/mylogin' as a parameter: 我在codeigniter中使用MVC框架,并且有一个链接指向的视图(请注意, href值是通过调用PHP函数生成的,而PHP函数又将'auth/mylogin'作为参数:

<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin'); ?>"> CLICK ME</a>

Now I have a radio button on the same view page. 现在,我在同一视图页面上有一个单选按钮。

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

Now what I want is this: I want to pass the user type value in addition. 现在我想要的是:我还想传递用户类型值。 What I mean is that depending on which radio button the user has chosen before he clicks the link, I want the code to be: 我的意思是,取决于用户在单击链接之前选择了哪个单选按钮,我希望代码为:

User Type 1 radio selected before clicking CLICK ME 单击“单击我之前”已选择“用户类型1”单选

<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/ut1'); ?>"> CLICK ME</a>

User Type 2 radio selected before clicking CLICK ME 单击“单击我之前”已选择“用户类型2”单选

<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/ut2'); ?>"> CLICK ME</a>

Since there is not submit, I assume I have to use javascript to do this. 由于没有提交,我假设我必须使用javascript来做到这一点。
But since javascript is client side and PHP serverside, I am not sure how to get this done. 但是由于javascript是客户端和PHP服务器端,所以我不确定如何完成此操作。

How do I achieve this? 我该如何实现?
Please note that the code has been stripped to show only the relevant part. 请注意,该代码已被剥离以仅显示相关部分。

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

You can use jQuery like this: 您可以像这样使用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.

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