calculate values on basis of text box entry - php

i have one row of text boxes which is generated with php loop and mysql id.
<input class="text_field1" id="<?php echo $i ?>" name="q_<?php echo $row['pid']; ?>"
type="text" style="width:150px" />
<input disabled="disabled" class="text_fieldD" id="p_<?php echo $row['pid'];
?>" name="p_<?php echo $row['pid']; ?>" type="text" value="<?php echo $row['pprice'] ?>" />
<input disabled="disabled" class="text_fieldD" id="t_<?php echo $row['pid']; ?>"
name="t_<?php echo $row['pid']; ?>" type="text" />
what i want is
when i enter any value in text box with class text_field1, i want to calculate total in text box name t_.... (qty * price ,p_....)
i try this code its works little bit but not all i want , my JQUERY code is
$('.text_field1').bind("focus blur change keyup", function(){
var $el = $(this);
var numPallets =IsNumeric1($el.val());
});
Thanks

I think this should work. I only binded it to keyup in this example:
$('.text_field1').keyup(function() {
var pid = $(this).attr("name").split("_")[1]; // row pid
var q = $(this).val() * 1;
var p = $("#p_"+pid).val() * 1;
$("#t_"+pid).val(q * p);
});

Related

select option to fetch other inputs

What Im trying to do is select item name where is ID and if item selected it automatically adds that item serial number to serial number input.
I think it should be used ajax maybe but I dont know how exactly.
Item Name:<br />
<div class="select-holder">
<i class="fa fa-caret-down"></i>
<?php
if($_items->count_items() == 0)
echo '<select name="item-name" disabled><option value="no">You need to add a item first</option></select>';
else{
echo '<select name="item-name" id="change-item">';
$items = $_items->get_items_dropdown();
while($item = $items->fetch_object()) {
echo "<option value=\"{$item->id}\">{$item->name} - {$item->serialnumber}</option>";
}
echo '</select>';
}
?>
</div>
Serial Number:<br />
<div class="ni-cont">
<input type="text" name="item-serialnumber" class="ni" disabled/>
</div>
I have also other inputs like description, price and etc.. to fetch these inputs with these values.
example: i need get data to my field from select option id (value)
<input type="text" name="item-serialnumber" value="<?php echo $_items->get_item_serialnumber($id); ?>" class="ni" disabled/>
<input type="text" name="item-serialnumber" value="<?php echo $_items->get_item_description($id); ?>" class="ni" disabled/>
That value is already shown inside your select-box you can simply use onchange event and then use $(this).find("option:selected").text().split('-')[1] this will give serial no then put it inside your input-box using $('[name=item-serialnumber]').val(somevalue) .Also ,you can send id via ajax to your server page and fetch result.
Demo Code :
$("#change-item").on("change", function() {
var value = $(this).find("option:selected").text().trim().split('-')[1]
$("[name='item-serialnumber']").val(value)
var id = $(this).val();
//use ajax :
$.ajax({
url: "somepgaename.php", //the page containing php script
type: "post", //request type,
dataType: 'json',
data: {
id: id //send id as well
},
success: function(data) {
console.log(data.yourfieldname);
//put inside your input fields values like :
// $("[name='item-serialnumber']").val(data.serialno)
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Item Name:<br />
<div class="select-holder">
<i class="fa fa-caret-down"></i>
<select name="item-name" id="change-item">
<option value="1">Abc-123</option>
<option value="2">Abc2-122</option>
<option value="3">Abc2-132</option>
</select>
</div>
Serial Number:<br />
<div class="ni-cont">
<input type="text" name="item-serialnumber" class="ni" disabled/>
</div>
Then , at your server end get the id pass via ajax using $_POST['id'] then fetch result using select query and send them back to ajax using echo json_encode($somearray); i.e:
$id = $_POST['id'];
$stmt = $pdo->prepare("SELECT * FROM tablename WHERE id=:id");
$stmt->execute(['id' => $id]);
//put fetch result inside array..send back to ajax
//echo json_encode($somevalue);

I want to use PHP variable in jQuery in loop

I want to use PHP variable in jQuery.
I am having a 50 students data in table which having textbox and radio button.
By default textbox is hidden but when I clicked on radio button textbox show in table.
$("input[type='radio']").on('change', function() {
if ($(this).val() == "2")
var id = '<?php echo json_encode($absent); ?>';
//$("input[type='text']").show();
else
$("input[type='text']").hide();
});
PHP code:
<input name="nxtmark<?php echo $row[0]; ?>" type="text" id="nxtmark<?php echo $row['id']; ?>" onblur="onleave(this.name);" maxlength="2" placeholder="Enter Mark" hidden="" />
<div class="noerrordiv" id="dnxtmark<?php echo $row[0]; ?>">Mark must not be blank</div>
</td>
<td>
<div align="justify">
<input type="radio" id="rdopresent" name="rdopresent<?php echo $row['id']; ?>" checked="checked" value="1" />Present
<input type="radio" id="rdoabsent" name="rdopresent<?php echo $row['id']; ?>" value="2" />Absent<br />
</div>
</td>
</tr>
You can show / hide text box without using PHP variable. See below code -
$("input[type='radio']").on('change',function() {
//find input text from the previous 'td' for which name starts with 'nxtmark'
var $textBox = $(this).closest('td').prev('td').find('input[name^=nxtmark]');
//show hide text box as per radio button value
if($(this).val() == "2")
$textBox.show();
else
$textBox.hide();
});
If your Problem is just to use PHP variable in jquery then following examples will help you.
if you have to user a php array in jquery then use json_encode in PHP not in SCRIPT.
<?php
$a = array(1,2,3,4);
$a = json_encode($a);
$b = "hello";
?>
<script>
$(document).ready(function(){
alert("<?php echo $a; ?>");
alert("<?php echo $b; ?>");
});
</script>

Jquery select specific element

I have a list of radio tags with a diffrent id(post-1,post2 etc).
I am trying to preforme get request with the specific post id by clickign the radio.
This is the html:
$("#pro").click(function(){
var post = $(this).val();
alert(post);
$.get("<?php echo get_template_directory_uri(); ?>/jquery.php",{type: 'post', userid :'<?php echo get_current_user_id();?>', postid: '<?php echo $post->ID; ?>', pro : "pro"},function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
<input type = "radio" value = "<?php echo $post->ID;?>" class="pro-radio" name="pro-<?php echo $post->ID;?>" id="pro" <?php echo $GLOBALS['lock']; ?> <?php echo $pro_select; ?>> <label for="pro" class="pro-radio" >pro</label>
and this is the jquery:
When I press one of them, all of the radios oprate the jquery.
Thank you.
I think you reverted name and ids,
try this html
<input type = "radio" value = "<?php echo $post->ID;?>" class="pro-radio" id="pro-<?php echo $post->ID;?>" name="pro" <?php echo $GLOBALS['lock']; ?> <?php echo $pro_select; ?>> <label for="pro" class="pro-radio" >pro</label>
try adding a different class for each radio button and select by class name
var post = $('.className').val();
alert(post);
I think your HTML is a bit weird. You use the same id but different name attributes for your radios. Assume, you have this HTML:
<input type="radio" name="pro" value="1" id="pro-1"> <label for="pro-1">pro 1</label>
<input type="radio" name="pro" value="2" id="pro-2"> <label for="pro-2">pro 2</label>
<input type="radio" name="pro" value="3" id="pro-3"> <label for="pro-3">pro 3</label>
Then use this JS to get the selected value:
$('input[name=pro]').change(function() {
if ($(this).is(':checked')) {
alert($(this).val());
}
});
Please note, that the radios are selected by their (identical) name, but not by the (different) id. Then you have to verify the current element is the selected one using .is(':checked') and finally I use change() instead of click() if the element is selected via keyboard input.
Check it out here: http://jsfiddle.net/seofu1t4/

jQuery Get Value From Looping in PHP

I'm using jQuery. Now I want to get value base on ID value from textbox loop.
Example :
<?php
while
{
$id = $d['id'];
?>
<input type="text" name="text" id="txt" value="<?php echo $id; ?>"/>
<input type="submit" class="btn_confirms" id="txt<?php echo $id; ?>" value="Go"/>
<?php
}
and jQuery like this:
$(function() {
$(".btn_confirms").click(function()
{
var txt_id = $('#txt').val();
alert(txt_id);
}
});
Example if I have 3 data.
The problem is, when I click button Go, it just show first value of PHP loop. Another button Go 2 & 3 is show same value.
Any help, please.
You can't have duplicate IDs on the page, they should be unique. For repeating elements either use classes, or make IDs unique (like you're doing for the buttons).
Something like this should work:
<input type="text" name="text" id="txt<?php echo $id; ?>" value="<?php echo $id; ?>"/>
<input type="submit" class="btn_confirms" id="button<?php echo $id; ?>" value="Go"/>
Then, to fetch the id of the input field from the click function, you can do something like this:
$(".btn_confirms").click(function()
{
var txt_id = $(this).attr('id').replace('button', 'txt');
var value = $('#'+txt_id).val();
alert(txt_id + ": " + value);
})
See it here: http://jsfiddle.net/shomz/R9s2R/
you can try this
<?php
while
{
$id = $d['id'];
?>
<input type="text" name="text" id="txt<?php echo $id; ?>" value="<?php echo $id; ?>"/>
<input type="submit" class="btn_confirms" id="btn<?php echo $id; ?>" textbox_id="#txt<?php echo $id; ?>" value="Go"/>
<?php
}
?>
<script>
$(function() {
$(".btn_confirms").click(function()
{
var textbox_id = $(this).attr('textbox_id');
var textbox_val = $(textbox_id).val();
alert(textbox_val);
});
});
</script>
DEMO

Passing loop values of child window to parent window and vice versa

I have a text box named subscriber name.when I double clicked on that text box,a child window should open.In that child window list of subscriber names are shown using while loop of mysql query.My problem is when I double clicked on it ll pass a value as undefined.This problems comes due to looping of subscriber names from db table.How can I solve it? Please help me soon.Here my code.
<script type="text/javascript">
function displaymessage(){
opener.document.cash_entry.sub_name.value = document.subscriber.subname.value;
self.close();
}
</script>
<form>
<?php
$sel=mysql_query("select * from add_ticket");
while($row=mysql_fetch_array($sel)) {
$subscriber=$row['subscriber'];
?>
<input type="text" name="subid" id="subid" value="<?php echo $subscriber; ?>"
ondblclick="displaymessage()" readonly="true">
<?php } ?>
</form>
You have not named your form and you named your field differently than in the HTML code and you have more than on element with the same name which makes it an array if more than one
Do this
<script type="text/javascript">
function displaymessage(fld){
opener.document.cash_entry.sub_name.value = fld.value;
self.close();
}
</script>
<form>
<?php
$sel=mysql_query("select * from add_ticket");
while($row=mysql_fetch_array($sel)) {
$subscriber=$row['subscriber'];
?>
<input type="text" name="subname"
id="subid<?php echo $subscriber; ?>"
value="<?php echo $subscriber; ?>"
ondblclick="displaymessage(this)" readonly="true">
<?php } ?>
</form>
by why not
<input type="button" value="<?php echo $subscriber; ?>"
onclick="displaymessage(this)" >
For more than one value, you can do
function displaymessage(fld){
var parts = fld.value.split("|");
opener.document.cash_entry.sub_name.value = parts[0];
opener.document.cash_entry.sub_id.value = parts[1];
self.close();
}
<input type="button"
value="<?php echo $subscriber; ?>|<?php echo $subscriberID; ?>"
onclick="displaymessage(this)" >

Categories