简体   繁体   中英

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>

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";
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM