Enviar cadena desde php a archivo js usando jquery

i need to get the string from on php file someone know how can i do this?

My.php that write and read contenute of one file

<?php
 $dir = '/var/www/devData/test';

 // create new directory with 777 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }
 if (isset($_POST['data'])) {

         $stringData = $_POST['data'];
         $file = "/var/www/devData/test/ciao.txt"; 
         $fh = fopen($file, 'r+') or die("can't open file");
         fwrite($fh, $stringData);
         $theData = fread($fh, filesize($myFile)); //this is the string that i have to pass
         fclose($fh); 

         return $theData;

 }


 ?>

my.js scrpt that use the methode get for retrive the strng from my php file

 function addLabelCustom_options() {
  var select = document.getElementById('label_custom');

 $.get("JS/foo.php", function(result){ alert(result) }, "json");


 /* $.ajax({
  url:'/var/www/devData/test/ciao.txt',
  success: function (data){
  //parse ur data
  //you can split into lines using data.split('\n') 
  //use regex functions to effectivley parse
  var label_parsed = data.splitCSV();
    select.options[0] = new Option("-- Select Label --",0);
    var label_sort = new Array();

    for (var i=0; i<label_parsed.length-1; i++)
        label_sort.push(label_parsed[i][1]);

    label_sort.sort();
    for (var j=0; j<label_sort.length-1; j++)
        select.options[j+1] = new Option(label_sort[j],j+1);

}
});

*/
}

i Just need to get the contenute of the string $theData Looks like if i don't retrive any string....

preguntado el 31 de julio de 12 a las 10:07

Have you tried adding the success, error and complete to the get request to make sure you are getting the data correctly ? -

yes and even if i send like that echo $theData "ciao" the alert box is still empty -

3 Respuestas

<?php
 $dir = '/var/www/devData/test';

 // create new directory with 777 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }
 if (isset($_POST['data'])) {

         $stringData = $_POST['data'];
         $file = "/var/www/devData/test/ciao.txt"; 
         $fh = fopen($file, 'r+') or die("can't open file");
         fwrite($fh, $stringData);
         $theData = fread($fh, filesize($myFile)); //this is the string that i have to pass
         fclose($fh); 

         echo $theData;  // <-- ECHO

 }


 ?>

You have to OUTPUT the data, not return it. That is, echo it. If the string is not yet json encoded, use echo json_encode($theData);

Respondido 31 Jul 12, 10:07

really thanks but my alert box has as result null. but the file is not empty!! - Edivad

You can see your network traffic (the call, header info, response etc) desarrolladores.google.com/chrome-developer-tools/docs/network - usuario657496

After echo the content in php function (echo $theData; ) try the following jquery code

$.ajax({
 url:JS/foo.php,
 success:function(result){ alert(result) }, "json"),
 error:function(alert('Not Completed'))
};

And also check the response in the console to check for any errors.You can view the console of F12 in firefox

Respondido 31 Jul 12, 11:07

the output is every time null and there are no errors i recive the data but it looks like empty - Edivad

use echo json_encode($theData);die; if that doesn't help either then echo a simple string like echo "hello World"; die; and then check the response if it is still null then there should be some problem in javascript and if not null then there should be problem in your file reading code in php - sumit neema

is the same every time i get the value = null - Edivad

change the condition is(isset($_POST['Data'])).There is nothing which is posting to that page .try to use by removing that condition - sumit neema

If i change with one simple hello world i have no alert box as output - Edivad

Guys i solved the problem like every time is stupid thing and every time i understand that i'm really stupid however i post the solution.

firs php file used for create a dir and store on file

<?php
 $dir = '/var/www/devData/test';

 // create new directory with 777 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }
 if (isset($_POST['data'])) {

         $stringData = $_POST['data'];
         $file = "/var/www/devData/test/ciao.txt"; 
         $fh = fopen($file, 'r+') or die("can't open file");
         fwrite($fh, $stringData);
         $theData = fread($fh, filesize($file));
         fclose($fh); 

        echo json_encode($theData);
 }


 ?>

For read the contenute i create another file php

<?php


        $file = "/var/www/devData/test/ciao.txt"; 
            $fh = fopen($file, 'r') or die("can't open file");
        $theData = fread($fh, filesize($file));<--- was the error before i used Myfile but that variable wasn't declare and was empty :) lol 
        fclose($fh); 

        echo json_encode($theData);


 ?>

thanks to everybody !!

Respondido 31 Jul 12, 12:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.