Reading a file in javascript |
Reading a file in javascript |
tudsy |
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 |
Christian J |
Jan 1 2021, 11:44 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,650 Joined: 10-August 06 Member No.: 7 |
I recall Ajax lets you do that. Otherwise, include it with PHP first.
|
CharlesEF |
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.
|
tudsy |
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/<script>document.writeln(Person);</script>E/Prices/<script>document.writeln(Pic1);</script>): 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> 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 |
Christian J |
Jan 3 2021, 08:57 AM
Post
#5
|
. Group: WDG Moderators Posts: 9,650 Joined: 10-August 06 Member No.: 7 |
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? |
tudsy |
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. |
tudsy |
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.php yourartEX.php ( 13.45k ) Number of downloads: 210 |
tudsy |
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"); |
Lo-Fi Version | Time is now: 18th April 2024 - 04:44 AM |