Reputation: 11
I am developing a chat by my own using php, mysql and ajax.
I have a page that loads a ajax every 5 seconds. This ajax verify the database and check if there is a person that wants to chat with another that is already connected.
This works fine. My problem is, that when this php file called in ajax function returns that there is someone who wants to chat, I need to open a popup.
This is the code of the php file called by an ajax.
// $query = "SELECT * FROM c_solicitud WHERE PARA = ".$_GET['id_corredor'];
// $result = mysql_query($query);
// echo $query;
$solicitud = 0;
while($row = mysql_fetch_assoc($result))
{
$solicitud = 1; //Existen solicitudes para este corredor
$chat_de = $row['de'];
$chat_para = $row['para'];
$nombre_de = $row['nombre_de'];
$nombre_para = $row['nombre_para'];
$input_desde = $chat_de;
$input_para = $chat_para;
//NEED TO OPEN A POP UP HERE.
}
I need to open a window after this select.
I tried to do a function in the main page (where the ajax function is called. But this javascript doesn´t work.
It is basic, I do a query, and if this query throws some result, I want to open a pop up window.
Upvotes: 0
Views: 7594
Reputation: 364
I'd suggest looking into JSONP.
That way you can have your AJAX poll the server and the server decide when to open up your javascript function containing window.open('http://google.com');
.
Upvotes: 1
Reputation: 40096
You do not pop-up the window on the PHP side, you do that on the javascript (client/browser) side.
The PHP side just returns the results back to you, either in HTML, or in JSON, or ...
So your PHP will look something like this:
while($row = mysql_fetch_assoc($result))
{
$solicitud = 1; //Existen solicitudes para este corredor
$chat_de = $row['de'];
$chat_para = $row['para'];
$nombre_de = $row['nombre_de'];
$nombre_para = $row['nombre_para'];
$input_desde = $chat_de;
$input_para = $chat_para;
//NEED TO OPEN A POP UP HERE.
}
$return = 'Message from: ' . $chat_de;
echo $return;
Important: this data will be received in your AJAX success function, and no where else
For example, if your AJAX code block looks like this:
$.ajax({
type: "POST",
url: "my_php_processor_file.php",
data: 'user=johnadams',
success:function(phpData){
alert(phpData);
}
});
The data "Message from: etc etc" will be returned in the variable phpData
-- and because we alert that variable, an alert box will display with the data.
For a pop-up window, I suggest using jQueryUI's dialog
widget, as it is easy to work with and a good starting place.
You will need these lines in the <head>
tags of your document:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
You would need a DIV in your document where the injected HTML (returned by PHP) would be stored:
<div id="chatresponse"></div>
and then your revised jQuery/AJAX code would look like this:
$.ajax({
type: "POST",
url: "my_php_processor_file.php",
data: 'user=johnadams',
success:function(phpData){
$('#chatresponse').html(phpData);
$('#chatresponse').dialog();
}
});
Upvotes: 2
Reputation: 4775
PHP is a server side scripting language. You'll need to use JavaScript (which runs on the client side most of the time)
You're going to want to use something like window.open which should work a little like this:
window.open('http://google.com');
Bear in mind that most browsers these days have built in pop-up blockers and will block your attempts unless over-ridden by the user. And if all else fails, use modals!
Just to sum that up, Get PHP to return a value to the user, which the Javascript will then catch and launch the appropriate URL. This URL should be made to contain whatever parameters you'll need to tell the PHP who to open the chat session with.
Upvotes: 4