Syntax error in jquery ajax call - html

My HTML is like this
<tbody>
<tr>
<td ><img src="" alt="close" /></td>
<td><input type="hidden" name="addproducts" value="141420">141420</td>
<td class="prd"><strong></strong></td>
<td><a rel="prettyPhoto" href=""><img src="" alt="Product"></a></td>
</tr>
<tr>
<td ><img src="" alt="close" /></td>
<td><input type="hidden" name="addproducts" value="1213143">1213143</td>
<td class="prd"><strong></strong></td>
<td><a rel="prettyPhoto" href=""><img src="" alt="Product"></a></td>
</tr>
<tr>
<td ><img src="" alt="close" /></td>
<td><input type="hidden" name="addproducts" value="242424">242424</td>
<td class="prd"><strong></strong></td>
<td><a rel="prettyPhoto" href=""><img src="" alt="Product"></a></td>
</tr>
</tbody>
I want select all hidden inputs with name addproducts from this and add to an ajax call.
The problem is that I can't predict how many elements will be there before the code execute.
The ajax url i am trying to make will be like this
http://mydomain.com?addproducts=141420&q141420=16&addproducts=X945X2MG&qX945X2MG=1&addproducts=8382355&q8382355=10&addproducts=146353&q146353=3
my usual code for specific parameters in url will be some thing like this
ajaxManager.add(({
type: 'GET', url: '/ajaxhandler', data: { addproducts: X945X2MG,qX945X2MG:1}
but here i can't use this because of unpredictable parameters.
any way i had made some try which ended up in syntax error.code is this
ajaxManager.add(({
$(this).parent().parent().find(".antal").find("input:hidden[name='addproducts']").map(function () {
return
type: 'GET', data: {addproducts:this.value,'&q'+$(this).val():$(this).next().val()}
EDIT:from Alnitak's post i have tried to edit this .
new code
var data = $(this).parent().parent().find(".antal")
.find("input:hidden[name='addproducts']").map(function () {
return
{ addproducts: this.value}
data['q' + $(this).val()] = $(this).next().val();
}).get().join(',')
ajaxManager.add(({
type: 'GET', data: data
but unfortunatedly it ended up my ajax call comes like this
http://mydomain.com?_=1365768440633
I am sure I have made some thing terribly wrong.Cany one help me on this

You should create an object literal with the known keys, and then use obj[key] syntax for the variable keys:
var data = { 'addproducts[]': [] };
$(this).parent().parent().find(".antal")
.find("input:hidden[name='addproducts']")
.each(function () {
data['addproducts[]'].push(this.value);
data['q' + this.value] = $(this).next().val();
});
$.ajax({
...,
data: data
});
Note the use of addproducts[] to allow encoding of an array of values for that particular parameter. Note that there's not actually a defined standard for passing multiple values with the same key for x-www-form-urlencoded data, but this works with PHP. You should consider changing the way your products are added - perhaps a comma-separated list?

Related

Fetching text data based on input given in jsp using ajax

I want to get text field data based on input given by the user.
Here is my code. This code will fetch data in dropdown(select), but I need to get data in text field.
function getEventname() {
var clid = $('#eventid').val();
console.log("i am ok" + clid);
$.ajax({
type: "GET",
url: "EduManage.jsp",
data: {
control: 'ajax',
ch: '1',
key: '1_0a1m_1',
eventid: clid
},
success: function(data) {
data = data.trim();
addOptions(eventname, data.split(", "), data.split(", "));
}
});
}
<td class="bg1">Event Id
<edu:prnStar/>
<img src="../images/util/final.gif" name="search" width="20" height="20" alt="Search" border="0">
</td>
<td class="bg1" width="25%">
<edu:input name="eventid" type="text" id="eventid" size="10" maxlength="10" onblur="getEventname()" />
</td>
<td class="bg1" width="40%"> Event Name
<edu:prnStar/>
</td>
<td class="bg1" width="25%"><input type="text" name="eventname" id="eventname">
</td>
In the key '1_0a1m_1' am having the query which is fetching the data
SELECT eventname FROM cam_eventdetail WHERE eventid=111

checking a single checkbox per html row using angular js

i have a table that loads multiple checkboxes and selectboxes.when i click on one checkbox or select box it automatically selects every other box on the table .i want to have the option to choose either one checkbox or select box per row on it own.
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
<td ng-bind="code .CodeID"><input type="text" ng-model="Register.CodeID" /></td>
<td ng-bind="code .name"><input type="text" ng-model="Register.Firstname" /></td>
<td ng-bind="code .Lastname"><input type="text" ng-model="Register.Lastname" /></td>
<td><input type="checkbox" ng-model="Register.Presentstatus" id="PresentCheckbox" name="PresentCheckbox" /></td>
<td><select id="reasons" name="reasons" ng-model="Register.Category" ng-disabled="Register.Presentstatus" ng-clicked="Register.Presentstatus && O" ></td>
</tr>
my module that gets my data
function Register(){ self.RegisterDetails = function () {
var params = { pass params here };
return $http.get
{
url: GetRegisterDetails,
params: params,
success: function (data) {
self.RegisterDetails = data.data;
}
});
}
my controller
ngAppModule.controller('RegisterController',['$scope','$http',function($scope,$http)
{
var self = this;
$scope.Register = new Register($http);
}]);
all the above code works fine. i just dont know how to check a single box per row.sorry im new to this site
The main problem you're running into is you are binding to the wrong this inside your ng-repeat.
Your HTML currently repeats a checkbox for every code, but binds that to the same object property Register.Presentstatus.
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
....
<td><input type="checkbox" ng-model="Register.Presentstatus" .../></td>
</tr>
You'll need to bind this to a row-specific (code-specific) property if you want each row to have independent check boxes. Perhaps you are looking for something that binds the checkbox to an element in an array:
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
....
<td><input type="checkbox" ng-model="Register.Presentstatus[$index]" .../></td>
</tr>
or actually binds to a property of the code object
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
....
<td><input type="checkbox" ng-model="code.Presentstatus" .../></td>
</tr>
Since youur ng-model="Register.Presentstatus" is repeating and is same for all so you have same binding for all rows. you can alter them to have different binding

How to navigate through html tab with casperjs

i need your experience in casperjs!
I am trying to access a web page (which is not an issue) and to navigate through a html tab.
When i access the page it is by default always showing the first tab "General" but i need to switch to "Options" tab so that i can access one field that i am interested to modify the value!
Sorry, can't post images yet!
Html code:
<b> <b>
<table class="commonfont" cellpadding="0" cellspacing="0" background="/tab_between.png" border="0">
<tbody>
<tr>
<td><input name="" src="/tab_sel_left.png" border="0" type="image"></td>
<td align="center" background="/tab_sel_bg.png">
General
</td>
<td><input name="" src="/tab_sel_right.png" border="0" type="image"></td>
<td width="1"></td>
<td><input name="" src="/tab_unsel_left.png" border="0" type="image"></td>
<td align="center" background="/tab_unsel_bg.png">
Options
</td>
<td><input name="" src="/tab_unsel_right.png" border="0" type="image"></td>
<td width="1"></td>
</tr>
</tbody>
</table>
...
</b></b>
My casper.js file looks like this:
...
casper.then(function() {
test.assertTextExists("DB", "Login into DB 2");
this.click('a#changeThis1_link');
});
casper.then(function() {
test.assertTextExists("Options", "DB Options");
this.click('a#menu1itemUnSel[tabindex="4"]');
});
casper.then(function() {
test.assertTextExists("Change", "DB -Change -Step 1/2");
this.fill('form[name="dbActionForm"]', {
'generalParams.poolSize': '1',
}, false);
this.click('input[name="Apply"]');
});
...
I just can't figure out what should this line look like:
this.click('a#menu1itemUnSel[tabindex="4"]');
since this isn't working!!!
Execution printout:
modifying DB pool size:
Test file: /target.js
# DB modify
PASS DB has the correct title
PASS login form is found
PASS Login into DB
PASS Login into DB 1
PASS Login into DB 2
PASS DB Options
FAIL Cannot dispatch mousedown event on nonexistent selector: a#menu1itemUnSel[tabindex="4"]
# type: uncaughtError
# error: Cannot dispatch mousedown event on nonexistent selector: a#menu1itemUnSel[tabindex="4"]
# CasperError: Cannot dispatch mousedown event on nonexistent selector: a#menu1itemUnSel[tabindex="4"]
# at mouseEvent (/casper.js:1323)
# at click (/casper.js:428)
# at /target.js:34
# at runStep (/casper.js:1523)
# at checkStep (/casper.js:368)
# stack: not provided
Any clue what i am doing wrong and how can i overcome this issue?
Thanks for your time
Some updates and more information:
After Fanch information, I changed the casperjs to:
...
casper.then(function() {
test.assertTextExists("DB", "Login into DB 2");
this.click('a#changeThis1_link');
});
casper.then(function() {
test.assertTextExists("Options", "DB Options");
this.click('a.menu1itemUnSel[tabindex="4"]');
});
casper.then(function() {
test.assertTextExists("Change", "DB -Change -Step 1/2");
this.fill('form[name="dbActionForm"]', {
'generalParams.poolSize': '1',
}, false);
this.click('input[name="Apply"]');
});
...
This a.menu1itemUnSel[tabindex="4"] solved my error about changing the tab but i still have the issue with reading/changing the field generalParams.poolSize.
I even added then waitForSelector/waitForText but still get the error: Errors encountered while filling form: form not found
See casperjs:
...
casper.then(function() {
test.assertTextExists("Options", "DB Options");
this.click('a.menu1itemUnSel[tabindex="4"]');
});
casper.waitForSelector("a#dve_menu_datarepositories", function() {
this.echo("1.Loading form");
});
casper.waitForText("50", function() { //the field that i want to change has text '50'
this.echo("2.Loading form");
});
casper.then(function() {
test.assertTextExists("Change", "DB -Change -Step 1/2");
this.fill('form[name="dbActionForm"]', {
'generalParams.poolSize': '1',
}, false);
this.click('input[name="Apply"]');
});
...
Thanks again
Sorry i was away for a while, here is some html part of the page:
<a name="topofpage">
<form autocomplete="off" enctype="application/x-www-form-urlencoded" action="/db.do" method="post" name="dbActionForm">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr height="100%">
<td valign="top" height="100%"></td>
<td valign="top" height="100%" background="/menu3_sel_right1.png"></td>
<td class="commonfont" width="100%" valign="top" background="/menu3_sel_right1.png" align="left">
<b>
<b>
<table class="commonfont" border="0" cellspacing="0" cellpadding="0" onkeypress="return onWizardPageKeyPress(event);">
<tbody>
<tr id="generalParams.poolSize_TR" class="formpagefieldname" style="display:table-row;">
<td>
<a id="generalParams.poolSize_changeA" style="visibility:hidden;" title="Undo Change" onclick="revertSingleChange('generalParams.poolSize', false); dependantsRunOnLoad(document.body); return false;" href="javascript:;"></a>
</td>
<td>
<input type="text" onblur="if (!this.disabled) {onChangeProperty(this, false);} " onpropertychanged="if (!this.disabled) {onChangeProperty(this, false);} " onkeyup="if (!this.disabled) { onChangeProperty(this, false); }" tabindex="2" title="10" size="30" value="50" name="generalParams.poolSize"></input>
</td>
</tr>
</tbody>
</table>
</b>
</b>
I still have the problem filling in the form!
FAIL Errors encountered while filling form: form not found
# type: uncaughtError
# error: Errors encountered while filling form: form not found
Any clue more?
Thanks for your time!!!
Use this :
this.click('a.menu1itemUnSel[tabindex="4"]');
-> # = div, . = class

Retrieving values from a table cell <td> to a controller

I'm trying to get a specific value from a table cell and pass is to a controller. But it doesn't seem to be working. I show you some of my codes:
This is in the controller:
def searchUser = {
String abc = request.getParameter("harrow")
println(abc)
}
This is in the html page:
<form>
<div style="height: 250px; overflow: scroll; width: 100%;">
<table id="normal">
<g:each in = "${result}">
<tr id="btn">
<td width=10% >${it.ID}</td>
<td width=25% id="harrow">${it.username}</td>
</tr>
</g:each>
</table>
</div>
<input type ="submit" name ="abc" id="opener">
</form>
EDIT
AJAX:
$("#edittable").on('click', function() {
$.ajax({
url: URL,
data: $(this).serialize(),
type: "POST",
success: function(html){
//do something with the `html` returned from the server here
$("#edit1").html(html).dialog("open")
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
});
return false;//suppress natural form selection
});
I can get the value to pass to the controller, but right now it only retrieves the first row of values rather than the other. Is there something wrong with my AJAX codes? Thank you guys so much.
So to show details of the row, one of the approaches can be:
CONTROLLER METHODS
def rows = [
[id:1, name:'name1'],
[id:2, name:'name2'],
[id:3, name:'name3']
]
def show(){
[results:rows]
}
def showLine(Long id){
render rows.find {it.id == id }
}
VIEW
<html>
<head>
<g:javascript library="jquery" />
<r:layoutResources />
</head>
<body>
<table>
<thead>
<tr>
<th>Action</th>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<g:each in="${results}" status="i" var="r">
<tr>
<td><g:remoteLink value="Details" id="${r.id}" action="showLine" update="lineDetails">Details</g:remoteLink></td>
<td>${r.id}</td>
<td>${r.name}</td>
</tr>
</g:each>
</tbody>
</table>
<div id="lineDetails">
</div>
</body>
</html>
Basically you call showLine method using AJAX and passing row object id. Then you search for the object and render it back. Rendered data is put into div under the table. It's up to you if you use onclick, button or link in the table. It's also up to you how to show details on results page - use jquery dialog, or something else. Hope it helps

store data from a form using just HTML

I want to create a form (will be filled by users) and store the data in excel stylesheet without using php just HTML ,is that possible?
I dont want to store data an a database.
I have tried to use google doc but it's not that good because the validation messages are generated depending on the browser language.
The unqualified response of "You can't write a file from HTML" is inaccurate. While you may need to add some "hidden" fields in your HTML (in order to simplify the exporting of only the data requested and not the questions or other text) it is ABSOLUTELY possible to do this. I've done JUST THAT in the code below - and all I use is JavaScript. No Server required, No Database required, No PHP required.
Below is the code and a link to the JSFiddle page where you can see it in action:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function fillHidTable(){
var htqf; //-- hidden field
var rf; //-- retrieved field
for ( var i = 1; i < 5; i++ ) {
rf = "htqf"+i;
document.getElementById(rf).innerHTML = document.getElementById("Q"+i+"CALC").value;
}
tableToExcel('hidTable', 'Analysis Results');
}
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
<title>HTML Form Data to Excel</title>
<style type="text/css" media="screen">
.divCenMid{font-family:Arial,sans-serif;font-size:14pt;font-style:normal;font-weight:700;text-align:center;vertical-align:middle;margin:0;}
.allbdrCenMid{border:.75pt solid windowtext;color:#000;font-family:Arial,sans-serif;font-size:10pt;font-style:normal;font-weight:400;text-align:center;vertical-align:middle;margin:0;}
.allbdrCenTop{border:.75pt solid windowtext;color:#000;font-family:Arial,sans-serif;font-size:10pt;font-style:normal;font-weight:400;text-align:center;vertical-align:top;margin:0;}
.allbdrLtMid{border:.75pt solid windowtext;color:#000;font-family:Arial,sans-serif;font-size:10pt;font-style:normal;font-weight:400;text-align:left;vertical-align:middle;margin:0;}
.allbdrLtTop{border:.75pt solid windowtext;color:#000;font-family:Arial,sans-serif;font-size:10pt;font-style:normal;font-weight:400;text-align:left;vertical-align:top;margin:0;}
</style>
</head>
<body>
<table width= "565px" cellspacing="0" cellpadding="0" style="border-spacing:0;" id="QMSTable">
<col width="25px"/>
<col width="120px"/>
<col width="360px"/>
<col width="60px"/>
<tr>
<td class="divCenMid" colspan = "4"> QMS Assessment</td>
</tr>
<tr>
<td class="allbdrCenMid"> No</td>
<td class="allbdrCenMid"> Criteria</td>
<td class="allbdrLtMid"> Question</td>
<td class="allbdrCenMid"> Score</td>
</tr>
<tr>
<td class="allbdrCenTop"> Q1</td>
<td class="allbdrLtTop"> Quality Unit Independency</td>
<td class="allbdrLtTop"> Do you have the Quality Unit?</td>
<td class="allbdrCenMid">
<input id="Q1CALC" type="text" value="" class="nobdrCenMid" style="overflow:hidden; width:93% " name="Q1CALC"/>
</td>
</tr>
<tr>
<td class="allbdrCenTop"> Q2</td>
<td class="allbdrLtTop"> Apply PICS GMP</td>
<td class="allbdrLtTop"> Which GMP regulation do you use?</td>
<td class="allbdrCenMid">
<input id="Q2CALC" type="text" value="" class="nobdrCenMid" style="overflow:hidden; width:93% " name="Q2CALC"/>
</td>
</tr>
<tr>
<td class="allbdrCenTop"> Q3</td>
<td class="allbdrLtTop"> Deviation or Non-conformance</td>
<td class="allbdrLtTop"> Do you have a deviation or non-conformance procedure?</td>
<td class="allbdrCenMid">
<input id="Q3CALC" type="text" value="" class="nobdrCenMid" style="overflow:hidden; width:93% " name="Q3CALC"/>
</td>
</tr>
<tr>
<td class="allbdrCenTop"> Q4</td>
<td class="allbdrLtTop"> Complaint</td>
<td class="allbdrLtTop"> Do you have a customer complaint procedure?</td>
<td class="allbdrCenMid">
<input id="Q4CALC" type="text" value="" class="nobdrCenMid" style="overflow:hidden; width:93% " name="Q4CALC"/>
</td>
</tr>
</table>
<div id="hidTable" style="display: none">
<table id="testTable">
<caption>Supplier Risk Analysis</caption>
<colgroup></colgroup>
<colgroup></colgroup>
<colgroup></colgroup>
<thead>
<tr>
<th>No.</th>
<th>Question</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>Q1</td>
<td>Do you have the Quality Unit?</td>
<td id="htqf1">-</td>
</tr>
<tr>
<td>Q2</td>
<td>Apply PICS GMP?</td>
<td id="htqf2">-</td>
</tr>
<tr>
<td>Q3</td>
<td>Do you have a deviation or non-conformance procedure?</td>
<td id="htqf3">-</td>
</tr>
<tr>
<td>Q4</td>
<td>Do you have a customer complaint procedure?</td>
<td id="htqf4">-</td>
</tr>
</tbody>
</table>
</div>
<input type="button" onclick="fillHidTable()" value="Export Data to Excel">
</body>
</html>
Here is the JSFiddle link: https://jsfiddle.net/MitchinThailand/LV9vr/
if you want more details feel free to holler.
No, HTML pages cannot write files. You need a server to do this.
The best you can do is generate CSV data in a textarea that the user could then copy and paste to a local file, then load that into Excel.
As it is not possible to save html form data to a file using javascript because of some security reason so for my solution i just use the TCPDF for this.
You can generate a data: URL with the download attribute:
<a download="test.csv" href="data:text/csv,foo,bar,baz">
You'll need to use JavaScript to build such URL from form data and insert/update appropriate link in the document.
To do what you want to do simply it will not be possible without php or some advanced HTML5 local storage.
I've done this by using simple PHP script to have form data get saved to a .txt file and then open the resulting .txt file in Excel and use the text to columns feature.
I have a HTML form which collects a field where people enter their email address. I want the form to post the email address to a text file. Please help! Will award maximum points to the one who will answer me correctly!
2 years ago Report Abuse
Additional Details
Please paste entire code to do this!
2 years ago
Form:
<form method="post" action="nameofyourscripthere.php">
Name: <input type="text" name="name" id="name" />
Email: <input type="text" name="email" id="email" />
<input type="submit" name="submit" value="Send Form" />
</form>
PHP:
Create a new page saved as .php with this code. All you need is the form and the PHP script on the server for this to work :)
<?php
// Get the name they entered in the form
// We'll be naming the file this
$file = $_POST['name'];
// Get the email from the form
$email = $_POST['email'];
// We want the file to be a text file right?
$ex = ".txt";
// Try to open a file named $file$ex (johndoe.txt for example)
// Because this file doesn't exist yet the server creates it
$write = fopen("$file$ex","w");
// Now open the file up again but this time save the email in it
fwrite($write,$email);
// MAKE SURE you close the file!!!
fclose($write);
// The folder that this script is in on the server is where the file we just made was saved
// We can 'rename' it to another folder
// The folder on the server we want to move it to
$data = "../emails/";
// Now put it all together: This example goes out of the folder we're in and into the folder 'emails'
// The new 'name' would be this now (../emails/johndoe.txt): So now the file is moved to where we want for storage
rename ("$file","$data$file$ex");
// The script is done, send the user to another page (Just read the address below and you'll get it)
// Its just an example fyi change to what you want
header('Location: http://YourWebsiteNameHere.com/contactFo…
exit;
?>