Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    London
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Alert pop up using AJAX

    Hi guys,

    Does anyone know how to make an alert pop up from an PHP script using AJAX?

    I have a price calculator that sets many choices and if two choices don't match I want a window alert with an error message.

    I can make so an error text is shown printed on the page together with the result but is it possible to have a window.alert('Wrong choice ') from inside the php script or from the .js file?

    Appreciates all help.

  • #2
    New Coder
    Join Date
    Nov 2003
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi - did you find a way to do this?

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    can you provide your script?

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I can't believe that you guys all have the same problem, and I don't even get what it's about. Have the PHP script generate an alert? Why would you want to do that? That's the most user-unfriendly type of server-side validation I can think of.

    And even if you really want to do it, although it's certainly a silly thing to do, what's the problem, really? Just let your PHP script write out the Javascript; there's nothing to it.

    Edit: I just reread the original post and saw that this is about AJAX, and I get it even less now. You can do whatever you want in the AJAX callback, so why not just alert something, if you feel that's what you must do?
    Last edited by venegal; 02-16-2011 at 02:28 AM.

  • #5
    New Coder
    Join Date
    Nov 2003
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is code called by the AJAX submit. The PHP messages display fine.

    There are several things this script will need to do:

    1. Alert if a reference number has not been entered
    2. Alert if the reference number does not exist on the database
    3. Provide an option to choose between SALES or RENTAL details if this property is for sale and for rent
    4. Open the PHP within the same window for the selected choice.
    PHP Code:

        $idRefNo 
    $_POST['idRefNo'];

        if (!isset(
    $_POST['submit'])) { 


            require_once(
    '../Connections/TestWebServer.php');

        
            
    //GET PROPERTY DETAILS
            
    $query "SELECT  DISTINCT * FROM property WHERE property.Title = '".$idRefNo."' AND suspend != 'Yes'";
            
    $result = @mysql_query ($query);                
            if (
    $result) { // If the query runs ok

                
    if ($result != "") {
                    while (
    $row mysql_fetch_array ($resultMYSQL_ASSOC)) { 
                        
                        if (
    $row["BaseRental"] > AND $row["Saleprice"] > 0) {
                            echo 
    'This property is for RENT and for SALE <br/>';
                        } else if (
    $row["BaseRental"] > 0) {
                        echo 
    'This property is for RENT only <br/>';
                            echo 
    '<script type="text/javascript">alert(\'Hello\');</script>';
                            
                        } else if (
    $row["Saleprice"] > 0) {
                            echo 
    'This property is for SALE only <br/>';
                        } else {
                            echo 
    'DOH! What is going on here!!! <br/>';
                        }
                    }

                    } else { 
    // This line is actioned if the query fails
                        
    $message '<p>We are unable to display the event information due to a system error.</p><p>' mysql_error() . '</p>';
                    }
                } else {
                    echo 
    'This property does not exist';
                }

            
    // mysql_free_result ($result); // Free up the resources

            // echo $message; // Display any error messages
        

    Last edited by skinnybloke; 02-16-2011 at 10:59 AM.

  • #6
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    PHP Code:
    echo '<script type="text/javascript">alert(\'Hello\');</script>'
    This would only work if the page would be normally loaded by the browser, not when it's an AJAX response that's manually added to the DOM.

    The right thing to do in this case would be to not return HTML but a JSON object that contains the response as well as additional data that you can use on the client side in order to figure out whether to alert something, and what to alert.

  • #7
    New Coder
    Join Date
    Nov 2003
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply - I'll give that a go.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •