The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Reading a file in javascript
tudsy
post Jan 1 2021, 10:57 AM
Post #1


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi

I was wondering, how do you read a text file on a server in javascript?


Thanks.

Adrian
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 1 2021, 11:44 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



I recall Ajax lets you do that. Otherwise, include it with PHP first.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 1 2021, 02:29 PM
Post #3


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



HTML5 browsers should have 'File API' support. I think you want FileReader.readAsDataURL(). I've never used it myself but it might work.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Jan 3 2021, 04:56 AM
Post #4


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi

I am trying to read a file in php: Pic1 and Personsnamejs are javascript variables.



<?php


echo "<script>var Pic1=$('#Graphic_select').val();</script>";

$pic="<script>document.writeln(Pic1);</script>";



$Pricefile = str_replace(".jpg",".log",$pic);



echo "<script>var Person = personsnamejs;</script>";

$Personsnamejs = "<script>document.writeln(Person);</script>";




$path = "https://ecovib2d.com.au/workfromhome/yourart/".$Personsnamejs."E/Prices/".$Pricefile;


$fo=fopen($path,"r");



$Priceofart =fread($fo,filesize($path));
fclose($fo);


?>


With the above code I get the following warning:


[03-Jan-2021 09:55:11 UTC] PHP Warning: fopen(https://ecovib2d.com.au/workfromhome/yourart/&lt;script&gt;document.writeln(Person);&lt;/script&gt;E/Prices/&lt;script&gt;document.writeln(Pic1);&lt;/script&gt;): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in /home/ecovibdc/public_html/workfromhome/yourart/AdrianTudiniE/yourartEX.php on line 283
[03-Jan-2021 09:55:11 UTC] PHP Warning: filesize(): stat failed for https://ecovib2d.com.au/workfromhome/yourar.../script&gt; in /home/ecovibdc/public_html/workfromhome/yourart/AdrianTudiniE/yourartEX.php on line 287
[03-Jan-2021 09:55:11 UTC] PHP Warning: fread() expects parameter 1 to be resource, boolean given in /home/ecovibdc/public_html/workfromhome/yourart/AdrianTudiniE/yourartEX.php on line 287
[03-Jan-2021 09:55:11 UTC] PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /home/ecovibdc/public_html/workfromhome/yourart/AdrianTudiniE/yourartEX.php on line 288

Any help will be appreciated.

Thanks.

This post has been edited by tudsy: Jan 3 2021, 04:57 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 3 2021, 08:57 AM
Post #5


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(tudsy @ Jan 3 2021, 10:56 AM) *

I am trying to read a file in php: Pic1 and Personsnamejs are javascript variables.

That can't work, since PHP runs on the server before the web page is sent to the browser, only after that is the javascript run in the browser. However, Ajax does let javascript make calls to the server and run e.g. PHP.

From where do you get the values for Pic1 and Personsnamejs in the first place?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Jan 5 2021, 07:16 AM
Post #6


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi


Thanks for that.


I am trying to read a txt file on the server using jquery (below):


Pricefile = Pic1.replace(".jpg",".log");


var Pricepath = 'https://ecovib2d.com.au/workfromhome/yourart/' + personsnamejs + 'E/Prices/' + Pricefile;


// read *.log files

jQuery.get(Pricepath, function(data) {




$().html('Price of your Art (AUD): $' + data + ' including GST ');

document.getElementById('Cost').value = data;

});

I am having trouble displaying the html content once and on one line.

Can you please help?

Thanks.

Adrian.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Jan 5 2021, 09:07 AM
Post #7


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi

I forgot to attach the file with the code: yourartEX.phpAttached File  yourartEX.php ( 13.45k ) Number of downloads: 210
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Jan 5 2021, 11:54 AM
Post #8


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi guys




Problem solved:


$.get(Pricepath, function(data) {




var Priceofart = data;



$('#Price2').html(function (){


return 'Price of your Art (AUD): $' + data + ' including GST ';


});

return Priceofart;

},"text");
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 29th March 2024 - 05:43 AM