how to uppercase multiple id (checkbox in php using javascript) - javascript

How to uppercase each j1 "click" then the f1 text change to uppercase, j2 "click" then the f2 text change to uppercase, et seq..
<table>
<form>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction()}"></td>
<td><input type="text" id="f<? echo $i ?>" value="Test"></td>
</tr>
<script>
function myFunction() {
document.getElementById("f"+<? echo $i ?>).value = document.getElementById("f"+<? echo $i ?>).value.toUpperCase();
}
</script>
<? endforeach; ?>
</form>
</table>

Pass the value of $i to the function and use it to identify the element. Try with -
<td><input type="checkbox" id="j<? echo $i ?>" onclick="if(this.checked){myFunction('<? echo $i ?>')}"></td>
And define the function -
<script>
function myFunction(i) {
document.getElementById("f"+i).value = document.getElementById("f"+i).value.toUpperCase();
}
</script>
You dont have to put the function inside the loop.

for another option, you don't need to declare "id" attribute in input text element, you can use "name" attribute http://jsfiddle.net/j4080ytq/
<form>
<table>
<?
$i=0;
foreach ($im as $row):
$i++;
?>
<tr>
<td><? echo $i;?></td>
<td><? echo $row['Something'] ?></td>
<td><input type="checkbox" onclick="if(this.checked){f<? echo $i; ?>.value = f<? echo $i; ?>.value.toUpperCase();}"></td>
<td><input type="text" name="f<? echo $i ?>" value="Test"></td>
</tr>
<? endforeach; ?>
</table>
</form>

Related

How to add Value in jQuery

I want to add value to td in jquery. I have this code but not working.
Return all values for single td. I need that input [name="text-194] contain further values not all.
<table id="mytable" style="width: 100%;" border="1">
<tbody>
<?php
$counter = 1;
foreach ($result_list as $key => $row) { ?>
<tr>
<td class="jakas"><p><?php echo $counter; ?></p></td>
<td class="numer" value="<?php echo $row->numer; ?>"><p><?php echo $row->numer; ?></p></td>
<td class="nazwa" value="<?php echo $row->nazwa; ?>"><p><?php echo $row->nazwa; ?></p></td>
<td class="input" value="<?php htmlspecialchars($row->nazwa); ?>">
<?php echo do_shortcode('[contact-form-7 id="73" title="plik"]'); ?></td>
<?php echo '<input type="text" name="idtest" value="' . htmlspecialchars($row->nazwa) . '">'; ?>
</tr>
<?php $counter++; ?>
<script>
(function ($) {
$(document).ready(function () {
var value = '<?=htmlspecialchars($row->nazwa)?>';
console.log(value);
$('input[name="text-194"]').val($('input[name="text-194"').val() + value);
});
})(jQuery);
</script>
<?php
}
?>

Assign id or class to fetched data to work with in JavaScript

i have fetched data from database with PHP in while loop.i want to assign id or class to this data to work with it in javaScript individually with every id or class which are in table td.
For example
$i = 0 ;
if($result->rowCount() > 0)
{
while($row_pro = $prod->fetch())
{
$type = $row->type;
$brand = $row->brand;
$qty = $row->qty;
$total = $row->total;
$i++ ;
?>
<tr>
<td><input type="text" value="<?php echo $type; ?>" ></td>
<td><input type="text" value="<?php echo $brand; ?>" ></td>
<td><input type="text" value="<?php echo $qty; ?>" ></td>
<td><input type="text" value="<?php echo $total; ?>" ></td>
</tr>
<?php }} ?>
One solution would be to simply echo out your loop's index ($i) as a class:
<tr>
<td><input type="text" value="<?php echo $type; ?>" class="<?php echo $i; ?>"></td>
<td><input type="text" value="<?php echo $brand; ?>" class="<?php echo $i; ?>"></td>
<td><input type="text" value="<?php echo $qty; ?>" class="<?php echo $i; ?>"></td>
<td><input type="text" value="<?php echo $total; ?>" class="<?php echo $i; ?>"></td>
</tr>
Keep in mind that you can't do this for IDs, as IDs must be unique. Also, you don't actually need to do this in the first place, as you can directly target elements with JavaScript that have neither IDs nor classes. This is most commonly with .getElementsByTagName(), which returns a NodeList collection of elements. Here's an example of that:
let three = document.getElementsByTagName('td')[2];
console.log(three.innerHTML);
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>

PHP submit button while loop

Hi guys im having a bit of a problem on how to get the value of the table row everytime i click an add button. what im trying to achieve is that to pass the value of the row to another php file for sql query. at the moment i have a table using a while loop and displaying a list of names from the database with a "ADD" button per row. When ever i click the add button regardless of which row i always get the value of the last row. can someone please point of on which part of the code i made a mistake?
here is my code
<form action="addFriend.php" method="post"> <table class="table table-bordered">
<?php
$i = 1;
while($row = mysqli_fetch_array($records))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="addedUser" value="<?php echo $row["username"]; ?>" readonly></td>
<td>
<button type="submit" name="row_<?php echo $i; ?>" id="row_user" value="<?php echo $row["username"]; ?>" class="btn btn-info">ADD</button>
<?php $i++; ?>
</td>
</tr>
<?php
}
?>
</table>
</form>
Try Below code. As you want only $row["username"] and it is already stored in button value. Add form in while loop and give same name for button so that you can get username in addFriend.php
<table class="table table-bordered">
<?php
$i = 1;
while($row = mysqli_fetch_array($records))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="addedUser" value="<?php echo $row["username"]; ?>" readonly></td>
<td>
<form action="addFriend.php" method="post">
<button type="submit" name="row" id="row_user" value="<?php echo $row["username"]; ?>" class="btn btn-info">ADD</button>
</form>
<?php $i++; ?>
</td>
</tr>
<?php
}
?>
</table>
Now in addFriend.php you can get value by $_POST['row'];
Try Below script
<form action="addFriend.php" method="post"> <table class="table table-bordered">
<?php
$i = 1;
while($row = mysqli_fetch_array($records))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="**addedUser[]**" value="<?php echo $row["username"]; ?>" readonly></td>
<td>
<button type="submit" name="row_<?php echo $i; ?>" id="row_user" value="<?php echo $row["username"]; ?>" class="btn btn-info">ADD</button>
<?php $i++; ?>
</td>
</tr>
<?php
}
?>
</table>
</form>
In addFriend.php
print form data using print_r($_REQUEST);

How to put 2 parameters on onclick using php

im having problem getting the value of a two input textbox using an onclick function.
<?php foreach ($showItems as $items) { ?>
<tr>
<td><?php if(!empty($items['ItemCode'])) echo $items['ItemCode'], ' '; ?><input type="hidden" name="pid" id="pid" value="<?php echo $items['ItemCode']; ?>" /></td>
<td><img src="" /></td>
<td><?php if(!empty($items['Name'])) echo $items['Name'], ' '; ?></td>
<td><?php if(!empty($items['Price'])) echo $items['Price'], ' '; ?></td>
<td><?php if(!empty($items['Description'])) echo $items['Description'], ' '; ?></td>
<td><input type="text" name="qty" id="qty" maxlength="3" size="2" onblur='if(this.value==""){this.value=1;}' /></td>
<td ><input type="submit" value="Add to Registry" onClick="addRegistry(<?php echo $items['ItemCode']; ?>, document.getElementById('qty').value;)"></td>
</tr>
<?php } ?>
javascript script:
function addRegistry(pid, qty){
alert(qty);
document.form.pid.value=pid;
document.form.command.value='add';
document.form.submit();
}
php script:
if($_REQUEST['command']=='add' && $_REQUEST['pid']>0){
$qty = $_POST['qty'];
//echo $qty;
$customerid = $_SESSION['id'];
$pid=$_REQUEST['pid'];
// echo $pid;
$admin->addToRegistry($pid, $customerid, $qty);
header('location:registryCart.php');
}
how do i get the two values so that i can insert it to my database, i was able to get the itemcode, but i cant get the value of the quantity.
Use $counter variable which will increment and differentiate each input id
<?php
$counter=1;
foreach ($showItems as $items) { ?>
<tr>
<td><?php if(!empty($items['ItemCode'])) echo $items['ItemCode'], ' '; ?><input type="hidden" name="pid" id="pid" value="<?php echo $items['ItemCode']; ?>" /></td>
<td><img src="" /></td>
<td><?php if(!empty($items['Name'])) echo $items['Name'], ' '; ?></td>
<td><?php if(!empty($items['Price'])) echo $items['Price'], ' '; ?></td>
<td><?php if(!empty($items['Description'])) echo $items['Description'], ' '; ?></td>
<td><input type="text" name="qty" id="qty-<?php echo $counter; ?>" maxlength="3" size="2" onblur='if(this.value==""){this.value=1;}' /></td>
<td ><input type="submit" value="Add to Registry" onClick="addRegistry(<?php echo $items['ItemCode']; ?>, document.getElementById('qty-<?php echo $counter; ?>').value;)"></td>
</tr>
<?php
$counter++;
} ?>

Javascript PHP/MySQL - Insert row value from database into textfield

Possibly very easy to do but I cannot work it out!
I'm trying to make a button which inserts row value pulled out from the database into a textfield.
<?php while ($row = mysql_fetch_array($query)) { ?> <tr>
<td><?php echo $row['mobile_number']; ?></td>
<td><input type="button" value="select" onclick="clickMe()" /></td>
</tr>
<?php } ?>
<input type="text" id="textfield" />
so when the button "select" is clicked, the textfield would get populated with the mobile number.
Many thanks for your help in advance.
Try this:
<script>
function clickMe(number) {
document.getElementById("textfield").value = number;
// or jQuery
$("#textfield").val(number);
}
</script>
<?php while ($row = mysql_fetch_array($query)) { ?> <tr>
<td><?php echo $row['mobile_number']; ?></td>
<td><input type="button" value="select" onclick="clickMe(<?php echo $row['mobile_number']; ?>)" /></td>
</tr>
<?php } ?>
<input type="text" id="textfield" />

Categories