Hi I have a javascript array of Url's in content.js file. I need to display the urls in seperate input boxes on a seperate page say display.php. Edit and delete buttons are given along with each input boxes. any change made by the user should be reflected in the content.js file.
I have shown all the url's in the input boxes. now an onclick event calls a function on button press.
My question is what will be the best way to update the array in content.js file(the file contains only one array). I'm trying to use file handling but that will require the content.js to have a strict format always. Is there a better way to modify the contents of the array or delete rows from array without file handling?
In first step, you should read the contents of JS file and pass it to json_decode function. It will give you PHP array. Use this array to display text boxes to user.
In second step, you should get the strings the user was submitted (e.g. from $_POST) and construct a PHP array from them. Then use json_encode to convert PHP array to JavaScript array represented in a string. Then you can write this string to JS file. So its content will be always valid.
You could do it with a server-side language like PHP. See this tutorial on how to write files using PHP. I would convert the array into a string using json_encode and use json_decode to convert it back into an array after reading it.
If you're only saving these URLs for the user and nobody else (so only the person entering/editing the data needs to see it) then I recommend using HTML5 localStorage
Related
I have an interactive fillable PDF form created in Acrobat. The form essentially relies on a combo box and a hidden text field containing two scripts (serving as a custom script) one that creates a number of multidimensional arrays nested inside one large array and a 2nd script that serves to populate a number of text fields residing on the PDF form by pulling data from any one of 14 multidimensional arrays depending on the combo box selection made by the end user. So far so good regarding displaying the data associated with the combo box selection. Needless to say, the data stored inside the multidimensional arrays will need to be appended and modified in time to come given data to be added, edited, and deleted. Hence, my present task entails knowing the steps required to access the multidimensional arrays in order to append and modify the data contained therein using JS. In general, I believe I first need the ability to access the entire script to be able to separate the script that creates the array from the second one. Once done, I figure I should then be able to append/modify the data in the array via scripts. Once done, I believe all that remains is to insert/replace the entire custom script so everything is in sync. What do you think, easier said than done? I hope not.
I haven't tried anything yet. Just want to make sure I'm not spinning my wheels whereby the task is impossible to achieve.
In regard to my initial post, I should have asked for a script to populate a drop-down combo box that resides on a PDF form using names stored inside a JS object converted to a JSON string that serves as a value in a hidden text field on the PDF form. Upon further study of the JS literal object, the required script is as follows:
dsFld =getField("dataSrc");// call the getField method to get the hidden text
field used to store oVendors data object as a JSON String
oVendors = JSON.parse(dsFld.value);// Parse the JSON string to manipulate the
stored oVendors data object
f = getField("cbNames");// call the getField method to get the CB drop-down
field for later use
aNames=new Array();..create a temp array
for(var key in oVendors){//script to populate the temp array from names stored
in oVendors data object
aNames.push(key);
}
aNames.sort();// sort the names alphabetically
f.setItems(aNames);// assign the names from the temp array to the combo box
field drop-down list
f.insertItemAt("Add or lookup and select a name"); // insert a string at the
beginning of the CB drop-down list
dsFld.value=JSON.stringify(oVendors);// convert oVendors back to a JSON string
Sorry for the ambiguity in my initial post and any inconvenience to the forum.
I'm trying to use AJAX to receive updates from a database without needing to reload the page. I've had very minimal problems thus far, however, I finally hit one. When retrieving information from a database, I have it convert all possible rows to an array. From there, I encode that array back to Javascript using $test1 = json_encode($array). In this same PHP function, I'm doing this to multiple pieces, converting those encoded arrays to a string by doing echo $test1."#".$test2."#".$test3. In Javascript, I split that string by the hashtag, and then grab the information of an array by doing something like test1 = array[1]. When doing that, I can retrieve that array entry as a string even though it's in the fashion of a Javascript array. Any ideas on how I can make it treat that string, which is setup like an array, as an array? Thanks in advance!
When retrieving the information from the AJAX response, you need to make it a Javascript Object. Convert the response to an object by doing JSON.parse(response) to get it to work
Forgive me if I dont describe my problem very well. I am a beginner with this.
Basically, I have a webpage which displays nine numerical values and beside these values is a form. The numerical values are stored as JSON data inside a .js file.
What I want is for when I type in a new numerical value into the form and click on submit, I want all the current values to move down one place (the last one getting deleted) and the new value I have entered to replace the first data entry.
I need this to work offline, so I cant use a regular database like MySql, and there will never be more than 9 items of data stored. Thats why I thought saving the data in the .js file would be best.
Any help would be greatly appreciated.
I know similar questions have been asked and answered before, but I see so many different answers that I'm wondering if somebody can give an answer for my specific issue.
I wish to display a list of Soccer players on a webpage, which are retrieved from a database. Each player needs a drop-down box, allowing him to be picked/unpicked/subbed in the squad. When this happens, the database needs to be updated, along with the webpage, to show his new position and to store the information. I know how to update the database with PHP, but the only way that I know how to do the webpage part, is with Javascript. I thought I'd cracked it when I fathomed out how to use 'onchange' to call a Javascript function, but that's brought up another problem. If I pass the ID of the player and his new position to the function, I'm unable to update the PHP object from inside the Javascript, because there's no way (that I know of) for the PHP code to read the passed variables. What I'd want to do inside the function, is take the passed ID parameter, use that to match up the player in the PHP array, then update him.
The only solutions that I can think of are either
A) Don't use PHP at all in the HTML page and instead pass the information to the page with json_encode... presumably then read it into a Javascript array/function. Then when the function updates the player, call an PHP file (can parameters be passed into the file this way?).
B) Somehow convert the parameters in the Javascript function to PHP. Is this possible?
C) Find another way to respond to the down-down box that calls PHP instead of Javascript. Again, not sure if this is possible.
D) Call a PHP function from within the Javascript function. Again, don't think it's possible?
Is there a simpler solution or do I go with one of the above?
Javascript should be used to enhance the user experience. It should not be mandatory for your page to work.
First do your page with HTML and PHP. HTML passes your form data via post to your PHP script. PHP then populates your database with that data.
Then add javascript if you want to enhance functionality.
With javascript you can do things like validate the form so that you don't need to send the page to PHP until is it properly validated thus by saving you a page load. Note: You should still validate using PHP afterwards, javascript is only for enhancement.
You can pass the data from javascript to PHP using ajax.
i have a list of values in javascript and the variable name is array with the values
['1','2','3'].here i want to assign this array values into a list in ftl file called list.
actually am trying to get checkbox value through js and assigning to a list which is the ftl file.
help me to overcome this issue.
FreeMarker runs on the server before the JavaScript runs on the client, so what you want is impossible. Or at least the way you want to do it, it is. You had to post that array back to the server (with a form or with AJAX), etc.
You have to send the list of values via ajax call and refresh a div to show new results.
since freemarker is a templating engine that runs on server to generate the html pages you will not be able to send any objects,lists, etc... to freemarker I could show you an example if you want.