Joe Mo
Joe Mo

Reputation: 135

display a javascript message before proceeding

I have this code that is to fetch a particular item in the database after the user presses a button. If the item is found I would like to display a confirm message via javascript and I don't know how to show it.

After obtaining the item from the database

if(null!=mysqli_fetch_array($res)))
{
    echo ""; // how can I call the javascript code here ?
}

and the following confirm box

  <script type="text/javascript">
    function onConfirm()
    {
        return confirm("Display this item ?");        
    }
  </script>

Upvotes: 0

Views: 406

Answers (2)

Adam
Adam

Reputation: 1694

Use jquery and ajax to get the value from the database:

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
$.get("ajax.php", function(data){

if(data != "0"){
if(confirm("Display this item ?"))
{

//do something

}
}

});

ajax.php:

//other db code here

if(null!=mysqli_fetch_array($res)))
{
echo "1"; 
}
else{
echo "0";
}

Upvotes: 1

Starx
Starx

Reputation: 78991

There are number of ways you can solve this. A good way is to:

  • Send an ajax request to a page
  • The page does the mysql stuff and returns(echo in case of PHP) the message
  • On the callback function use simple way such as alert() to show the message.

Upvotes: 0

Related Questions