4 Replies - 2769 Views - Last Post: 24 December 2012 - 08:43 PM

#1 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

JQuery POST with Ajax to over write txt file not working

Posted 23 December 2012 - 09:37 PM

Hello dreamincode! I've been trying to create a counter for software download on my site (specifically at http://www.gfcf14gre.../smssender.html ), by reading a txt file called "smssender.txt", getting its value, and once the download button is pressed, increment a javascript variable that would initially hold the value of the text file, then use post() to call a php named "overwritefile.php", and overwrite smssender.txt using php. So far, I have this:

The smssender.txt file:

0



The script tag in the page:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script type="text/javascript">
  
    function handleTextFile()
	{
		document.getElementById('counter').style.color = "rgb(0, 255, 0)";
		document.getElementById('counter').style.fontWeight = 'bold';
		
		var downcounter = 0;
		
		$.get("http://www.gfcf14greendream.com/counters/smssender.txt", function(data){
			downcounter = data;
			if (downcounter == 1)	$("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");	
			else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");	
		});
		
		$("#downbutton").click( function(){
			downcounter++;
			if (downcounter == 1)	$("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");	
			else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
			
			$.post("http://www.gfcf14greendream.com/PHP/overwritefile.php", {counter: downcounter, file: "http://www.gfcf14greendream.com/counters/smssender.txt" }, function(data){
				
			});
		});
		
	}
    
  </script>



and the overwritefile.php:

<?php
	$counter = $_POST['counter'];
	$file = $_POST['file'];
	file_put_contents($file, $counter);

?>



I know next to nothing about PHP, but I needed this because I would like a counter for my page, without actually having to get a pre-made like the one I have at this other page ( http://www.gfcf14gre...votebuster.html ). The function handleTextFile() is run on the HTML body tag:

<body onload="handleTextFile()">



The 'counter' id is for a DIV that will be set to display the number of times the program is downloaded. This part:

var downcounter = 0;
		
$.get("http://www.gfcf14greendream.com/counters/smssender.txt", function(data){
downcounter = data;
if (downcounter == 1)	$("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");	
else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");	
});



, works just fine and "SMS sender has been downloaded 0 times..." is displayed. The post part is what I'm not getting right:

$("#downbutton").click( function(){
	downcounter++;
	if (downcounter == 1)	$("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");	
	else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
			
	$.post("http://www.gfcf14greendream.com/PHP/overwritefile.php", {counter: downcounter, file: "http://www.gfcf14greendream.com/counters/smssender.txt" }, function(data){
				
	});
});



The object for the "downbutton" id is a css div button. The variable downcounter is easily incremented and displayed, but if I check the file at http://www.gfcf14gre...s/smssender.txt I still see a 0. Is it that jquery cannot call an external php? Or am I just calling it incorrectly? Please let me know your thoughts, thank you

Is This A Good Question/Topic? 0
  • +

Replies To: JQuery POST with Ajax to over write txt file not working

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: JQuery POST with Ajax to over write txt file not working

Posted 23 December 2012 - 10:21 PM

The problem here is that using file_put_contents() over in your PHP script requires allow_url_fopen to be set to "on" if you want to give it a URL as the filename.... which you are doing here. Because your option is set to off, you are actually encountering an error on your server along the lines of URL file-access is disabled in the server configuration. Because it errors out, it is not writing the file.

So your options are to set the URL in the PHP file itself and just call the file using your $.post()... in other words don't send the smssender.txt file as a parameter or to set allow_url_fopen to "on" in your php.ini (if you are able). This second option may also open you up to potential problems later.

My advice would be to write in your overwrite.php file the smssender.txt file path (not a url) in there and just call your overwrite.php from jquery.

:)
Was This Post Helpful? 0
  • +
  • -

#3 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: JQuery POST with Ajax to over write txt file not working

Posted 23 December 2012 - 11:02 PM

I've changed the script's $("#downbutton").click( function() to:

$("#downbutton").click( function(){
	downcounter++;
	if (downcounter == 1)	$("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");	
	else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
			
$.post("/PHP/smssender.php", {counter: downcounter }, function(data){
				
	});
});



and the PHP, now smssender.php:

<?php
	$counter = $_POST['counter'];
	file_put_contents("/counters/smssender.txt", $counter);

?>



And still there's no overwrite. Is post even called properly? Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: JQuery POST with Ajax to over write txt file not working

Posted 24 December 2012 - 12:30 AM

The $.post() path should be a full URL (like you had before), the path in the PHP file should be a relational path like you have now.

I would first work on making sure that your overwrite.php file is writing correctly. Then move on to debugging your jQuery code.
Was This Post Helpful? 1
  • +
  • -

#5 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: JQuery POST with Ajax to over write txt file not working

Posted 24 December 2012 - 08:43 PM

I changed like so:

$.post("http://www.gfcf14greendream.com/PHP/smssender.php", {counter: downcounter }, function(data){



yet it still doesn't work. How do I check if the php is correct?

<?php
	$counter = $_POST['counter'];
	file_put_contents("/counters/smssender.txt", $counter);

?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1