LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP: How to can a PHP function without refreshing the page? (https://www.linuxquestions.org/questions/programming-9/php-how-to-can-a-php-function-without-refreshing-the-page-720797/)

frenchn00b 04-21-2009 10:23 PM

PHP: How to can a PHP function without refreshing the page?
 
Hello,

I have a task01 function. I would like that the click on the button, does not refrest hte page. I does not work... :( :(

Here is the code:

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
 

 
 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

 
<body>

 
 
 <?php
function task01() {
  $logfile2    = 'file.txt';
  $fp = @fopen($logfile2,'a+') or die('ERROR: Can\'t write to the log file ('.$logfile.'), please make sure this file exists and is CHMOD to 666 (rw-rw-rw-)!');
 
 flock($fp, LOCK_EX);
  // fputs($fp, $_POST[name]);
  //  fputs($fp, $_POST[nachricht]);
    fwrite($fp, "\r\n" );
    fwrite($fp, "_______________________\r\n" );
    fwrite($fp, "$datum\r\n");
    fwrite($fp, "Number of lines: $numLines\r\n");
    fwrite($fp, "Message envoye a $tag, $zeit der $datum.\r\n");
    fwrite($fp, "$_POST[name] \r\n");
    fwrite($fp, "$_POST[nachricht] \r\n");
    flock($fp, LOCK_UN);
    fclose($fp);
 }

?>
 
 <? // ### click button01 ?>
<form action="../ausgabe.php" method="post" name="textmail">
<table border=0 cellspacing=2 cellpadding=0 width=100%>
<tr><td nowrap align=right>Your name:</td><td>&nbsp;</td>
<td width=100%><input type="text" name="name" size=50 maxlength=120 value="<?php echo $from_name; ?>"></td></tr>
<tr><td nowrap align=right>Your E-Mail adress:</td><td></td>
<td width=100%><input type="text" name="email" size=50 maxlength=120 value="<?php echo $from_mail; ?>"></td></tr>
<tr><td nowrap align=right>Subject:</td><td></td>
<td width=100%><input type="text" name="betreff" size=50 maxlength=120 value="<?php echo $mail_subject; ?>"></td></tr>
<tr><td nowrap align=right valign=top>Content:<br><i>(max. 1.000 chars)</i></td><td></td>
<td width=100%><textarea cols="40" rows="10" name="nachricht"><?php echo $mail_text; ?></textarea></td></tr>
<tr><td></td><td></td><td>
<input type="hidden" value="1" name="s">
<input type="submit" value="Send your message." name="submit">
</td></tr>
</table>
</form>
 
 
 <? // ### click button02 ?>
<form action="task01" method="post" name="textmail">
    <input type="submit" value="Click here." name="task01">
</form>




</body>
</html>


frieza 04-21-2009 10:28 PM

only problem is by the time you click on the link all the php is done executing,that being said it would be easier said then done

Robhogg 04-22-2009 05:19 AM

Quote:

Originally Posted by frenchn00b (Post 3516837)
I have a task01 function. I would like that the click on the button, does not refrest hte page. I does not work... :( :(

PHP can't function without refreshing the page, as it runs on the server, not the client machine. However, with Ajax, Javascript is used to send a request to a script on the server, then process the response. It is usually used to update part of a page without having to refresh the whole page, but could probably achieve what you're wanting here.

aspire1 04-22-2009 05:42 AM

Perhaps they mean they don't want the form fields reset. If so sticky form using php?

frenchn00b 04-22-2009 07:03 AM

Quote:

Originally Posted by aspire1 (Post 3517147)
Perhaps they mean they don't want the form fields reset. If so sticky form using php?

I just wanted at the click of the button, one single part fo the page is changed. Damn after PHP, seems that I got to learn java now :(

Robhogg 04-23-2009 04:11 AM

Quote:

Originally Posted by frenchn00b (Post 3517200)
I just wanted at the click of the button, one single part fo the page is changed. Damn after PHP, seems that I got to learn java now :(

Not necessarily, or at least not too much. There are toolkits such as Sajax that allows you to script in PHP, with the Ajax/JavaScript code created automatically.

jlinkels 04-23-2009 04:57 PM

It is not quite necessary to learn Java. PHP + Ajax can create
'non refreshing' pages updates

I 'ajaxified' pages developed in PHP without many changes. There are some documents around on the internet which suggest to refresh each and every DOM element in your page leaning heavily on Javascript. Not so. Be sure to read all pages about Ajax on the web site below.

http://www.w3schools.com/PHP/php_ajax_database.asp

jlinkels


All times are GMT -5. The time now is 08:03 PM.