onkeyup function inside for loop of php script - javascript

I want to use onkeyup function inside for loop of a php script.
The value in the second box in each row should be 5, 10, 15, 20, 25 respectively irrespective of the entry in the first box. But I am not getting the desired result. The final result (ie 25) is showing for each box.
<?php
for ($i=1;$i<=5;++$i)
{
${"k3".$i}=5*$i;
?>
<html>
<script>
function gftt2(xx1){
var drr2=<?php echo ${"k3".$i}?>;
document.getElementById(xx1).value=drr2;
}
</script>
<input type="text" size="1" maxlength="" id="<?php echo "dd1{$i}"?>" name="dd1[$i]" onkeyup="gftt2('<?php echo "dd4{$i}"?>')"/>
<input type="text" size="1" maxlength="" id="<?php echo "dd4{$i}"?>" name="dd4[$i]" /><br/>
</html>
<?php } ?>
Please help.

<html>
<head>
<script>
function gftt2(id, value)
{
document.getElementById(id).value = value;
}
</script>
</head>
<body>
<?php
for ($i=1;$i<=5;++$i) {
$value = 5*$i;
?>
<input type="text" size="1" maxlength="" id="dd1<?php echo $i ?>" name="dd1[<?php echo $i ?>]" onkeyup="gftt2('dd4<?php echo $i ?>', '<?php echo $value?>')"/>
<input type="text" size="1" maxlength="" id="dd4<?php echo $i ?>" name="dd4[<?php echo $i ?>]" /><br/>
<?php
}
?>
</body>
</html>
It's untested. :o

Related

How can I update any row rather than only the top row with the update button on each side in PHP and AJAX

just starting out so gonna sound like a noob question but how can I use the update button to update individual rows rather than only the top row.
Whenever I use the update button, the request only goes through if it was the top row.
This is the code for the index.php
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<center>
<h3>UPDATE DATA</h3>
<?php
$run = 0;
$conn = new mysqli('localhost', '', '', '');
$sql = "SELECT * FROM moderator";
$result = $conn->query($sql);
while ( $row=mysqli_fetch_assoc($result)) {
$run = $run + 1;
?>
<div>
ID<input type="text" id="id" value="<?php echo $row['id']; ?>">;
Moderator<input type="text" id="mod" value="<?php echo $row['name']; ?>">;
Category<input type="text" id="ctgr" value="<?php echo $row['category']; ?>">;
<button type="submit" id="update" rel="<?php echo $run; ?>">UPDATE</button>
</div>
<?php
}?>
</center>
<script>
$(document).ready(function(){
$("#update").click(function(){
var name=$("#mod").val();
var ctgr=$("#ctgr").val();
var id=$("#id").val();
$.ajax({
url:'update.php',
method:'POST',
data:{
name:name,
ctgr:ctgr,
id:id
},
success:function(response){
alert(response);
}
});
});
});
</script>
</body>
This is the code for the update.php
<?php
$conn = new mysqli('localhost', '', '', '');
$name=$_POST["name"];
$ctgr=$_POST["ctgr"];
$id=$_POST["id"];
$sql="UPDATE moderator set name='$name', category='$ctgr' where id='$id'";
if($conn->query($sql)===TRUE){
echo "DATA updated";
}
?>
You can probably see I'm trying to create an individual variable for each row but I can't figure it out. Sorry, code's very messy too.
I've removed datebase information but the rows display when credentials are inserted.
This is because you have duplicated input ids, you should have only one ID with the same value on html, this is valid some how, you don't get an error, but you will have trouble in javascript when you try to access those id, you will get only the first one.
To fix this you can remove id and put it as class:
<div>
ID<input type="text" class="id" value="<?php echo $row['id']; ?>">;
Moderator<input type="text" class="mod" value="<?php echo $row['name']; ?>">;
Category<input type="text" class="ctgr" value="<?php echo $row['category']; ?>">;
<button type="submit" class="update" rel="<?php echo $run; ?>">UPDATE</button>
</div>
and use this JS:
$(document).ready(function(){
$(".update").click(function(){
var container = $(this).closest('div'); // parent div
var name = container.find('.mod').val();
var ctgr = container.find('.ctgr').val();
var id = container.find('.id').val();
$.ajax({
url:'update.php',
method:'POST',
data:{
name:name,
ctgr:ctgr,
id:id
},
success:function(response){
alert(response);
}
});
});
});
All your rows input elements have same id attribute
Like id of first rows elements are like id, mod, ctgr
Also id of second rows elements are id, mod ctgr
Jquery find first element, if multiple elements of same id there
You can make unique id by apppending a uniqe incremental variable $run to it as
ID<input type="text" id="id"+<?php echo $run; ?> value="<?php echo $row['id']; ?>">; Moderator<input type="text" id="mod"+<?php echo $run; ?> value="<?php echo $row['name']; ?>">; Category<input type="text" id="ctgr"+<?php echo $run; ?> value="<?php echo $row['category']; ?>">; <button type="submit" id="update" rel="<?php echo $run; ?>" onclick="updatetodb( <?php echo $run; ?>);“ >UPDATE</button>
Then create a javascript function with name updatetodb with one argument, and identify row by that argument as
function updatetodb(var run) {
var id=$('#id' +run).value;

Getting the value of a select, with onchange and add row id number

Hi guys I'm having a hard time with this one please help me.
So I have an add row function where I can add dynamic rows to the form. My plan is to get the value of the Particulars with onchange event. So when you change the particular if will get the current value of the selected Particulars specifically on the selected row only.
The form looks like this:
So to test this out I'm trying to put the value in alert messages.
So this is the one I've done so far, I don't know if I'm making this correctly.
This is my html code.
<table class="table table-bordered" id="addSubPaymentTable">
<thead>
<tr>
<th >Particulars</th>
<th style="width:10%;"><button type="button" class="btn btn-success btn-sm" onclick="addPaymentRow()">Add Row</button></th>
</tr>
</thead>
<tbody>
<?php $arrayNumber = 0; for($x = 1; $x < 2; $x++) { ?>
<tr id="row<?php echo $x; ?>" class="<?php echo $arrayNumber; ?>">
<td class="form-group">
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance('<?php echo $x; ?>')" /> //AS YOU CAN SEE I HAVE AN ONCHANGE EVENT HERE
<option value="">Select Particulars</option>
<?php foreach ($particularsData as $particulars)
{
?>
<option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>
<?php
}
?>
</select>
<?php $arrayNumber++; } // /.foreach?>
</tbody>
So this is the function for the onchange event of Specific Balance
Javascript
// Get Specific Balance
function specificBalance(row = null)
{
var particular = $(this).val();
alert('Testing value is' +particular );
}
Please help me to get the value of the Particular, that is different in every row.
replace this line
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance('')" />
with
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance(this)" />
and in the jquery function use this
function specificBalance(row = null)
{
var particular = $(row).val();
alert('Testing value is' +particular );
}
Use this.value in onchange() function for select tag in HTML
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance(this.value)" /> //AS YOU CAN SEE I HAVE AN ONCHANGE EVENT HERE
<option value="">Select Particulars</option>
<?php foreach ($particularsData as $particulars)
{
?>
<option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>
<?php
}
?>
</select>

How to continue two type of loop at a time using Javascript and PHP

I need one help.i need to continue 2 types of loop at a time using Javascript and PHP.Let me to explain my code and scenario as well.
<script>
function addQuestionField(){
var get =$("#ques").val();
if(get==null || get==''){
alert('Please add no of questions');
}else{
var counter = 0;
if (counter > 0){
return;
}else{
counter++;
<?php
$status=array("status"=>'1');
$feeddata=$db->kf_answertype->find($ustatus);
?>
for(var i=1;i<get;i++){
$('#container').append(' <textarea class="form-control" name="questions'+ i +'" id="questions" placeholder="Questions" style="background:#FFFFFF;" rows="2"><?php if($_REQUEST['edit']) { echo $getcustomerobj->questions; } else { echo $_REQUEST['questions']; } ?></textarea>');
<?php
foreach($feeddata as $v){
?>
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>');
<?php
}
?>
}
}
}
}
</script>
Here i have one for loop but inside this one foreach loop is still there .By doing this in one iteration of for loop foreach loop continuing many times which i dont need.Here i need each time of for loop the foreach loop also iterate.suppose for 1st iteration of for loop the foreach loop also iterate once and so on.Please help me.
Use current and next functions to get consistent elements of an array. So, this fragment of your code
foreach($feeddata as $v){
?>
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>');
<?php
}
?>
should be changed to
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo current($feeddata)['_id']; ?>"> <?php echo current($feeddata)['answertype']; ?>');
<?php next($feeddata); ?>
or, if you use old version of php do it in such way:
<?php $v = current($feeddata); ?>
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>');
<?php next($feeddata); ?>

get value of select option in multiple row

My php code is as follows
<?php
if (isset($_POST["pitcode"]))
{
echo $_POST['pitcode'];
$pnetamt=0;
foreach ($_POST['txtwono1'] as $k => $v)
{
$pitcode=$_POST['pitcode'][$k];
$m_no=0;
$sql = mysql_query("select itrate from ITMAST where itcode='$pitcode'") or die(mysql_error());
$row = mysql_fetch_array($sql) or die(mysql_error());
$m_no = mysql_num_rows($sql);
if ($m_no!=0)
{
$sql = mysql_query("select itrate from ITMAST where itcode='$pitcode'") or die(mysql_error());
$row = mysql_fetch_row($sql);
$prate=$row[0];
}
}
}
?>
Javascript code is
<script type='text/javascript'>
function getitemcode()
{
var model=$('#itname').val();
document.frmwo.getElementById("txtitcode").value = model;
}
</script>
and HTML is
<html>
<td><input type="text" maxlength="3" size="2" name="txtwono1[]" class="rightJustified" value="<?php echo $pwono1; ?>"/></td>
<td><input type="text" maxlength="2" size="2" name="txtsno[]" class="rightJustified" value="<?php echo $psno; ?>"/></td>
<td>
<?php
$sql = "SELECT itcode,itname FROM ITMAST ";
$result = mysql_query($sql) or die(mysql_error());
echo "<select name='itname[]' id='itname' onchange='getitemcode()'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value = '{$row['itcode']}'";
if ($pitcode == $row['itcode'])
echo "selected = 'selected'";
echo ">{$row['itname']}</option>";
}
echo "</select>";
?>
</td>
<td><input type="text" maxlength="8" size="8" name="txtitcode" id='txtitcode' class="rightJustified" value="<?php echo $pitcode; ?>"/></td>
<td><input type="text" maxlength="8" size="8" name="txtqty[]" class="rightJustified" onchange="calcitemamt()" value="<?php echo $pqty; ?>"/></td>
<td><input type="text" maxlength="6" size="6" name="txtrate[]" class="rightJustified" disabled="disabled" value="<?php echo $prate; ?>"/></td>
<td><input type="text" maxlength="7" size="7" name="txtamt[]" disabled="disabled" class="rightJustified" value="<?php echo $pamt; ?>"/></td>
</html>
My Problem is when I selects the Item from Combo, its value is not passed to php code from where I am getting price of that item and that price should come to in rate column. This should work for all rows of html table - How
Change html code
echo "<select name='itname[]' id='itname' onchange='getitemcode()'>";
with
echo "<select name='itname[]' id='itname' onchange='getitemcode(this.value)'>";
also change javascript code
<script type='text/javascript'>
function getitemcode()
{
var model=$('#itname').val();
document.frmwo.getElementById("txtitcode").value = model;
}
</script>
with
<script type='text/javascript'>
function getitemcode(model)
{
document.frmwo.getElementById("txtitcode").value = model;
}
</script>

Can only post first result of while loop

I am using a while loop to display results from a query. The while loop is working fine. In hidden fields I would like to post the values of userID and accessID to the user details page. I am submitting the form using javascript to submit from a link. My problem is that regardless of the username I click I can only post the values for the first displayed record. What am I doing wrong?
The code:
<?php
while($row = $result->fetch_array()) { ?>
<form method="post" action="edit_user.php" id="userForm">
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
</form>
<?php } ?>
The javascript used for submitting the form:
function submitForm() {
var form = document.getElementById("userForm");
form.submit();
}
Thank you.
EDIT - I don't want to pass the values in the url.
you are generating multiple <form>s inside loop, move your <form> outside while loop, like:
<form method="post" action="edit_user.php" id="userForm">
<?php
while($row = $result->fetch_array()) { ?>
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID[]" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID[]" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
<?php } ?>
Submit
</form>
You're running into trouble because of this line
var form = document.getElementById("userForm");
In Javascript and HTML, an ID is supposed to be unique to a certain DOM element. In this case, you've got a whole load of form tags that have the same ID. You need to give each form a different ID, and then pass that ID to the submitForm function.
For example:
<?php
$id = 0;
while($row = $result->fetch_array()) { ?>
$id++;
<form method="post" action="edit_user.php" id="<?php echo "userForm".$id ?>">
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
</form>
<?php } ?>
and then
function submitForm(id) {
var form = document.getElementById(id);
form.submit();
}
edit: how do I php? :D

Categories