Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-11-2010, 10:42 AM   PM User | #1
Bernt_R
New to the CF scene

 
Join Date: Jun 2010
Location: London
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Bernt_R is an unknown quantity at this point
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.
Bernt_R is offline   Reply With Quote
Old 02-15-2011, 11:20 AM   PM User | #2
skinnybloke
New Coder

 
Join Date: Nov 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
skinnybloke is an unknown quantity at this point
Hi - did you find a way to do this?
skinnybloke is offline   Reply With Quote
Old 02-16-2011, 01:13 AM   PM User | #3
ankifreeze
New Coder

 
Join Date: Dec 2010
Posts: 62
Thanks: 2
Thanked 0 Times in 0 Posts
ankifreeze is an unknown quantity at this point
can you provide your script?
ankifreeze is offline   Reply With Quote
Old 02-16-2011, 01:24 AM   PM User | #4
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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 01:28 AM..
venegal is offline   Reply With Quote
Old 02-16-2011, 09:48 AM   PM User | #5
skinnybloke
New Coder

 
Join Date: Nov 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
skinnybloke is an unknown quantity at this point
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 09:59 AM..
skinnybloke is offline   Reply With Quote
Old 02-16-2011, 04:17 PM   PM User | #6
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Quote:
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.
venegal is offline   Reply With Quote
Old 02-16-2011, 04:38 PM   PM User | #7
skinnybloke
New Coder

 
Join Date: Nov 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
skinnybloke is an unknown quantity at this point
Thanks for the reply - I'll give that a go.
skinnybloke is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:22 PM.


Advertisement
Log in to turn off these ads.