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

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" />

Related

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);

Send php variable onclick through javascript code

I have a form table, in which when I click on Show button, then a popup window open. Till now everything is good.
But after click on Show button, I also want to send php variable in popup window and want to show values related to that variable. How it can be possible?
My table is below
<table align="center">
<?php
include("connection.php");
$query1=mysql_query("select * from career") or die(mysql_error());
while($row1=mysql_fetch_array($query1))
{
extract($row1);
?>
<tr>
<td>Designation</td>
<td><input type="text" name="des" value="<?php echo $designation; ?>"></td>
</td>
</tr>
<tr>
<td>Number of position</td>
<td><input type="text" name="des" value="<?php echo $no_of_position; ?>"></td>
</td>
</tr>
<tr>
<td>Experience</td>
<td><input type="text" name="des" value="<?php echo $experience; ?>"></td>
</td>
</tr>
<tr>
<td>Qualification</td>
<td><input type="text" name="des" value="<?php echo $qualification; ?>"></td>
</td>
<tr>
<td>Job profile</td>
<td><input type="text" name="des" value="<?php echo $job_profile; ?>"></td>
</tr>
<tr>
<td><?php echo"$id"; ?></td>
<td><button onclick="document.getElementById('id01').style.display='block'" style="width:auto;" id="id1">Show</button>
</td>
</tr>
<tr>
<td></td>
<td> </td>
</tr>
<?php } ?>
</table>
In above code when I click on Show Button, then popup window open, code is below:
<div id="id01" class="modal1">
<form class="modal1-content animate" action="xyz.php">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close modal1">×</span>
</div>
<div class="container">
<div>Content</div>
</div>
</form>
</div>
<script>
// Get the modal1
var modal1 = document.getElementById('id01');
// When the user clicks anywhere outside of the modal1, close it
window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
}
</script>
I want to send id variable, by which I can fetch values from database in popup window.
Thank you.
If I understand correctly you need an ajax call to a php page like this:
modalService.php
if(!isset($_GET['id']){
echo "no request";
exit;
}
$id = $_GET['id'];
//Do your query and echo the modal content
table
<td><?php echo $id; ?></td>
<td>
<button onclick="myFunction('<?php echo $id; ?>')" >Show</button>
</td>
JS
function myFunction(id){
var modal = document.getElementById("id01");
modal!==null && (modal.style.display="block");
var modalServiceUrl = "./modalService.php?id="+id;
//xhr to get modalService
}
EDIT: USING JQUERY
<button data-queryid="<?php echo $id; ?>" class="showModal">Show</button>
jQuery(document).ready(function($){
var modal = document.getElementById("id01");
$("button.showModal").on("click", function(){
modal.fadeIn();
var id = $(this).attr("data-queryid");
var modalServiceUrl = "./modalService.php?id="+id;
modal.load(modalServiceUrl);
});
});

how to uppercase multiple id (checkbox in php using 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>

How i can get value of specific cell if row is checked?

I have an HTML table. I need to get specific cell value if column is checked! For each row I have a checkBox.
<table id="tabellaOrdinaFarmaci" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Codice Farmaco</th>
<th>Nome Farmaco</th>
<th>Quantità </th>
<th>Quantità di alert</th>
<th>Stato</th>
<th>Quantità da ordinare</th>
<th></th>
</tr>
</thead>
</thead>
<tbody>
<?php
foreach ($query as $row):
$quantitaDaOrdinare = $row->alert - $row->quantita;
if ($quantitaDaOrdinare > 0) {
?>
<tr id="<?php echo $row->aic; ?>" >
<td ><?php echo $row->aic; ?></td>
<td name="nomeFarmac"o><?php echo $row->denominazione ?></td>
<td><?php echo $row->quantita; ?></td>
<td><?php echo $row->alert; ?></td>
<td><?php echo $row->stato; ?></td>
<td>
<input type="text" class="form-control auto" name="codiceFarmaco" value="<?php echo $quantitaDaOrdinare ?>"/>
</td>
<td> <label>
<input type="checkbox" name="checkBox" value="<?php echo$row->aic; ?> ">
</label></td>
</tr>
<?php
} else {
}
endforeach;
?>
</tbody>
I need (using javascript or jquery) to get, if a button is clicked, every value of cell that have name="nomeFarmaco" for every row that is checked! I try to call this function whe
function getValue(){
var nome = $('input[name="checkBox"]:checked').map(function() {
return $(this).parent().parent().parent().find('name="nomeFarmaco').val();
}).get();
return nome;}
but nome is an empty array! any ideas? thanks in advance!
You can put a custom attribute for the check box and set the value in this attribute.
Like this :
<input type="checkbox" name="checkBox" value="<?php echo$row->aic; ?> " customattr="<?php echo $row->denominazione ?>">
And you javascript function will be :
function getValue(){
var nome = $('input[name="checkBox"]:checked').attr('customattr');
return nome;
}
I think this is typo?
<td name="nomeFarmac"o><?php echo $row->denominazione ?></td>
Change it to:
<td name="nomeFarmaco"><?php echo $row->denominazione ?></td>
And you are targetting a <td> and .val() won't work. Should be .text()
var name = [];
$('input[name="checkBox"]').click(function(){
if($(this).is(':checked')) {
name.push($(this).parent().parent().siblings('td[name="nomeFarmaco"]').text());
} else {
var index = name.indexOf(name);
name.splice(index, 1);
}
console.log(name);
});

Get closest selected option

I am having trouble finding the closest selected option (text) near a submit button in multiple forms (with multiple submit buttons).
But I don't get anything how to fix this?
Code :
$(document).ready(function() {
$("form").submit(function() {
var x = $(this).closest('form').find('input[type=submit]');
var y = $(x).closest('select option:selected').text();
alert(y);
});
});
<form action="<?php echo site_url('manage/deleteVendor'); ?>" method="POST">
<table cellspacing='10'>
<tr>
<td>Delete Vendor</td>
</tr>
<tr>
<td>Vendor</td>
<td><select name="vendor" class="vendor">
<?php foreach ($vendors as $vendor) { ?>
<option value="<?php echo $vendor->ID; ?>" ><?php echo $vendor->NAME; ?></option>
<?php } ?>
</select></td>
<td><input type="submit" name ="submit" value="Delete Vendor"/></td>
</tr>
</table>
</form>
<form action="<?php echo site_url('manage/deleteVendor'); ?>" method="POST">
<table cellspacing='10'>
<tr>
<td>Delete Vendor 2</td>
</tr>
<tr>
<td>Vendor</td>
<td><select name="vendor" class="vendor">
<?php foreach ($vendors as $vendor) { ?>
<option value="<?php echo $vendor->ID; ?>" ><?php echo $vendor->NAME; ?></option>
<?php } ?>
</select>
</td>
<td><input type="submit" name ="submit" value="Delete Vendor"/></td>
</tr>
</table>
</form>
Find the closest select and find the selected option in it followed by .html() to get the text.
var y=$('selector').closest('select').find(':selected').html(); //find the text
var x=$('selector').closest('select').find(':selected').val(); //find the value
alert(y);
alert(x);
Edit: After viewing you HTML
var SelectedOptionText=$('.vendor').find(':selected').html();
alert(SelectedOptionText);
Edit: Based on your commment
$(document).ready(function() {
$("form").submit(function() {
var x =$(this).find('.vendor :selected').html();
alert(x);
});
});

Categories