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

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

本文关键字:href PHP javascript 交互 动态 单选按钮 状态 链接      更新时间:2023-09-26

我在codeigniter中使用MVC框架,我有一个视图,我有这个链接(请注意,href值是通过调用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在点击CLICK ME之前被选中

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

用户类型2在点击CLICK ME之前被选中

<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());
    });
    });