php how to show message before process is end - php

PHP how to display a message "newsletter sent, can close page" before sending function is complete?Thank you.
function myfun(){
if(!ob_start("ob_gzhandler"))
ob_start();
$mail_id = $id;
include($_SERVER['DOCUMENT_ROOT']."/newsletter/".$template);// include a newsletter template
$htmltext = ob_get_contents();
ob_end_clean();
ob_end_flush();
ob_flush();
flush();
// close current session
if (session_id())
session_write_close();
sleep(2);
sendmail($htmltext); //sendo more then 2000 whith PHPmailer
}

hey You can simple echo the code of an alert script to display your message. As I am doing below:
echo '<script type="text/javascript">
alert("Your Message");
</script>';
You can even wrap this in a function provide your message as a parameter and call it any where you want to display your message.

Related

How to php send result to ajax immediately

There is a question bothering me, pls help!
I use jquery's ajax to call a php api.
$.ajax({
type:"POST",
dataType:"json",
data:{type:type,fid:fid,model:'star'},
url:'Recommend/Star/mark',
success:function (data) {
//do something
},
...
});
php code in Recommend/Star/mark method:
error_reporting(0);
set_time_limit(0);
ob_implicit_flush(true);
ob_end_clean();
ob_start ();
echo json_encode($boolMark);
ob_flush();
flush();
sleep(3);
$this->afterMark($uid, $fid);
I want php echo result to ajax immediately, but my codes do not work.
How to make php send result to ajax immediately, and continue to execute remaining code?
....
echo json_encode($boolMark . "<br>\n");
// get the size of the output
$size = ob_get_length();
// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
/******** background process starts here ********/
ignore_user_abort(true);
/******** background process ********/
set_time_limit(0); //no time limit
/******** Rest of your code starts here ********/
You also need to call these two functions after flush():
session_write_close();
fastcgi_finish_request();

php contact form using location.href

i have created a contact form using html and php to send the email, when the user fills the forms it just display blank screen
// Let's send the email.
if(!$error) {
//$messages="From: $email <br>";
$messages.="Company Name: $name <br>";
$messages.="Email: $email <br>";
$messages.="Message: $message <br>";
$emailto=$to;
$mail = mail($emailto,$subject,$messages,"from: $from <$Reply>\nReply-To: $Reply \nContent-type: text/html");
if($mail) {
$url = 'index.php?page=process&token=101';
echo "<script language=\"javascript\">
location.href=\"$url\";
</script>";
exit;
}
} else {
echo '<div class="error">'.$error.'</div>';
}
}
want if the user entered all the fields then should send them to index.php?page=process&token=101
Try this instead. There is no sense in echoing out a partial HTML page with a script tag in it when you can redirect them in PHP.
header("Location: $url");
Try this,
echo "<script>
window.location = '$url';
</script>";
I would suggest to go with header(), instead of java script.
In this case instead on JavaScript best practice is use the php function for redirection.
Try this
header("Location: index.php?page=process&token=101");

PHP/JQuery - Send data to a client from another client

I'm building a person to person chat, and want person A's page to refresh, loading new messages from Person B when Person B sends them. How would I send a message/data to Person A when Person B sends a message via PHP? I know I can check on Person A's page via Ajax, but constantly running a MySQL query would drastically bring down the server's speed. Any ideas?
EDIT: Using Server Sent Events, here's my script code:
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("update.php?user=<? echo $recip ?>");
source.onmessage = function(event) {
document.write(event.data);
if (event.data=="yes"){
window.location.href="/chat?with=<? echo $recip ?>";
}
};
} else {
document.getElementById('info-text').innerHTML="Hmm... looks like your browser doesn't support auto updating. Please refresh the page to check for new messages." //'
}
And here's my PHP code:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$user=$_GET['user'];
$sql=mysql_query("SELECT * FROM chatmsg WHERE sender='$myusername' AND receiver='$recip' OR sender='$recip' AND receiver='$myusername'");
$newrows=mysql_num_rows($sql);
if ($newrows!=$_SESSION['chat'.$user]) {
echo "data: yes";
flush();
}
else {
echo "data: no";
flush();
The problem is, nothing is happening when there's a new row in MySQL.
I found the solution, everyone. I still used the Server Sent Events, but made some changes and found the error. Here's the final working code:
PHP:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header("Access-Control-Allow-Origin: *");
$user=$_GET['user'];
$sql=mysql_query("SELECT * FROM chatmsg WHERE sender='$myusername' AND receiver='$user' OR sender='$user' AND receiver='$myusername'");
$newrows=mysql_num_rows($sql);
if ($newrows!==$_SESSION['chat'.$user]) {
$msg="yes";
}
else {
$msg="no";
}
echo "data: {$msg}\n\n";
flush();
sleep(10);
(sleep is to save server resources).
JS:
var source = new EventSource('update.php?user=<? echo $recip ?>');
source.onmessage = function(e) {
if (e.data=="yes") {
window.location.href="viewchat.php?viewer=<? echo $viewer ?>&recip=<? echo $recip ?>";
}
}

print progress as looping through php class functions

I am doing something with aOuth and some else's server at the moment and while the class I've created works it doesn't keep the HTML page posted of it's progress. For example say I have the following class:
class Something {
function a() {
sleep(2); echo "a()"; return TRUE;
}
function b() {
sleep(2); echo "b()"; return TRUE;
}
function c() {
sleep(2); echo "c()"; return TRUE;
}
}
Then I loop through the class in my HTML:
$something = new Something();
if($something->a()) {
if($something->b()) {
if($something->c()) {
echo "everything completed!!";
}
}
}
The page will render:
a()b()c()everything completed!!
6 seconds later. I want it to update as it goes (i.e. print a() when it's finished processing something->a(), print b() when it's finished processing something->b(), etc...)
Worked for me in Chrome 21, Firefox 15 and IE8:
<?php
header( 'Content-Type: text/html; charset=utf-8' );
class Something {
function a() {
echo "a()" . str_repeat(' ', 1024);
ob_flush(); flush();
sleep(2); return TRUE;
}
function b() {
echo "b()" . str_repeat(' ', 1024);
ob_flush(); flush();
sleep(2); return TRUE;
}
function c() {
echo "c()" . str_repeat(' ', 1024);
ob_flush(); flush();
sleep(2); return TRUE;
}
}
$something = new Something();
if($something->a()) {
if($something->b()) {
if($something->c()) {
echo "everything completed!!";
}
}
}
Explanation: we have to persuade into starting an output immediately...
PHP processor, with ob_flush-flush combo.
Apache (web-server), with sending Content-Type header right at the beginning of the script.
web-browsers, as some of them won't consider drawing a partial output unless it's big enough. The workaround is to append a sizable, but empty string (str_repeat(' ', 1024)) to the output.
Try flushing the output buffer each time you want the progress to be shown, with ob_flush function.
Each time you output some text, if output buffering is enabled, it is added to a buffer rather than being sent immediately to the client. When the request is done processing, the output is sent all at once.
If you flush the buffer, you force PHP to send the text it already have, without waiting for the request to complete.

Way to send functions as message in php's mail()

I am writing a PHP script that will send via a cron an email every night. In this script, I have multiple functions which output particular text. I am then trying to send the contents of those functions in the email. For some reason the email is going through fine, but the body of the content is showing up empty. If there's a better way to do this, by all means I'm open to it.
function function1() {
global $new;
echo "<p>";
while ($row = mysql_fetch_array($query)) $content = $row["COUNT(column1)"];
if ($content != 0) echo "output1";
else echo "output2";
echo "</p>";
}
$emailMessage = function1().function2().function3();
if ($_GET['version'] == "email") {
mail ($emailTo, $emailSubject, stripslashes($emailMessage));
}
else echo $emailMessage;
Obviously the code is obfuscated a bit, but the general outline is there.
echo sends the output to the standard out, it doesn't return it from the function. Try this.
ob_start()
// run function contents, including echo
var message = ob_get_clean();
return message;
This will capture what you echo into the buffer, prevent the buffer from being sent, and then reading the buffer into a variable. It will then empty the buffer ready for next time.

Categories