Show whether dynamic radio button is checked or not - php

<?php $result2 = db_getsub( array('campaigns_id' => $SYS_campaign) );
if (!is_null($result1)){
while ( $row = $result2->fetch_object() )
{
?>
<div class="item">
<div style="text-align: left;">
<div class="choicetitle">
<input type="radio" id="Radio1" checked="checked" title="" name="subtype" value="<?php echo $row->subid; ?>" /><?php echo $row->sub_name; ?></div>
<div class="choicedesc descrip">
<?php echo $row->gavdescrep; ?> </div>
<!-- <div class="choicelesmer">
Les mer
</div>-->
<div class="choicedesc1">
<?php echo $row->gavdescrep; ?> </div>
</div>
<span><?php
$value=$row->price;
if($value != 0)
{
echo $row->price; ?>,-
<?php }else { echo ""; } ?> </span>
<span class="bgcolor discprice">
<?php echo $row->pricediscount; ?>,-
</span>
</div>
<?php } } ?>
I want to show default 1st radio button checked now its showing last button checked as it is coming dynamically any help???

You are using checked="checked" for all your radio buttons in the loop. So, last assigned radio button retains the checked property. To solve the problem, Conditionally set checked="checked" only for the first looping
if(loopingFirstTime){
$checked = ' checked="checked" ';
}else{
$checked = ' ';
}
and then ....
<input type="radio" name="radio1" '.$checked.' />
Not tested this .... please check the syntax

Use checked=checked property to make a radio button checked by default:-
Blue
http://www.w3schools.com/jsref/prop_radio_defaultchecked.asp
And to handle that with jquery:-
jQuery("#radio_1").attr('checked', 'checked');
How to check a radio button with jQuery ?

Like the other guys have already said, you have only implemented one radio button.
You can try jsfiddle.net for example to try out you code and quickly see your changes.
Btw: I'm not quite sure what you're trying to do in you last row: Is that a leftover or those this belong to the wrapped-around code?

Related

How to show echo "checked" edit in the checkbox?

I'm writing a system and have a checkbox in a form.
My problem is, when i press the edit button, the checkbox cannot show me the tick in the checkbox, I have tried to change checkbox type to text, it can show me the value. I have set the value if value = 1 is tick, if value = 0 is no tick. How can shows up tick in the form? Anyone can guide me to solve it?
Below is my coding:
Checkbox
<div class="form-group col-lg-6">
<label class="control-label col-lg-4">Pricing<span style="color:red;"> </span></label>
<div class="col-lg-8">
<input type="text" name="rm_option" id="rm_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?> ><strong> RM </strong></input>
<input type="text" name="point_option" id="point_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Full Point </strong></input>
<input type="text" name="partial_option" id="partial_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Partial Point + RM </strong></input>
</div>
</div>
Checkbox function
<?php
$sql = "select * from promotion_list where id=" . $_GET['id'];
$arr_sql = db_conn_select($sql);
foreach ($arr_sql as $rs_sql) {
foreach ($rs_sql as $key => $value) {
?>
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php
}
?>
When I press the edit button, other column can show up in the form, only the checkbox cannot show me the tick. Below is the output picture:
Output
If I change Checkbox type to text, below is the output (Prove inside the checkbox got value)
Output 2
I have stuck in this problem already 1 week, hope someone coding hero can guide me to solve this problem. Thanks a lot.
Add below code exactly after this line:
$("#<?php echo $key ?>").val("<?php echo $value?>");
it became:
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php if($value == 1){ ?>
$("#<?php echo $key ?>").attr("checked", true).prop("checked", true);
<?php } ?>
its clear that you are using jquery to work on the checkbox.

How to make a 5 checkbox to be marked with values from databases

i want to make a form with 5 checkbox that will be retrieved from my table genres that contain(ID, name) of books (from database)-this works, and i want to mark old genres from my table book_genre, this contain(ID,ID_book, ID_genre) and then to make an update with new genres - this did not work.
I did this:
<div>
<input type="checkbox" name="ID_gen<? echo $id=$row_genre_new['ID']; ?>"
value="1" <? if($row_genre_old['ID_genre'] ) echo 'checked="checked"'; ?> />
<? echo $row_genre_new['name']; ?>
</div>
<!--
<div>
<input type="checkbox" name="ID_gen<? echo $id=$row_genre_old['ID']; ?>"
value="1" checked="checked" />
<? echo $row_genre_old['name']; ?>
</div>
-->
<? } ?>
You would loop through database results echoing a checkbox for each database entry with their id as the value and check whether it has been assigned in this case and if so echo checked

How to work this if condition in form and codeigniter

i am using this condition for check box value in codeigniter i want if $v_d value take go in 3rd condition ,and value $v_v then go in 2nd condition and if avail both value then go 1st condition.
print_r($this->input->post());
$id =$this->input->post('id');
$v_v=$this->input->post('d_v');
$v_d=$this->input->post('d_d');
if(isset($v_d) && isset($v_v)){
echo"hwqq";
}
elseif(isset($v_v)){
echo"hello1";
}
elseif(isset($v_d)){
echo"he2";
}
HTML CODE iS THIS
<form action="<?php base_url()?>index.php/index/home_email_update" method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<h5><?php echo $msg;?></h5>
<?php if($f['subscription_discount_voucher']== 1){?>
<div class="email"><input type="checkbox" name="d_v" value="0" id="textfield"> Unsubscribe from Discount Voucher Emails.</div>
<?php }else{?>
<div class="email"><input type="checkbox" name="d_v" value="1" id="textfield"> Subscribe from Discount Voucher Emails.</div>
<?php }
if($f['subscribe_daily_deal']==1){ ?>
<div class="email"><input type="checkbox" name="d_d" value="0" id="textfield"> Unsubscribe from Daily Deal Emails. </div>
<?php } else{?>
<div class="email"><input type="checkbox" name="d_d" value="1" id="textfield"> Subscribe from Daily Deal Emails. </div>
<?php }?>
<div class="email_1"><button type="submit" class="submit_new">Change</button></div>
</form>
Try this,
$id =$this->input->post('id');
$v_v=$this->input->post('d_v');
$v_d=$this->input->post('d_d');
if(isset($v_d) && !isset($v_v)){
echo "hwqq";
}
else if(!isset($v_d) && isset($v_v)){
echo "hello1"
}
else if(isset($v_d) && isset($v_v)){
echo "he2";
}
Since $this->input->post() returns FALSE if there aren't any matches in the $_POST superglobal, isset check is not going to be an appropriate check here.
Instead you could try using empty:
if(!empty($v_d) && !empty($v_v)){
echo"hwqq";
}
elseif(!empty($v_v)){
echo"hello1";
}
elseif(!empty($v_d)){
echo"he2";
}
Please make sure to check Return values of empty for the list of things considered empty.

Php-CodeIgniter Help needed

I am taking values form database in my php project. The values are taken and displayed fine. The problem is I have a button with each value. And I need a separate link for each button to see which data is selected. So that the functioning would depend on selected data. But each button gets the same name. This is my code.
<?php foreach($query as $row): ?>
<hr id="line">
<div id="wrong">
<!--<button id="connect" type="submit">Connect</button>-->
<span id="name">
<?php
if($row->fname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->fname; ?> </span>
<?php
}
else {
echo $row->fname;
$this->session->set_userdata('re_use',$row->fname);
}
?>
&nbsp
<?php
if($row->lname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->lname; ?> </span>
<?php
}
else {
echo $row->lname;
}
?>
</span>
<!-- <select name="back_up">
<option selected><?php echo $row->userid ?></option>
<option>a</option>
</select>-->
<button id="connect" type="submit" >Connect</button>
</div>
<?php endforeach; ?>
Please Help.
Keep a hidden input and set the value to it on click of the button. You can store the value to the radio button and use it inside the JS function.
function setSelected(elm)
{
document.getElementById('inputID').value = elm.value;
}
Outside the foreach loop.
<input type='hidden' name='SelectedUser' id='inputID' />
Inside loop, in the generated code for button.
<button id="connect" onclick='setSelected(this)' type="submit" value="<?php echo $row->userid; ?>">Connect</button>
In server side you can get the value using $_POST['SelectedUser'] when the page is submiited after selecting the user.

Only one value from an hidden input is being displayed

<?php
foreach($_color_swatch as $_inner_option_id){
preg_match_all('/((#?[A-Za-z0-9]+))/', $_option_vals[$_inner_option_id]['internal_label'], $matches);
if ( count($matches[0]) > 0 ) {
$color_value = $matches[1][count($matches[0])-1];
?>
<li>
<input type="hidden" id="fakecolor" value="<?php echo $color_value;?>"/>
<div onclick="alert(document.getElementById('fakecolor').value);">
<img src="<?php echo $color_value;?>.png" /></div>
</li>
<?php
}
}
?>
This works for displaying the images, using the $color_value but i need to pass the value from hidden input to another javascript function.
And when i click on div it displays only one value no matter how many are inside the foreach.
Can anyone give me a little help? Thanks.
This is the output:
<li>
<input type="hidden" id="fakecolor" value="red"/>
<div onclick="alert(document.getElementById('fakecolor').value);"><img src="red.png"/></div>
</li>
<li>
<input type="hidden" id="fakecolor" value="blue"/>
<div onclick="alert(document.getElementById('fakecolor').value);"><img src="blue.png"/></div>
</li>
<li>
<input type="hidden" id="fakecolor" value="white"/>
<div onclick="alert(document.getElementById('fakecolor').value);"><img src="white.png"/></div>
</li>
<li>
<input type="hidden" id="fakecolor" value="green"/>
<div onclick="alert(document.getElementById('fakecolorx').value);"><img src="green.png"/></div>
</li>
But when i click on each of the divs, it displays only the value of the second, blue.
Try something like this:
<?php
$cont = 0;
foreach($_color_swatch as $_inner_option_id){
preg_match_all('/((#?[A-Za-z0-9]+))/', $_option_vals[$_inner_option_id]['internal_label'], $matches);
if ( count($matches[0]) > 0 ) {
$color_value = $matches[1][count($matches[0])-1];
?>
<li>
<input type="hidden" id="fakecolor<?php echo $cont; ?>" value="<?php echo $color_value;?>"/>
<div onclick="alert(document.getElementById('fakecolor<?php echo $cont; ?>').value);">
<img src="<?php echo $color_value;?>.png" /></div>
</li>
<?php
}
$cont = $cont + 1;
}
?>
In this way every input hidden have a different id, the same whit the onclick function.
Saludos ;)
I dont quite understand what you're trying to do, still:
<input type="hidden" id="fakecolor" value="<?php echo $color_value;?>"/>
<div onclick="alert(document.getElementById('fakevalue').value);">
First line: you're using an unique id while looping so you'll get several elements with the same id and you'll always end up with the first one with document.getElementById.
Second line: aren't you supposed to get the value of the hidden field ? (ie #fakecolor and not #fakevalue as you're getting).
You have multiple IDs on the same page which will cause getElementById to fail. Why don't you loop and construct your JavaScript like so:
<div onclick="alert("<?php echo $color_value; ?>");">
If you ever move from just alert you can have whatever JavaScript function with a string parameter to accept the color value.

Categories