37793

display a javascript message before proceeding

Question:

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>

Answer1:

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

Answer2:

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

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

Recommend

  • unserialize multiple array and echo by passing more than 2 parameter to arraycombine method
  • php file download headers
  • PHP MySQL generating unique random number
  • How to resolve permission denied maybe missing internet permission?
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Keep this build forever option - Jenkins
  • Set the selected item in dropdownlist in MVC3
  • D3 nodes and links from JSON with nested arrays of children
  • DomPDF {PAGE_NUM} not on first page
  • Insert into database using onclick function
  • Javascript simulate pressing enter in input box
  • Is calc() supported in html email?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Submit form in a displaytag pagination
  • How to show dropdown in excel using jrxml (jasper api)?
  • ActionScript 2 vs ActionScript 3 performance
  • Importing jscolor library in angular 2
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • To display the title for the current loaction in map in iphone
  • Traverse Array and Display in markup
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?