How to add a javascript value into my SQL table? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm kind of new to javascript but I'm currently working on my website:
When I press a button, javascript generates a random number (for example: Your Coins: 25) and then I need to connect to my 'members' table and add 25 to the 'coins' field. (I'm already connected with mysql in the php code if this matters.)
Could anyone help me?

If it's "coins" then you probably won't want it to generate client side, otherwise someone would be able to call your java script function with any number they like and add in millions of coins!
The other way is to have PHP generate the number for you.
You can use something like jQuery's $.get function to call your php script with the action of "adding a random number of coins" and the php script can return the random number to java script via JSON for it to be displayed.

First the browser sends a request to the server, which, on its turn parses it. This is the time when your HTML is generated and your PHP runs. When the HTML is generated and ready, it is being sent to the web-browser. The HTML might contain script tags which are pointing to js files via the src attribute, or script tags which contain Javascript code, but before the server sends the response, the Javascript files are not loaded, Javascript code inside the scripts will not be executed. When the response arrives to the web-browser, it parses the HTML, loads the external js, css files and pictures and executes the Javascript code.
So, when your Javascript generates the value, it is running on the user's web-browser, remote from the server. Therefore, from this point the Javascript code should send an AJAX request to the server. This will post a request to the server, which, on its turn will receive and parse it. You can post parameters when you send an AJAX request. jQuery has an easy-to-use variation. Your server will receive the request with your parameters and you will be able to read the parameters via $_GET or $_POST, which are associative arrays containing parameters with their names used as indexes. You can use those to write your query.
All this is well-documented, if you watch a few tutorials, you should be able to solve the problem. On the other hand the commenters and the other answerer are right when they tell you that you should never trust the browser to generate sensitive data, as hackers could easily see what requests are being sent from the web-browser and would send similar posts where they would be "lucky".

Related

Classic ASP and Javascript Integration

I'm currently using Classic ASP and youtube javascript API in order to pull information of videos and store them into a database, however I need to know if some of the next steps are possible, or if I would have to convert to another language.
The information I am seeking to download into my SQL 2012 Database currently exceeds the maximum space allowed, meaning I can only send about 50 of my 1700 results (and growing) each time. Prior to the space cap, I would simply keep running the next page function until there is no more pagetokens and simply upload all the data, however, now I must do it in small steps.
My application currently works like this: Javascript creates hidden forms->Forms are submitted->classic ASP queries form and moves information to database
By directly editing the code I can modify which 50 results I send to the classic ASP, but I'd like to be able to do this without modifying code.
So my question is this: Is it possible to send a url query of sorts to javascript so that I know what results I have sent? Or is there a better way to circumvent the space issue aside from rerunning the javascript each time?
The error I get when attempting to spend too much information is:
Request object error 'ASP 0104 : 80004005'
Operation not Allowed
I apologize if this question seems a little vague as I'm not entirely sure how to word this without writing a 5 paragraph essay.
You could add a redirect on the ASP doing the downloading. The redirect can go back to the javascript page and include the number of results processed in the url like so:
Response.Redirect "javascript.asp?numResults=" & numberOfResultsSentSoFar
Then on the javascript page include some ASP to extract the number of results processed
dim resultsProcessed = Request.QueryString("numResults")
Then you can feed it into javascript like so:
var currentResultIndex = <%=resultsProcessed%>;
However, a better way might be to use AJAX to send the first 50 results and wait for a response from the ASP and then send the next 50.

How to do a api request on the server side before rendering the page? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a JS script on a page that makes a call to Y server on page load and it displays some data. If you look on page source you can see the script making the call to Y server.
What I need to do instead is make the api request to Y server from MY server and render the page to client completed without the JS scripts. So if you look at the page source you will not see any reference to Y server because that will all have happened on my server in the background before the page was rendered to the client.
Does anyone know how this setup can be accomplished? Looking for guidance... Links to docs? Please ask for clarification if unclear.
At a high level, you'll need to:
Implement an HTTP client for the API
Add code that calls the API (probably in a controller, but it could be a helper, model or service object)
(Optionally) parse the response (this depends on what format it's in and what format you need)
Render the parsed response in the view
2 & 4 are easy. 3 is going to be up to you, unless you can provide concrete examples. It should be easy, though. That leaves 1.
If you're using a popular API, there's a good chance that a client has already been written. If it's something in house, you can write something custom using Net::HTTP or one of the other popular HTTP client libraries. As long as you don't need to send along any cookies/headers from the browser, this should be really easy.
Here's a quick example of how this could look using RestClient and an API that returns HTML.
class SampleController < ApplicationController
def index
#mydata = RestClient.get('http://path.to/your/api?with=params')
end
end
# /app/views/index.html.erb
<h1>Here's Your Data</h1>
<%= raw #mydata %>
what you need, is to generate directly your html from your server, and render a dynamic page.
You'll have to general your html from your templates, and render it directly to the client.
For example, php, you'll have your .twig file with php variables instead of your static html.
The same goes for java, with your servlets.
The page is rendered Back side, and sent as static file to the client.
Not sure for the exact method for ROR.
I'm trying a simple example. imagine that the variable data contain the elements you want to get from your api, here is your 2 ex :
//first case staticPage1.html
[...]
var data=loadAjax(url)
window.console.log(data);
//the page served from the back is the same in the front
//second case generated.someServerSideTemplate [...]
var data={{getDataFromUrl(url)}}
//some server side templating language
generated.html [...]
var data={"message":"hello world"} // actual page rendered
i hope it's clear...

generate a file using jquery and pass it to php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
everyone, I am making a website. It can generate a MS excel file using jquery and download. The question is that I hope this generated file can be sent as an attachment in an email using php, so firstly how can I pass the generated file to php?
The following simplified code illustrates my javascript code:
$(document).ready(function() {
$("#excelExport").click(function() {
var fileexample;//file declaration before generation
....//generate the file
fileexample.click();//download the file
return fileexample;
});});
I have tried some methods to solve the problem as the following, just a reference:
pass the fileexample variable using jquery ajax POST request to php server, but it cannot work. It can only work when passing normal variable like a string.
put the javascript code and php code together in a php file. But I don't how to call the javascript fileexample variable in php code.
put the javascript code and php code together in a php file and use php to generate a MS excel file. But how to call the javascript normal variable like document.getElementById("XXX") in php code.
Hope someone can help me, thanks a lot in advance.
Judging from the title a library that does it, found from google, could be
http://excelbuilderjs.com/
As it is nicely explained on the first page the main reason to do that would be to avoid contacting the server. Otherwise there is no good reason to create an excel on the client and then send it to your server.
Regarding your methods,
1.You can post xml data created at the client from js to the server jQuery ajax post to web service
2.It is not possible to call js code in php. Javascript is executed at client side, php at server side. You can merely prepare the js that will be executed at client side, within your php code at server side.
3.This is probably towards the right direction. Prepare the xls file with php at server side and providing it as a result to php call by having visited a page or a js call via ajax. Then you could allow the download of your file from the resulting php page, or via js as a response to the ajax call.

Flow of Jsp -> Javascript webpage (Ajax?)

I am making a website but I get a bit lost programming dynamic sites.
The user needs to enter x (inside a textbox), click submit, process in java (serverside) and present outcome as a report to the user (using javascript).
I'm at the moment using JSP to process the users input but now I need to pass the JSON code into the javascript. The javascript requires JSON data.
At the moment I have JSP which returns the necessary JSON code and the Javascript which works with hardcoded JSON code. I need to somehow store the returned JSON (from the JSP) in a variable and pass it to the Javascript. I have a vague understanding of AJAX - i'm just unsure if this is possible and how to link it all together.
Thank you.
Your JSP "page" can simply generate JSON directly. There's no reason the output from JSP has to be HTML. From the client, therefore, you POST to the server, the JSP runs, and the result (pure JSON from your JSP) is sent back to the client as the ajax response.
Sounds like the perfect place for AJAX - you can submit the request with javascript, and when it comes back process it further with javascript. Unless you want the page to refresh.
If you are using jQuery, you can look here to see how to implement it, should be relatively painless: http://api.jquery.com/jQuery.post/

Retrieve a CSV file generated from a table by javascript

First, let me say that I am not familiar with the terminology so, if you see something, by all means, help me improve the wording.
What I want to do is retrieve a CSV file that is generated by a website, apparently based on a table.
The site in question has two drop boxes from which one can select the queries and then, based on `onchange=', a search is made and a table is filled.
With the table filled, a button appears, which can then be pressed and the CSV file, containing the fields, is offered to download.
After poking around with the page, I was able to find and construct the URL responsible to retrieve the CSV file. It is something like:
http://www.example.com/exportCSV.action?field1=3&field2=5
The problem is, if I try to `curl' it, a empty CSV file is retrieved, with just the headers. So, I think that the actual content must be inside the table which is filled using the normal web interface.
The last call from the javascript function that generates the CSV is:
window.open("exportCSV.action?"+fields)
Is there a way to satisfy the initial search so, when I try to curl the `CSV url' I can get a filled CSV, and not a empty one?
This rather sounds like that web site is not accepting your cURL request. Some try to limit their services to “real” browsers only.
Try using a debugging tool like FireBug to have a look at the actual data that the JavaScript sends and receives over the network.
I assume you are doing your cURL call right? Passing parameters, especially on the command line, can be a bit tricky. Make sure you escape the URL correctly, for example with single quotes:
curl 'http://www.example.com/exportCSV.action?field1=3&field2=5'
Else, the & character and possibly the question mark as well might get interpreted by your shell.

Categories

Resources