I am using radio boxes to save values into an array, the issue I am having is trying to automatically check the checkbox for the corresponding value after the page is refresh or whatever
I have tried the following;
<h3 style="margin-bottom: 0px;">Floating</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="multi"<?php echo ('multi' == get_option( 'noticeType' ))? 'checked="checked"':''; ?> /></input>
<h3 style="margin-bottom: 0px;">Floating</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="floating"<?php echo ('floating' == get_option( 'noticeType' ))? 'checked="checked"':''; ?> /></input>
The value is being saved when I click either one array (size=6)
'noticeType' => string 'multi' (length=5) but the corresponding checkbox isnt being checked.
Anyone help?
output markup
<div style="margin: 10px;">
<h3 style="margin-bottom: 0px;">Multiple</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="multi" /></input>
<h3 style="margin-bottom: 0px;">Floating</h3></br>
<input type="radio" name="lu_ban_data[noticeType]" value="floating" /></input>
</div>
the "checked" is not being printed
You say its in an array? how are you getting that array?
To use get_option() at some point you have to use update_option()
Try echo get_option( 'noticeType' ) and see what is stored in the wordpress option.
I made it work like so;
value="multi" <?php $value = get_option('lu_ban_data'); checked( $value['noticeType'], 'multi' ); ?>
Related
I have a select option, I have a written function when I click a particular option it will fetch values related to that option and this particular value will be checked using the checkbox and other values will be unchecked.
I need to hide those unchecked values or else I need to keep unchecked values below the toggle button!! I am stuck right now!
<input type="checkbox" class="value" name="value[]" id="value<?=$i?>" value="<?=$brow["process"]?>" data-process-name="<?=$brow["process_name"]?>"/> <?=$brow["process_name"]?>
also, I am getting checkbox value as an array!
help me to solve this!
I added screenshot of checkbox where I get unchecked values below the checked values.
JS:
if(jsonProcessArr.length > 0){
$(".proces_name_value").each(function(){
if($.trim(this.value) != ""){
if ($.inArray(this.value, jsonProcessArr) != -1){
$(this).prop("checked",true);
}
else{
$(this).prop("checked",false);
} // here I check values from json and if there is the value inside json it will check otherwise uncheck//
after I receive checked and unchecked values together!!
mycode:
<div class="row form-group ">
<?php
$pquery = "SELECT distinct(process_name),process_nid FROM bi_process_info WHERE status=true";
$presult = mysqli_query($conn, $pquery);
$i =1;
while ($brow = mysqli_fetch_array($presult, MYSQLI_ASSOC))
{
?>
<div class="col-lg-3 col-md-3 col-sm-12 form-group">
<input type="checkbox" class="proces_name_value process_name" name="process_value[]" id="process_value<?=$i?>" value="<?=$brow["process_nid"]?>" data-process-name="<?=$brow["process_name"]?>"> <?=$brow["process_name"]?></input>
</div>
<?php
$i++;
} ?>
</div>
already i added my ajaxcall code !! so after that ajax call i added function where it hides unchecked checkbox :
function uncheck(){
$('.process_name').each(function(){
$t_this= $(this);
if($t_this.is(':checked')){
$t_this.show();
}
else
{
$t_this.parent().hide(); // this hides my element but when i click another option i hiding values but it hided values that are already hided
i dont want to do that!!
is there any way to refresh the hided elements?
}
});
}
Firstly you're getting the value back as an array because the name="value[]" contains '[]'. Drop the braces and it will return as a single value. Second, all child checkboxes that you want to hide should be in the nested html or have dedicated classes to handle this.
I would recommend the below (Note: the children could be an array if you desire)
<div>
<div>
<input type='checkbox' class='someCheckbox' name='value'>
<div class='children'>
<input type='checkbox' name='childValue1'>
<input type='checkbox' name='childValue2'>
</div>
</div>
<div>
<input type='checkbox' class='someCheckbox' name='value'>
<div class='children'>
<input type='checkbox' name='childValue3'>
<input type='checkbox' name='childValue4'>
</div>
</div>
</div>
<script>
jQuery(document).on('change','.someCheckbox',function(event){
let checkbox = jQuery(event.target);
if(checkbox.prop('checked')){
checkbox.siblings('.children').show();
}else{
checkbox.siblings('.children').hide();
}
})
</script>
Since I already know how to use the $_GET to store value from a radio button, this time I wanted it to be stored in a PHP session. The page reloads after clicking a button. I used javascript and PHP to do this. What I intended to do was the value from the radio button when clicked goes to the post() function. The post() sends the value to the Test.php file through $.post. The test.php file sends back the value to be stored in the variable a which will later output in the PHP session variable. The $state will be used to make the radio buttons stay checked after reload. However, the code has an error as it does not let me click any of the radio buttons and after clicking one, the whole page stops working and other buttons cannot be clicked anymore.
<script>
function post(){
var eqstate = $('#state').val();
$.post('test.php', {statevalue:state},
function(data){
var a = data.value;
});
}
</script>
<?php
$_SESSION['state']="<script> document.write(a)</script>";
$state=$_SESSION['state'];
?>
<span style="margin-left:35px;">STATE</span><br><br>
<form>
<input type="radio" name="state" id="state" value="allstate" onclick="post();" <?php echo
$state==='allstate' ? 'checked' : '' ?>> All State <br>
<input type="radio" name="state" id="state" value="new" onclick="post();" <?php echo $state==='new' ? 'checked' : '' ?>> New EQ: <br>
<input type="radio" name="state" id="state" value="old" onclick="post();" <?php echo $state==='old' ? 'checked' : '' ?>> Old EQ: <br>
<input type="radio" name="state" id="state" value="unknown" onclick="post();" <?php echo $state==='unknown' ? 'checked' : '' ?>> Unknown State: <br>
</form>
Test.php code:
<?php
$a=$_POST['statevalue'];
echo $a;
?>
I am trying to find a way to paste items from the clipbaord into a php form. Essietnailly not having to copy line by line into the form.
So for example if in my clipboard I have:
Sally Susaze
SSSusaze#gmail.com
304-506-7054
And want to be able to paste that information into this form in one step:
What do I do? I know that is easy enough to create a button that copies contents from a form but ideally i'd like to have a button that pastes the clipboard into the form (as I said above)
This is an example of a button that copies: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_copy_clipboard2
And here is a code snippet of the form (the image above)
if (in_array((int) $tpl['option_arr']['o_bf_include_name'], array(2,3)))
{
?>
<p>
<label class="title"><?php __('lblBookingName'); ?></label>
<span class="inline-block">
<input type="text" name="c_name" id="c_name" class="pj-form-field w400<?php echo $tpl['option_arr']['o_bf_include_name'] == 3 ? ' required' : NULL; ?>" />
</span>
</p>
<?php
}
if (in_array((int) $tpl['option_arr']['o_bf_include_email'], array(2,3)))
{
?>
<p>
<label class="title"><?php __('lblBookingEmail'); ?></label>
<span class="inline-block">
<input type="text" name="c_email" id="c_email" class="pj-form-field w400<?php echo $tpl['option_arr']['o_bf_include_email'] == 3 ? ' required' : NULL; ?>" />
</span>
</p>
<?php
}
if (in_array((int) $tpl['option_arr']['o_bf_include_phone'], array(2,3)))
{
?>
<p>
<label class="title"><?php __('lblBookingPhone'); ?></label>
<span class="inline-block">
<input type="text" name="c_phone" id="c_phone" class="pj-form-field w400<?php echo $tpl['option_arr']['o_bf_include_phone'] == 3 ? ' required' : NULL; ?>" />
</span>
</p>
<?php
}
if (in_array((int) $tpl['option_arr']['o_bf_include_notes'], array(2,3)))
{
?>
<p>
<label class="title"><?php __('lblBookingNotes'); ?></label>
<span class="inline-block">
<textarea name="c_notes" id="c_notes" class="pj-form-field w500 h120<?php echo $tpl['option_arr']['o_bf_include_notes'] == 3 ? ' required' : NULL; ?>"></textarea>
</span>
</p>
<?php
}
Thanks!
To get the string from the clipboard, check out this article.
Then: you'll need to find some way to determine which parts of the copied value go into which form. I think splitting on a newline would work for the example above, but what if they're in a different order? To check for that you will probably need a regex.
Then, since each input has an id, you can select that element and change its value. A possible solution:
let infoArray = string.split('\n')
document.getElementById('c_name').value = infoArray[0]
When i creating a quiz, i found something interesting. When trying to change content with PHP using jQuery everything changing in web. row1 as row 1, row2 as row2; question display correctly, but when i would like to get value from question 2 i gat value from question 1. So question only load value, doesn't mean if i'm on question 1,2,3,4,5,6 or 10. I get value from 1st displayed question.
jQuery:
$(document).ready(function(){
var quizCount = 1;
$("#next").click(function(){
quizCount = quizCount+1;
$("#quiz").load("load-quiz.php",{
quizNewCount : quizCount
});
});
$('.service-container').each(function(){
$("input[name=quizid"+quizCount+"]:radio").change(function () {
return ans = $("input[name=quizid"+quizCount+"]:checked").val();
});
});
});
That is a part of my code, but most interesting, as you see i using increasing value for "quizId". Is same file have div with id quiz.
PHP inside quiz:
<h3><?=$row['topic'];?></h3>
<h4><?=$row['question'];?></h4>
<div>
<input type="radio" name="<?= $choice ?>" value="<?= $ans_array[0]?>">
<label id="<?= $ans_array[0];?>">A) <?= $ans_array[0];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[1]?>">
<label id="<?= $ans_array[1];?>">B) <?= $ans_array[1];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[2]?>">
<label id="<?= $ans_array[2];?>">C) <?= $ans_array[2];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[3]?>">
<label id="<?= $ans_array[3];?>">D) <?= $ans_array[3];?>
</label></br>
</div>
<div>
<input type="radio" name="<?= $choice ?>" value="<?=$ans_array[4]?>">
<label id="<?= $ans_array[4];?>">E) <?= $ans_array[4];?>
</label></br>
</div>
same code is a part of my grade.php file.
Resuming:
server side $choice = client side ans;
every time value is equal. but i get result only for 1st displayed question , when i load my grade.php i don't have a output because, i getting value only from 1st question.
Can somebody give me some clue?!
Update:
That php display title from database
<h3><?=$row['topic'];?></h3>
and i get like:
"Javascript "
when i use ajax call to load next title from database:
$(document).ready(function(){
var quizCount = 1;
$("#next").click(function(){
quizCount = quizCount+1;
$("#quiz").load("load-quiz.php",{
quizNewCount : quizCount
});
});
I get "CSS", when click again i get "HTML", click again "jQuery" etc... everything display correct, but not for value, every time i see "Javascript" value.
Question updated
Ok, I manage somehow to get value of checkbox, but now i have another issue.
This is my view, rssFeeds.php
<?php foreach( $feeds as $row ) : ?>
<div class="row">
<div class="col-md-2 col-sm-3 text-center">
<a class="story-title"><img alt="" src="http://www.solostream.com/wp-content/uploads/2014/06/20140110080918_0555.jpg" style="width:100px;height:100px"></a>
<label class="checkbox-inline">
<input type="checkbox" class="chkbox" value="chkbox_<?php echo $row->category; ?>_<?php echo $row->id; ?>"> Mark Read
</label>
</div>
<div clas="col-md-10 col-sm-9">
// here are my feeds....
</div>
</div>
<?php endforeach; ?>
I have this script, which take checkbox value, and send it to my controller:
<script>
$(document).ready(function(){
$(document).on('click','.chkbox',function(){
var id=this.value;
$.ajax( {
type: "POST",
context: "application/json",
data: {id:id},
url: "<?php echo site_url('rssFeedReader/markReadUnread'); ?>",
success: function(msg)
{
// what should i do here ?....
}
})
});
});
</script>
In my controller, i just load a model which change a value on my database, 0 or 1( meaning read or unread).
The problem is that nothing change on my table...
I need to put something in that .succes function in ajax ? What.. ? I just need to change one value in my database....
#James-Lalor has the answer you are looking for, but I'll expand upon it.
You can give inputs the same name (radio buttons, checkboxes) to have them interact with each other. In the case of radio buttons it's actually required to have the same name to mark and unmark others. However in this case we will use <input name=example[]> note the [], this means when you do an ajax post (or any post) it will send all the values checked as an array.
So following James' suggestion, you would do a <input name="checkbox[<?php echo $row->id?>]" to which you can post using $.post(url, data, callback), the easiest way to do this would be to put it into a form, assign the form an id, do a serialized post. You could do something like:
<form id="rss_form" method="post" action="javascript:rssUpdate();">
<input name="checkbox[<?php echo $row->id?>]" type="checkbox"/>
<input type="submit" value="submit"/>
</form>
<script>
function rssUpdate()
{
$.post(url/to/post/to, $("#rss_form").serialize());
}
</script>