admin管理员组

文章数量:1323529

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.

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.

Share Improve this question edited Sep 12, 2013 at 0:07 nathanchere 8,09815 gold badges67 silver badges86 bronze badges asked Sep 11, 2013 at 23:48 user2770706user2770706 111 silver badge2 bronze badges 4
  • Please include the javascript you tried and what went wrong. – showdev Commented Sep 11, 2013 at 23:50
  • 5 php server side, it does not know what a window is – user557846 Commented Sep 11, 2013 at 23:50
  • 1 Please tell me that you are NOT calling your database query every 5 seconds! You should be using long-polling or some sort of socket to do this. Also please don't use mysql_* as it is depreciated. Please use mysqli_* or PDO instead. – Evan Stoddard Commented Sep 11, 2013 at 23:59
  • I do not remend using ajax to make a chat programme. Try web socket instead. And for your popout question, I think this may help you. thomasgrauer./popeasy – Jonathan Commented Sep 12, 2013 at 4:34
Add a ment  | 

3 Answers 3

Reset to default 4

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.');

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.

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./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis./ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery./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();
    }
});

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.');.

本文标签: javascriptOpen a window popup using phpStack Overflow