Multi-file upload using google app script - php

I am trying to use use a form and image uploader using google app scripts. I want to have the option to dynamically increase the number of images uploaded using a button, and then upload those to my drive. I am able to add more file inputs, but can't seem to get the files to upload if I have more than one image. I commented out the for loop, and am using only two images as an example. The if/else statement functions, but it does not upload the image. I am relatively new to programming and very new to google app scripts. Any help would be much appreciated. Thanks!
form.html:
<form id="myForm">
<p>Name</p>
<input type="text" name="PosterName">
<p>Email</p>
<input type="text" name="email">
<p>What are you selling/renting?</p>
<table>
<tr>
<td><input type="radio" name="sell" value="apt" id="apt"></td>
<td><label for="apt">Apartment</label></td>
</tr>
<tr>
<td><input type="radio" name="sell" value="furn" id="furn"></td>
<td><label for="furn">Furniture</label></td>
</tr>
<tr>
<td><input type="radio" name="sell" value="books" id="books"></td>
<td><label for="books">Books</label></td>
</tr>
<tr>
<td><input type="radio" name="sell" value="other" id="other"></td>
<td><label for="other">Other</label></td>
</tr>
</table>
<p>Give a description</p>
<textarea name="desc" rows="10" cols="50"></textarea>
<table id="submittable">
<tr>
<td><input type="file" name="myFile1"></td>
<td><button onclick="addmore();" id="D">Add More</button></td>
</tr>
</table>
<input type="submit" value="Upload File"
onclick="this.value='Uploading...';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
<input type="text" id="test" name="appt">;
</form>
<div id="output"></div>
<script>
function addmore() {
var tname = document.getElementById('submittable');
var row = tname.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var numrows = tname.rows.length;
cell1.id = "A";
cell2.id = "B";
var place1 = document.getElementById("A");
var place2 = document.getElementById("B");
var fle = document.createElement("input");
var add = document.createElement("button");
add.setAttribute("onclick", "addmore()");
var x = document.getElementById('test');
x.setAttribute("value", numrows);
fle.setAttribute("type", "file");
fle.setAttribute("name", "myFile" + numrows);
var y = fle.getAttribute("name");
add.innerHTML = y;
place1.appendChild(fle);
place2.appendChild(add);
cell1.id = "C";
cell2.id = "D";
var place3 = document.getElementById("D");
place3.parentNode.removeChild(place3);
}
</script>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<style>
input { display:block; margin: 20px; }
</style>
And
code.gs:
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(form) {
try {
var dropbox = "shukimages";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var spreadsheet = DriveApp.getFilesByName(dropbox);
var ss = SpreadsheetApp.openById("1O4GM0DT5kqu5MNLcYFZe_vrI2KK1lmO9s5XviHWsMEg");
var sheet = ss.getSheets()[0];
sheet.appendRow([form.PosterName, form.email, form.sell, form.desc]);
//return "Files uploaded successfully";
var nums = form.appt;
if (nums < 1) {
var blob = form.myFile1;
var file = folder.createFile(blob);
return "File uploaded successfully";
} else {
return "Files uploaded successfully";
//for (i = 1; i <= nums; i++) {
var blob = form.myFile2;
var file = folder.createFile(blob);
//}
}
} catch (error) {
return error.toString();
}
}
EDIT:
A little more clarification; if I only attempt to upload one image, it works and the image is uploaded, but it will not upload more than one image.
Where I got createFile:
https://developers.google.com/apps-script/reference/drive/drive-app

Related

Autofill Input Field Based On Another Input Field OnKeyUp (Dynamic Generated)

I hope today I'll find good solution for my case below.
Case
Actually, I have dynamic input fields and let say there field A, B and C. So, the purpose is when field A receives OnKeyUp Action then field B and C will autofill based query result from field A value.
index.php
<html>
<body>
<script type="text/javascript">
var ct = 1;
// Function Tambah Form
function new_link()
{
ct++;
var div1 = document.createElement('div');
div1.id = ct;
// Link to Delete Form
var delLink = '<div style="text-align:left"><a id="del" href="javascript:delIt('+ ct +')">Delete</a></div>';
div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
document.getElementById('newlink').appendChild(div1);
}
// Function Delete Form
function delIt(eleId)
{
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newlink');
parentEle.removeChild(ele);
}
function ajaxFunction(str,divId)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateChanged()
{
if(httpxml.readyState==4)
{
document.getElementById(divId).innerHTML=httpxml.responseText;
}
}
var url="cek.php";
url=url+"?txt="+str;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
}
</script>
</head>
<body>
<form name="myForm">
<a id="new" href="javascript:new_link()">ADD NEW PAYMENT</a>
<div id="newlink">
<table width="50%" border="1">
<tr>
<td><input type="text"
onkeyup="ajaxFunction(this.value,'displayDiv');" name="username" /></td>
<td>
<div id="displayDiv">
<table width="100%" border="0">
<tr>
<td><input type="text" name="curr"></td><td><input type="text" name="amount"></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<div id="newlinktpl" style="display:none" >
<div>
<table width="50%" border="1">
<tr>
<td><input type="text"
onkeyup="ajaxFunction(this.value,'displayDiv<?php echo ++$np;?>');" name="username" /></td>
<td>
<div id="displayDiv<?php echo $np;?>">
<table width="100%" border="0">
<tr>
<td><input type="text" name="curr"></td><td><input type="text" name="amount"></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</div>
cek.php
<?php
mssql_connect('localhost','sa','');
mssql_select_db('NCD');
$in=$_GET['txt'];
$msg="";
$t=mssql_query("SELECT * FROM DPaym1 WHERE (JENIS = 1) AND (DPSPAID = 0) AND (CNDPSNO = '$in')");
while($nt=mssql_fetch_array($t)){
//$msg = "$nt[CURRID]#$nt[AMOUNT]";
$curr = "$nt[CURRID]";
$amount = "$nt[AMOUNT]";
}
//echo $msg;
//echo "$curr $amount";
?>
<table width="100%" border="0">
<tr>
<td><input type="text" name="curr" value="<?php echo $curr?>"></td>
<td><input type="text" name="amount" value="<?php echo $amount?>"></td>
</tr>
</table>
Above code already success for row 1. but when I input value in row 2 (dynamic add new row) the result change field B and C in row 1 not in row 2.

Dynamically Add Rows In HTML Table Using JavaScript and getting textbox value of each text box by submitting a button

I have a table where I can dynamically add rows. I want to get the data in each row to a php array when I submit the save button . Can please somebody help me on this. I'm new to java-script and know very little about it. Thank you!
<HTML>
<HEAD>
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>
<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name="chkbox[]";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
element2.name = "txtbox[]";
cell3.appendChild(element2);
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT type="button" value="Add Row" onClick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onClick="deleteRow('dataTable')" />
<form action="" method="post">
<?php $i= 1; ?>
<TABLE id="dataTable" width="350px" border="1">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD> 1 </TD>
<TD> <INPUT type="text" name="harsh"/> </TD>
</TR>
</TABLE>
<input name="saveNewSales" type="submit" value="Save" id="button2" style="text-align:center"/>
</form>
<?php
foreach($_POST as $name => $content) { // Most people refer to $key => $value
echo "The HTML name: $name <br>";
echo "The content of it: $content <br>";
}
?>
</BODY>
</HTML>
Your code is right, just little change over there
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD> 1 </TD>
<TD> <INPUT type="text" name="<?php echo $i; ?>"/> </TD>
</TR>
replace with this code
<TR>
<TD><INPUT type="checkbox" name="chkbox[]"/></TD>
<TD> 1 </TD>
<TD> <INPUT type="text" name="txtbox[]"/> </TD>
</TR>
and your work finish
var rows=getElementsByTagName("tr");
var noOfRows=rows.length;
for(int i=0;i<noOfRows;i++){
var html=rows[i].innerhtml; /*a single row's inner html say (<td>apple</td><td>25</td>)*/
var td=html.match(/(?!<td>)([\s*\w*]*)[^<\/td>]/g);
/**
td[0] is 'apple', td[1] is '25'
**/
}

Using PHP and Javascript in onblur function

so I have this form:
<form enctype="multipart/form-data" action="cgi-bin/upload.cgi?upload_id=" method="post" onSubmit="return StartUpload(this);" target="xupload">
<table>
<tr>
<td class="first"><span class="right">Upload File: </span></td>
<td class="second"><input name="file_1" type="file" onChange="checkExt(this.value)" accept=".mp4, .avi, .wmv, .mov, .camrec, .flv, .zip, .rar"></td>
<td class="third"><span class="left">(required)</span></td>
</tr>
<tr></tr>
<tr><center><h2>Video Information</h2></center></tr>
<tr></tr>
<tr>
<td class="first"><span class="right">Your Name: </span></td>
<td class="second"><input type="text" name="name"></td>
<td class="third"><span class="left">(preferred)</span></td>
</tr>
<tr>
<td class="first"><span class="right">Series ID: </span></td>
<td class="second"><input id="series_id" type="text" name="series_id" placeholder="You should have been given this by your admin"></td>
<td class="third"><span class="left">(preferred)</span></td>
</tr>
<tr>
<td class="first"></td>
<td class="second"><span id="series_id_check">Enter You're series ID Above and this will change</span></td>
<td class="third"></td>
</tr>
<tr>
<td class="first"><span class="right">Title: </span></td>
<td class="second"><input type="text" name="title"></td>
<td class="third"><span class="left">(preferred)</span></td>
</tr>
<tr>
<td class="first"></td>
<td class="second center"><small>Please use "<span style="color:#9c0000"><br></span>" to make a new line</small></td>
<td class="third"></td>
</tr>
<tr>
<td class="first"><span class="right">Description: </span></td>
<td class="second"><textarea name="desc"></textarea></td>
<td class="third"><span class="left">(preferred)</span></td>
</tr>
<tr>
<td class="first"><span class="right">Guests: </span></td>
<td class="second"><input type="text" name="guests"></td>
<td class="third"><span class="left">(optional)</span></td>
</tr>
<tr>
<td class="first"><span class="right">Additional Tags: </span></td>
<td class="second"><input type="text" name="tags"></td>
<td class="third"><span class="left">(comma separated - optional)</span></td>
</tr>
</table>
<input type="submit" name="submit" value="Upload File">
</form>
Which I use to upload videos to my web hosting server. I want to be able to change the text in the series_id_check span with some javascript that uses PHP arrays... Here's the PHP that I use for populating the arrays from mysql (I KNOW THAT ITS DEPRECIATED BUT FOR MY WEB HOSTING PHP 5.3 IS INSTALLED AND ITS ONLY DEPRECIATED FROM 5.4):
$sql1 = mysql_fetch_assoc(mysql_query("SELECT COUNT cnt FROM series"));
$count = $sql1['cnt'];
$gamearray = array($count - 1);
$namearray = array($count - 1);
$idarray = array($count - 1);
for ($i = 0; $i < $count; $i++)
{
$sql2 = mysql_fetch_assoc(mysql_query("SELECT id,game,name FROM series WHERE id='{$i}'"));
$gamearray[$i] = $sql2['game'];
$namearray[$i] = $sql2['name'];
$idarray[$i] = $sql2['id'];
}
I know it connects to the database fine as I've got a die statement when it tries to connect
The javascript is:
var id = "series_id";
document.getElementById("series_id").onblur = function()
{
var value = document.getElementById(id).value;
var gameArray = new Array(<? echo implode(',', $gamearray); ?>;
var nameArray = new Array(<? echo implode(',', $namearray); ?>;
var idArray = new Array(<? echo implode(',', $idarray); ?>;
if (inArray(value,idArray)
{
var id = parceInt(value);
var game = gameArray.indexOf(id);
var name = nameArray.indexOf(id);
var input= "You've inputted the id for the game: " + game + " for the series: " + name;
document.getElementById("spawn_series_id_check").innerHTML = input;
}
});
function inArray(var input, var array)
{
var count = array.length;
var returnVal = false;
for (var i = 0; i < count; i++)
{
if (array[i] == input)
{
returnVal = true;
}
}
return returnVal;
}
Basically the text in the series_id_check doesn't change. Could you tell me how to fix it
You should consider using JSON to handle these data.
$result = mysql_query("SELECT id,game,name FROM series");
$count = count($result);
$series = array();
while ($row = mysql_fetch_assoc($result)) {
$series[$row['id']] = array('game' => $row['game'], 'name' => $row['name']);
}
And at your JS:
document.getElementById("series_id").onblur = function()
{
var series = '<?php echo json_encode($series); ?>';
var id = parseInt(this.value, 10);
if (undefined !== series[id])
{
var game = series[id]['game'];
var name = series[id]['name'];
var message = "You've inputted the id for the game: " + game + " for the series: " + name;
document.getElementById("spawn_series_id_check").innerHTML = message;
}
});
I haven't tested the code, but this is the way you should go.
Your javascript arrays are incomplete, and will come out like this:
var gameArray = new Array(-1;
try rewriting the array building part of you code to do this for each array:
var gameArray = new Array(<? echo implode(',', $gamearray); ?>);
Notice that the javascript array and line has been closed.
added fix
if (inArray(value,idArray)
{
should be
if (inArray(value,idArray))
{
And another - try changing
function inArray(var input, var array)
{
to
function inArray(input, array)
{
Also - cannot set property of null likley means an issue with the anonymous function.
The second line of your function var value = document.getElementById(id).value
needs to end with a semicolon var value = document.getElementById(id).value;

adding dynamically added table row values into database

hi i am working on a form which has a table whic adds rows on a button click
but my problem is when i add a new row the counter value goes to 12 instead of getting 2 can anyone help me out
here is my code
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: unauthorize_access.php");
}
require("includes/dbconnect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Makhtab</title>
<link rel="stylesheet" type="text/css" href="form2/view.css" media="all">
<script type="text/javascript" src="form2/view.js"></script>
<script type="text/javascript" src="form2/calendar.js"></script>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function deleteRow(row)
{
var i=row.parentNode.parentNode.rowIndex;
document.getElementById('POITable').deleteRow(i);
}
function insRow()
{
console.log( 'hi');
var x=document.getElementById('POITable');
var new_row = x.rows[1].cloneNode(true);
var len = x.rows.length;
new_row.cells[0].innerHTML = len;
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
x.appendChild( new_row );
}
</script>
<script language="javascript" type="text/javascript">
var i=1;
function addRow()
{
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_' + i;
el.id = 'zimname_' + i;
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_' + i;
el2.id = 'zimmob_' + i;
el2.size = 13;
el2.maxlength = 13;
secondCell.appendChild(el2);
// alert(i);
i++;
makhtab.h.value=i;
alert(i);
}
</script>
<script language="javascript" type="text/javascript">
var i=1;
function addalRow()
{
var tbl = document.getElementById('alimtable');
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 = 'alimname_' + i;
el.id = 'alimname_' + i;
el.size = 40;
el.maxlength = 40;
firstCell.appendChild(el);
var secondCell = row.insertCell(1);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'alimmob_' + i;
el2.id = 'alimmob_' + i;
el2.size = 13;
el2.maxlength = 13;
secondCell.appendChild(el2);
var thirdCell = row.insertCell(2);
var element4 = document.createElement("select");
element4.name ='qabil_'+i;
var option1 = document.createElement("option");
option1.value='MUFTI';
option1.innerHTML='MUFTI';
element4.appendChild(option1);
var option2 = document.createElement("option");
option2.value='AALIM';
option2.innerHTML='AALIM';
element4.appendChild(option2);
var option3 = document.createElement("option");
option3.value='QARI';
option3.innerHTML='QARI';
element4.appendChild(option3);
var option4 = document.createElement("option");
option4.value='HAFIZ';
option4.innerHTML='HAFIZ';
element4.appendChild(option3);
thirdCell.appendChild(element4);
// alert(i);
i++;
makhtab.r.value=i;
alert(i);
}
</script>
<!--<style type="text/css" title="currentStyle">
#import "tran/css/layout-styles.css";
#import "tran/css/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>-->
<script type="text/javascript" src="tran/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="tran/js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="tran/js/jq-ac-script.js"></script>
<script type="text/javascript">
// function checkForm()
// {
// if(makhtab.code.value == "") {
// alert("Error: Code cannot be Empty!");
// makhtab.code.focus();
// return false;
// }
// if(makhtab.name.value == "") {
// alert("Error: Name cannot be Empty!");
// makhtab.name.focus();
// return false;
//}
//if(makhtab.cmbcount.value == "") {
// alert("Error: Country cannot be Empty!");
//makhtab.cmbcount.focus();
// return false;
// }
//if(makhtab.cmbstate.value == "") {
// alert("Error: State cannot be Empty!");
//makhtab.cmbstate.focus();
// return false;
// }
/* if(makhtab.cmbteh.value == "") {
alert("Error: Tehsil cannot be Empty!");
makhtab.cmbteh.focus();
return false;
}
if(makhtab.cmbcity.value == "") {
alert("Error: City cannot be Empty!");
makhtab.cmbcity.focus();
return false;
}
if(makhtab.tel.value == "") {
alert("Error: Telephone cannot be Empty!");
makhtab.tel.focus();
return false;
}
if(makhtab.mob.value == "") {
alert("Error: Mobile cannot be Empty!");
makhtab.mob.focus();
return false;
}
if(makhtab.stu.value == "") {
alert("Error: Students cannot be Empty!");
makhtab.stu.focus();
return false;
}
}*/
</script>
<SCRIPT type="text/javascript">
<!--
pic1 = new Image(16, 16);
pic1.src = "loader.gif";
$(document).ready(function(){
$("#code").change(function() {
var code = $("#code").val();
if(code.length >= 4)
{
$("#status").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "check_ct.php",
data: "code="+ code,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){
if(msg == 'OK')
{
$("#code").removeClass('object_error'); // if necessary
$("#code").addClass("object_ok");
$(this).html(' <img src="tick.gif" align="absmiddle">');
}
else
{
$("#code").removeClass('object_ok'); // if necessary
$("#code").addClass("object_error");
$(this).html(msg);
}
});
}
});
}
else
{
$("#status").html('<font color="red">The code should have at least <strong>4</strong> characters.</font>');
$("#code").removeClass('object_ok'); // if necessary
$("#code").addClass("object_error");
}
});
});
//-->
</SCRIPT>
</head>
<body id="main_body" >
<div id="form_container">
<form id="makhtab" class="appnitro" enctype="multipart/form-data" method="post" onsubmit="return checkForm()" action="submit2.php">
<div class="form_description">
<br />
<h2>Makhtab Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
<table border="0px" width="100%">
<tr>
<td><label class="description" for="element_1">Code</label></td><td><input id="element_1" id="code" name="code" class="element text small" type="text" maxlength="6" value=""/></td>
</tr>
<tr>
<td><label class="description" for="element_1">Name</label></td><td><input id="element_1" name="name" class="element text large" type="text" maxlength="40" value=""/> </td>
</tr>
<tr>
<td><label class="description" for="element_1">Address</label></td><td><input id="element_4_1" name="add1" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td></td><td><input id="element_4_1" name="add2" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td></td><td><input id="element_4_1" name="add3" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td><label class="description" for="element_1">City</label></td><td><select name="cmbcity" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
$result = mysql_query("SELECT code, `name` from city ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[code]'>$year[name]</option>";
}
?>
</select>
</tr>
<tr>
<td><label class="description" for="element_1">State</label></td><td><select name="cmbstate" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
$result = mysql_query("SELECT code, `name` from state ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[code]'>$year[name]</option>";
}
?>
</select>
</tr>
</tr>
<tr>
<td><label class="description" for="element_1">Country</label></td><td><select name="cmbcount" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
$result = mysql_query("SELECT code, `name` from country ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[code]'>$year[name]</option>";
}
?>
</select>
<tr>
<td><label class="description" for="element_1">Telephone</label></td><td><input id="element_1" name="tel" class="element text medium" type="text" maxlength="255" value=""/></td>
</tr>
<tr>
<td><label class="description" for="element_1">Mobile</label></td><td><input id="element_1" name="mob" class="element text medium" type="text" maxlength="10" value=""/></td>
</tr>
<tr>
<br />
</tr>
</table>
<tr>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<div class="form_description">
<h2>Zimmedar Details</h2>
</div>
</tr>
<table border="1px" id="zimtable" border="0px" size="100px" cellspacing="0" cellpadding="2">
<tr>
<td><strong>Zimmedar Name</strong></td>
<td><strong>Mobile</strong> </td>
</tr>
<tr>
<td><input name="zimname_0" type="text" id="zimname_0" size="40" maxlength="20" /></td>
<td><input name="zimmob_0" type="text" id="zimmob_0" size="13" maxlength="20" /></td>
</tr>
</table>
<input type="button" value="Add" onclick="addRow();" /><input name="h" type="hidden" id="h" value="1" />
<tr>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<div class="form_description">
<h2>Muallim Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
</tr>
<table border="1px" id="alimtable" border="0px" size="100px" cellspacing="0" cellpadding="2">
<tr>
<td><strong>Muallim Name</strong></td>
<td><strong>Mobile</strong> </td>
<td><strong>Qabiliyat</strong> </td>
</tr>
<tr>
<td><input name="alimname_0" type="text" id="alimname_0" size="40" maxlength="20" /></td>
<td><input name="alimmob_0" type="text" id="alimmob_0" size="13" maxlength="20" /></td>
<td><select id="qabil_0" name="qabil_0" class="element text large" style="font-size:14px;"/>
<option value="MUFTI">MUFTI</option>
<option value="AALIM">AALIM</option>
<option value="QARI">QARI</option>
<option value="HAFIZ">HAFIZ</option>
</select></td>
</tr>
<input name="r" type="hidden" id="r" value="1" />
</table>
<input type="button" value="Add" onclick="addalRow();" />
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<table border="0px" width="85%">
<tbody><tr>
<td width="105"><label class="description">No. of Students</label></td>
<td width="65"><input type="text" name=stu" size="5" maxlength="5"></input></td>
<td width="105"><label class="description">No. of Batches</label></td><td width="14"><input type="text" name="batch" size="5" maxlength="3"></input></td>
</tr>
<tr>
<input type="submit" name="submit" value="Save"></input>
</tr>
</tbody>
</table>
</div>
</div>
</form>
</body>
</html>
It seems that your problem is that you don't cast your strings as int. + can mean either concatenation or addition, depending on the types of the 2 variables it's used on.
For example:
var x = '1';
var y = 2;
alert(x+y); // 12, since x is a string and y is automatically cast to string.
alert(parseInt(x)+parseInt(y)) = 3; // since we cast both x and y to int.
PS: If you really need help, and for anyone here to read your code, copy the important bits, not the whole file.
PS2: You should start using jQuery - it can save you a lot of time

Add submit button to HTML form

I have a simple form with four input field (contact id, tel number, name, salutation). if i input 'contact id' & press Enter button, the rest of the field will auto filled retrieve from DB. now I want to add a 'submit' button to the form, so that those info can be save to another DB table by clicking on that button. i've tried to add the button, but the auto fill function is not working anymore. anyone can help? thanks in advance :)
here is my index.html file:
<html>
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction(e){
var e=e || window.event;
var keycode=e.which || e.keyCode;
if(keycode==13 || (e.target||e.srcElement).value==''){
var http; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
http = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
http = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
var url = "getagentids.php?param=";
var idValue = document.getElementById("agid").value;
var myRandom = parseInt(Math.random()*99999999); // cache buster
http.open("GET", "getagentids.php?param=" + escape(idValue) + "&rand=" + myRandom, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
function handleHttpResponse() {
if (http.readyState == 4) {
results = http.responseText.split(",");
document.getElementById('agfn').value = results[0];
document.getElementById('agsal').value = results[1];
document.getElementById('agtel').value = results[2];
document.getElementById('agid').value = results[3];
}
}
}
}
</script>
<form>
<table>
<tr>
<td>Contact ID:</td>
<td><input id="agid" type="text"
name="contactid" onkeyup="ajaxFunction(event)"></td>
</tr>
<tr>
<td>Tel Number:</td>
<td><input id="agtel" type="text"
name="contacttel"></td>
</tr>
<tr>
<td>Name:</td>
<td><input id="agfn" type="text"
name="contactfullname"></td>
</tr>
<tr>
<td>Salutation:</td>
<td><input id="agsal" type="text"
name="contactsalutation"></td>
</tr>
<tr>
<td><input type="reset" value="Clear"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
If you want to add the submit button after you populate the form, you can do this by a using innerHTML
...
if (http.readyState == 4) {
results = http.responseText.split(",");
document.getElementById('agfn').value = results[0];
document.getElementById('agsal').value = results[1];
document.getElementById('agtel').value = results[2];
document.getElementById('agid').value = results[3];
document.getElementById('buttons').innerHTML = "<input type=\"submit\" name=\"submit\" value=\"Submit\"/>"; // add Submit button by innerHTML
}
...
And in your table add a <div id="buttons"> to where you want to add the submit button
<tr>
<td><div id="buttons"></div><input type="reset" value="Clear"></td>
<td></td>
</tr>

Categories