开发者

Radio button checked issue

My question was about the browser matter as far as I see. I asked is there any solution to tell browser to move checked="checked" between radio buttons. I thought there will be a quick solution for this but this case is just confusing. I changed my code now. I use select list element now.

I have a form, and I get the actual settings with php, but the matter is this:

When the radio button 1 comes checked="checked" if that is actual, after it when I change to other option in post I get checked="checked"'s value, I was wondering is there a solution for this or Javascript is needed something like onclick"this.checked otherone uncheck" ?

if($type==1) { $like = 'checked="checked"'; } else { $ref='checked="checked"'; }

<form method="post" acti开发者_StackOverflow社区on="process.php" id="fbform" name="fbform">
<div class="radio">
<p>Like button: </p>
<input type="radio" name="tipi" <?=$like?> value="1" />
</div>
<div class="radio1">
<p>Link:</p> 
<input type="radio" name="tipi" <?=$ref?> value="2" />
</div>
</form>

Process.php file code

<?php
$mysql->update('settings'," tipi='".$_POST['tipi]."' ");
?>


checked is a marker tag - it does not care , as long if the word 'checked' is there.


Here is what I would do:

<div class="radio">
  <p>Like button: </p>
  <input type="radio" name="tipi" id="tipi1" value="1"<?php if ($type == 1) echo ' checked="checked"'; ?> />
</div>
<div class="radio1">
  <p>Link:</p> 
  <input type="radio" name="tipi" id="tipi2" value="2"<?php if ($type != 1) echo ' checked="checked"'; ?> />
</div>

I have changed the id's because you had two elements with the same id, which will never work, even if those elements are part of the same group.


You can do better with this. Also, IDs are used only for CSS or JS, and moreover must be UNIQUE.

<div class="radio">
<p>Like button: </p>

<input type="radio" name="tipi" id="tipi" <?php echo ($type == '1')?'checked="checked"' : '';?> value="1" />

</div>

<div class="radio1">

<p>Link:</p> 
<input type="radio" name="tipi" id="tipi2" <?php echo ($type=='2')?'checked="checked"' : '';?> value="2" />

</div>

Then you access them in php

$tipi = $_POST['tipi']; //either 1 or 2


you need very simple changes in your code..See below code:

first of all you need to define variables at very top of the page/Code:

$like = ""; 
$ref = "";

Now your code will working good as per my knowledge..

Thanks.


Ghostology: Not sure about your query ... But all I can do is explain you the working of the radio button.

suppose there are two radio buttons inside a form:

<input type="radio" name="radioTest" value="1" checked />
<input type="radio" name="radioTest" value="0"  />

Here checked specifies the button which will be checked(or turned on) during page load.

Now suppose we check the second button and submit this form, now we get the value 0 against the parameter 'radioTest' as radio button with value 0 is checked.

Now when page loads again with some value of radio buttons. You would want the proper radio button to be checked. For this case I guess u can use if/else condition like: <% if(radioTest.value == 1){ %> <% }else{ %> <% } %>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜