0

I have an output from a javascript file in the format

and the js files look like

$(document).ready(function()
{
$.ajax({
type: "POST",
url: "test.php",
data: { name: "term", location: "Boston" }
}).done(function( msg ) {
 alert( "Data Saved: " + msg );
 });
  });

I want to output these values in a separate excel file.. So Ive used php and wrote a script in to another file

<?php
$name = $_POST["name"];
$location = $_POST["location"];
<?php
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv'); 
$output = fopen('php://output', 'w');
fputcsv($output, array('Type', 'Entity Name', 'Link'));
fputcsv($output, results);
?>

and included the javascript file inside the header part of php file.. bt I dont get the "result" output generated by the js file. How can I pass the result from js file to php file?

1 Answer 1

2

The only way to pass data from js to php is via POST or GET requests.

This is an old school tutorial about it: http://www.tizag.com/phpT/postget.php

The "new" and easy way is via ajax with jquery: http://api.jquery.com/jQuery.ajax/

JS:

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

PHP:

$name = $_POST["name"];
$location = $_POST["location"];

In "msg" you receive what you return from your php script.

Sign up to request clarification or add additional context in comments.

5 Comments

so I can pass the array results to php file using jquery?
you can pass whatever you want via GET or POST parameters to php using jquery
Ive edited the code part in question as I did..bt its nt wrking
you have to include the latest version of jquery also: jquery.com/download and you need 2 separate files: one for js and parsing the response, and one for your php script
and the php error is also getting printed inside excel file like undefined index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.