Skip to content
Advertisement

Show alert from php action page after fetching the records from the database

I have been trying to show alert message from php action page after the form records are fetched from database. I have 2 php pages. In one.php I am making a ajax call to action page two.php. Till now I am able to make successful ajax call from one.php to two.php and I am able to receive all the parameters passed from one.php to two.php. But I would like to show an alert message in one.php after the records are fetched into the database in the file two.php.

two.php

$query = "SELECT status FROM dash_apparel_reprint WHERE piece_number='" . $piece_info[0] . "' ";
    $result = mysql_query($query);

    if(($row = mysql_fetch_array($result)) != false) {
       $status = $row[0];
    }

    if($status == "New" || $status == "Out of Stock"){
        echo('<script type="text/javascript">window.alert("fetching records done"); window.location.href="one.php"</script>');
        exit();
    }

I am using POST method for both one.php and two.php. referring to this Display alert box upon form submission Any kind of advice or suggestion will definitely help me learn this concept

Advertisement

Answer

If you want to use an alert you need to set it in the success part of your ajax call. See below:

$.ajax({ url: 'two.php',
             data: {var1: var1value} //Pass your variables here
             type: 'post',
             success: function(output) {  //This is where you receive the result from two.php
                  //Do whatever you want with the data stored in output
                  alert('It worked!'); //Alert whatever you need here           
             },
             error: function() {
                  alert('That did not work...'); //Handle errors here
             }
        });

In the success or error functions you can update other DOM elements in your page as well, which may be more elegant than just an alert.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement