簡體   English   中英

基於單選按鈕狀態的鏈接點擊動態生成href:PHP與javascript交互

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM