how to store value from html textbox to javascript variable - php

I am creating a form which fetches the value from the database and stores the value dynamically into a table. I have a button named add which adds a row dynamically when a user press it and I have a hidden counter which counts the rows added.
The problem is, when I fetch the record, the counter shows the number of rows present and when I add a new row it goes back to 1.
If there are two rows in the table, the counter will show 2 but when I try to add a new row the counter shows 1.
Is there any way to set the value from the counter text into the JavaScript variable and increment it?
Here is my JavaScript code:
<script language="javascript" type="text/javascript">
var jj= 1;
alert(jj);
function addRow()
{
//alert(jj)
var tbl = document.getElementById('zimtable');
var lastRow = tbl.rows.length;
var iteration = lastRow - 1;
var row = tbl.insertRow(lastRow);
var firstCell = row.insertCell(0);
var el = document.createElement('input');
el.type = 'text';
el.name = 'zimname_' + jj;
el.id = 'zimname_' + jj;
el.size = 40;
el.maxlength = 40;
firstCell.appendChild(el);
var secondCell = row.insertCell(1);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'zimmob_' + jj;
el2.id = 'zimmob_' + jj;
el2.size = 13;
el2.maxlength = 13;
secondCell.appendChild(el2);
// alert(i);
//$('#hh').val(jj);
jj++;
makhtab.hh.value=jj;
alert(jj);
}
</script>
Here is my php code to show the table:
<?php
$zim = mysql_query("SELECT * FROM `makhzim` WHERE makhcode='$newsid' ORDER BY srno")or die(mysl_error());
$ctrzim = 0;
while ($zrow = mysql_fetch_array($zim)){
$ctrzim++;
print '<script type="text/javascript">';
print "alert('$i')";
print '</script>';
echo "<tr>";
echo "<td><input name='zimname_$ctrzim' type='text' size='40' maxlength='20' value=$zrow[name] /></td>";
echo "<td><input name='zimmob_$ctrzim' type='text' size='13' maxlength='20' value=$zrow[mobile] /></td>";
echo "</tr>";
}
echo "</table>";
echo "<input type=\"button\" value=\"Add\" onclick=\"addRow();\" /><input id=\"hh\" name=\"hh\" type=\"text\" value= '$ctrzim'/>";
?>

In your php code put the value of the counter to a hidden element
<input type = "hidden" id = "hiddenElementId" value = "your php counter value"/>
In your javascript
<script>
var count;
window.onload=function(){
count = = document.getElementById("hiddenElementId").value;
};
function addRow() {
alert(count);
..
..
count++;
}
<script>

Related

Unable to find the solution help me out ..regarding js and php

php code :
while($row = mysql_fetch_assoc($arr))
{
$id = $row['id'];
$user_from = $row['user_from'];
$user_to = $row['user_to'];
$date = $row['date'];
$msg_body = $row['msg_body'];
$opened = $row['opened'];
if(strlen($msg_body)>150)
{
$msg_body = substr($msg_body,0,150)."....";
}
echo "<div id='toggletext$id' style='background-color:#ADD8E6;padding:6px;' onclick='toggle()'>";
echo "<form method='post'>";
echo "<input type='hidden' id='id1' value=$id>";
echo "<a href='$username'>$username</a>&nbsp";
echo "&nbsp: $msg_body ";
echo "</form>";
echo "</div> <hr/>";
}
** it will echo the value present in $arr in forms of div so each $arr row will get displayed with unique div id **
js code :
function toggle()
{
var hr = new XMLHttpRequest();
var id = document.getElementById("id1").value;
var tx = "toggletext"+id;
var fn = document.getElementById(tx);
var url = "in.php";
fn.style.backgroundColor = "#fff";
var v = '<?php echo #$username;?>';
var vars = "post="+v+"&id="+id;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function()
{
if(hr.readyState == 4 && hr.status == 200)
{
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("status").innerHTML = "processing...";
}
// The problem is ...when there are 2 div(if $arr has 2 rows)..if i click the second div first div will get selected ..
Change your id in your input type hidden
echo "<input type='hidden' id='id".$id."' value=$id>";
and get like this in your javascript...
var incval = '<?php echo $id ?>';
var id = document.getElementById("id"+incval).value;
I am giving php echo because you already call username....
Finally, try to use My_Sqli function instead my_sql

How to generate number with textbox by javascript?

I have one textbox called main_textbox when user enter any number i call javascript
function on the onblur event of textbox and by this code
function generate_bale(){
var bale=document.getElementById("number_of_bale").value;
var boxes = "<p>";
for (var i=0;i<`bale`;i++){
boxes+="<input type='text' name='bale_numbers[]'><br><br>";
}
document.getElementById("bale_box").innerHTML = boxes;
}
I genereate number of textbox which enter in main_textbox
now suppose if i want to generate number like
1.textbox
2.textbox
3.textbox
so how to print this number in front textbox so i can identify from all generated textbox which number of textbox is
THANX in advance
for (var i=0;i<bale;i++){
boxes+= i + ". <input type='text' name='bale_numbers[]'><br><br>";
}
document.getElementById("bale_box").innerHTML = boxes;
}
Did you ment to something like that?
function generate_bale(){
var bale=document.getElementById("number_of_bale").value;
var boxes = "<p>";
for (var i=0;i<bale;i++){
boxes+="<label for='bale_"+i+"'>"+i+"</label><input id='bale_"+i+"' type='text' name='bale_numbers[]'/><br/><br/>";
}
boxes+="</p>";
document.getElementById("bale_box").innerHTML = boxes;
}
You may change the code that used to generate textbox as following:
boxes+="<label>"+ i + "<label><input type='text' name='bale_numbers[]'><br><br>";
Assuming that the following is the answer you expected,
Try this
function generate_bale(bale){
var bale=document.getElementById("number_of_bale").value;
var boxes = "<p>";
for (var i=0;i<bale;i++){
textboxNo = i+1;
boxes+=textboxNo+".textbox<input type='text' name='balue_"+textboxNo+"'><br><br>";
}
document.getElementById("bale_box").innerHTML = boxes;
}
Check the fiddle here
function generate_bale(){
var bale=document.getElementById("number_of_bale").value;
var boxes = "<p>";
for (var i=0;i<`bale`;i++){
boxes+=+i+1+".textbox <input type='text' name='bale_numbers[]'><br><br>";
}
document.getElementById("bale_box").innerHTML = boxes;
}

Increment href value by one (Javascript)

I have got a ordered list
<li id="prev">
<a href="#fragment-1>Next</a>
</li>
I want to increment the href value to
<a href="#fragment-1">...
<a href="#fragment-2">...
<a href="#fragment-3">...
<a href="#fragment-4">...
When the next is clicked it should stop from 4 and return to 1 allso is it possible with javascript at all
Thank you
Give your link an id first to make it easier to select.
Then,
document.getElementById('the_id_here').addEventListener('click', function(e) {
var n = e.target.href.split('-')[1] * 1 + 1;
if (n > 4)
n = 1;
e.target.href = e.target.href.split('-')[0] + '-' + n;
}, false);
Example: http://jsbin.com/ajegaf/4#fragment-1
Not exactly what you want but should be close enough to aim you in the right direction, check http://jsfiddle.net/pj28v/
<ul id="list"></ul>
Next​
$(function() {
var $list = $('#list'),
count = 4,
i = 0,
cur = 0;
for (; i < count; i++) {
$list.append('<li>frag' + i + '</li>');
}
$('a', $list).eq(0).css('color','red');
$('#next').click(function(ev) {
ev.preventDefault();
cur++;
if (cur >= count) cur = 0;
$('a', $list).css('color','blue').eq(cur).css('color','red');
});
});​
A jQuery solution:
$(document).ready(function(){
$('#prev a').click(function(){
var href = $(this).attr('href');
var num = parseInt(href.substr(href.indexOf('-') + 1));
if(num == 4)
{
num = 1;
}
else
{
num++;
}
$(this).attr('href', '#fragment-' + num)
});
});​
http://jsfiddle.net/UYDdB/
A solution similar to Delan's one but with jQuery and without the usage of a test. http://jsfiddle.net/GZ26j/
$('#next').on('click', function(e) {
e.preventDefault();
href = $(this).attr('href');
var n = href.split('-')[1] * 1 + 1;
n = n%5;
e.target.href = href.split('-')[0] + '-' + n;
});​
Edit:
This solution makes the counter start at 0 and not 1
use a hidden field to hold the value, and an onclick function to increase it and submit the form. SEE: How can I increase a number by activating a button to be able to access a function each time the button is increased?
<?
if(!isset($_GET['count'])) {
$count = 0;
} else {
$count = $_GET['count'];
}
?>
<script type='text/javascript'>
function submitForm(x) {
if(x == 'prev') {
document.getElementById('count').value--;
} else {
document.getElementById('count').value++;
}
document.forms["form"].submit();
}
</script>
<form action='hidfield.php' method='get' name='form'>
<input type='hidden' name='count' id='count' value='<?php echo $count; ?>'>
</form>
<input type='submit' name='prev' value='prev' onclick="submitForm('prev')">
<input type='submit' name='next' value='next' onclick="submitForm('next')">

how to set checkbox values and how to get checkbox values in javascript

i want to set value to checkbox,how do it? this is my code: from this code only i could get upto link,image,name value,why? i want to get link,image,name,description,categ value.how do it?
$results=$watch.",".$thumbnail.",".$name.",".$description.",".$val;
<input type="checkbox" name="checkbox[]" id="checkbox[]" class="addbtn" value=<?php echo $results;?>
this is my javascript function to get the checkbox value.
function chkbox() {
$('[name^=checkbox]:checked').each(function() {
var ckballvalue = ($(this).val());
var latlngStrs = ckballvalue.split(",", 5);
var link = latlngStrs[0];
var thumbnail = latlngStrs[1];
var name = latlngStrs[2];
var description = latlngStrs[3];
var categ = latlngStrs[4];
alert(link + thumbnail + name + description + categ);
}
I've tried to revise your code.
When doing it like this it works:
$("input.addbtn").click(function() {
var val = $(this).val();
var arr = val.split(',', 5);
var link = arr[0];
var thumb = arr[1];
var name = arr[2];
var desc = arr[3];
var cat = arr[4];
alert(link + "|" + thumb + "|" + name + "|" + desc + "|" + cat);
});
I hope this helps

Setting up the default value in a new textbox

I am trying to make a form in which there will be a button, when clicking on it a Javascript function will be called and a new row with a textbox will be added. In simple words, when clicking on the button, new text will be added. Here is the function:
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtRow' + iteration;
el.id = 'txtRow' + iteration;
el.size = 40;
cellRight.appendChild(el);
// select cell
var cellRightSel = row.insertCell(2);
var sel = document.createElement('select');
sel.name = 'selRow' + iteration;
sel.options[0] = new Option('text zero', 'value0');
sel.options[1] = new Option('text one', 'value1');
cellRightSel.appendChild(sel);
}
Now please guide me: how can I set the default value of the text box and drop down box? I mean in this code where should I put <?php echo $data['pno'];?>
I have tried putting in the HTML form; for the first row it works but for the second row it doesn't work. Thanks.
i have solved in this way
<input type="button" value="Add" onClick="addRowToTable(<?php echo $data['pno'];?>);" />
and on the javascript side
function addRowToTable(a)
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.value=a; // here i have achived the value
el.name = 'txtRow' + iteration;
el.id = 'txtRow' + iteration;
el.size = 40;
}
might be its not a good solution but for my case it works
How about you save the default value in a separate JS variable in your html like:
var defaultValue = "<?php echo $data['pno'];?>";
Or try this:
var defaultValue = "<?= $data['pno'] ?>";
Then when you are adding new input you can use it like this:
el.value = defaultValue;

Categories