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

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.

Related

$.post() html to another html file

Is it at all possible to post HTML to an HTML file using jquery's post method.
for example:
$.post("database.html", "<div>Content</div>")
once a button is clicked I want to post to the database file and have this current file load in content from the database file using.
$.load("database.html")
Does anyone know how I could implement this method properly?
If I rewrite your question a bit: you want to pass data between two html pages. If you only use static html you cannot do that with javascript only. You have to send the data to a server and then use this data in your second html file.
But I think you quite misunderstood what http can or cannot do, I would recommend reading mozilla explanation
It is possible to pass HTML content in POST from one HTML page to another. But in order to use or process received the data at the second HTML page, you'll need a programming language.
Read the accepted answer to question How to read the post request parameters using javascript.

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

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".

how to access JavaScript variable in php i have tried following but doesn't work [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
i have try to perform select query from tag with JavaScript variable but it is not work.
What wrong with it
function method12()
{
var value12=document.getElementById('period').value
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('could not connect:'.mysql_error());
}
mysql_select_db("db_loan",$con);
$sqlstr="select bankcode from tbl_master where loanaccountnumber='".value12."'";
$result=mysql_query($sqlstr);
$row=mysql_fetch_array($result);
echo $row['bankcode'];
?>
alert(value12);
}
PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.
What happens in a nutshell is this:
You click a link in your browser on your computer under your desk
The browser creates an HTTP request and sends it to a server on the Internet
The server checks if he can handle the request
If the request is for a PHP page, the PHP interpreter is started
The PHP interpreter will run all PHP code in the page you requested
The PHP interpreter will NOT run any JS code, because it has no clue about it
The server will send the page assembled by the interpreter back to your browser
Your browser will render the page and show it to you
JavaScript is executed on your computer
In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.
The only way to do what you are looking to do is either:
do a redirect to a PHP script or
do an AJAX call to a PHP script
with the values you want to be insert into the database
You can't use JS variables in PHP. JS code is run on the client and PHP code is run on the server.
To do what you want you have to send the JS value via a GET or POST request to the server and then get that value via $_POST["varname"] or $_GET["varname"]

Javascript onload MVC ASP .NET MVC [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 6 years ago.
Improve this question
Trying to stop this piece of code from running when the webpage loads.
<input type="submit" class="btn btn-info col-md-pull-4" value="Download" id="mybutton" onclick="myFunction">
<script type="text/javascript">
function myFunction() {
document.getElementById("mybutton") = #api.function();
}
</script>
Whenever I load the page, this connects to the web api and launches the api.function code. I only want this to happen when the button I have is clicked.
Am I going about this the wrong way?
From comments:
Paul: It (#api.function()) executes a workflow in an external program. Essentially just starts a process in a different application. Is there anyway to stop Razor rendering it on page load and only when I click the button?
Short answer - No. You are confusing server side and client side code. There is a distinction. The server side code is executed in the Model, in the Controller, and in the razor View. The client side code is executed using JavaScript in the context of the web browser but this code has no access to the server side code/processes. The only access it has is the same access as anything running from a browser, it uses HTTP calls to the server using URLs and passed in data.
If you want #api.function() to be called on click you have these options:
Post back - create a post back / submit and start this process from your MVC Controller. In this case remove the client script tag completely and rely on server code to start the process.
Create new public method in either your existing MVC controller OR a new Web API controller. Mark this method with HttpPost attribute and then call it using a JQuery .ajax call. This will allow the call to be submitted without the need for a form post.
I don't see why you would want to call a server method onload? can't you just load in the data that you need from start?
in any case an alternative with jquery:
$("#mybutton").on("click", function(){
// call server
})
That happens because Razor renders #api.function() and the result of the rendering is the execution of the function. Can you explain what is #api.function() and what does it do?

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...

Categories

Resources