5 Replies - 223808 Views - Last Post: 20 November 2008 - 02:38 AM Rate Topic: -----

#4 steevo301  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 90
  • Joined: 07-September 08

Re: Call PHP Function With Button Onclick

Posted 17 November 2008 - 06:15 PM

Hey! I'm trying to write information to a file on the server by clicking a button. I eventually want to submit actual form data in this way, but I'm trying to get this simpler code to work first. I've tried two different methods, and neither of them works.

 <html>
<head>

<script type="text/javascript">
function myfunc()
{

<?php

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Hi there!";
fwrite($fh, $stringData);
fclose($fh);

?>

}
</script>

</head>
<body>

<input type="button" onclick="myfunc()" value="Click!" />

</body>
</html>


<html>
<head>

<?php

function myfunc()
{
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Hi there!";
fwrite($fh, $stringData);
fclose($fh);
}

?>

</head>
<body>

<input type="button" onclick="myfunc()" value="Click!" />

</body>
</html>


I know it would be easier to just have an html file and a php file, but I'd really like to have it all in one page. If anyone can help, I'd really appreciate it.

This post has been edited by steevo301: 17 November 2008 - 06:27 PM

Was This Post Helpful? 1

#5 noobiez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-August 08

Re: Call PHP Function With Button Onclick

Posted 17 November 2008 - 08:36 PM

Hi,

I'm a noob but won't having both in file make it easier for people to know how the data is processed and where it is? And in turn get it?

-Xyber.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,008
  • Joined: 08-August 08

Re: Call PHP Function With Button Onclick

Posted 17 November 2008 - 08:38 PM

You want to use a form:
<?php
if (isset($_POST['sometext']))
	{
	$myFile = "testFile.txt";
	$thetext=$_POST['sometext'];
	writemyfile($myFile,$thetext,"w");
	} else
	{
	$thetext="Enter text here";
	}
	
function readmyfile($thefile)
	{	
	$myfile=fopen($thefile,"r");
	$x = fread($myfile, filesize($thefile));
	fclose($myfile);
	return $x;
	}
		
function writemyfile($thefilename,$data,$mode)	
	{
	$myfile=fopen($thefilename,$mode);
	fwrite($myfile,$data);
	fclose($myfile);
	}	
?>
<html>
<head>
<title>Example of a form</title></head>
<form method="post" action="<?php echo $php_self ?>">
<input type="text" name="sometext" value="<?php echo $thetext ?>" >
<input type="submit" name="Submit" value="Click this button">
</form>
</html>


Was This Post Helpful? 1

#7 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Call PHP Function With Button Onclick

Posted 18 November 2008 - 08:20 AM

This is PHP you're talking about, not ASP.NET. In PHP, there is no such thing as a button click event. PHP runs entirely on the server and has absolutely no knowledge of client-side events.

Your first try won't work because the PHP code only runs when the page first loads. It does not run when you call a Javascript function. Your second example won't work because Javascript and PHP can't talk directly to eachother like that. Trying to directly call a PHP function from Javascript just doens't make sense. Remember, PHP only runs on the server. By the time you get to the point where Javascript can run, the PHP code has long since completed its work.

If you want to do something when a button is clicked, you have to explicitly make a request back to the server. You can do this by just POSTing the form as CTphpnwb suggested. Just be aware that this will reload the page and you will have to manually save and restore the page state, e.g. repopulate input boxes. There is no built-in magic that will do this for you.

Alternatively, you can get all AJAXy and do the POST in Javascript. However, you will have to write the Javascript to send the request and process the response, and write the server-side PHP code to handle the request. This gets a little awkward to do in a single page.
Was This Post Helpful? 1

#8 bombshop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-November 08

Re: Call PHP Function With Button Onclick

Posted 20 November 2008 - 02:12 AM

 function myfunc()  
 {  
   
 <?php  
   
 $myFile = "testFile.txt";  
 $fh = fopen($myFile, 'w') or die("can't open file");  
 $stringData = "Hi there!";  
 fwrite($fh, $stringData);  
 fclose($fh);  
   
 ?>  
   
 }  


will result in
 function myfunc()  
 {  
   
 Output of the php lines in the source of the page!
   
 }  


So you wont be able to interact them :(

This post has been edited by bombshop: 20 November 2008 - 02:14 AM

Was This Post Helpful? 0
  • +
  • -

#9 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: Call PHP Function With Button Onclick

Posted 20 November 2008 - 02:38 AM

You're mixing up server side and client side coding. onclick is a Javascript trigger, that triggers a Javascript piece of code.

However, PHP is running on your server. So you need to call the PHP from your Javascript. There is a solution: AJAX. Look for some tutorials how to use them.

For wirting a file, JS has very tight security. You will not be able to change files on a system with default security settings.


Edit: in your OP you state POSTing the form is harder; it certainly is NOT. If a form with a field is enough, start iwth that, that's the easiest way to call a php script form a web site.

This post has been edited by Hary: 20 November 2008 - 02:40 AM

Was This Post Helpful? 1

Page 1 of 1