How to pop up an alert from a mysql result?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MozgafD1
    New Member
    • Jul 2015
    • 4

    How to pop up an alert from a mysql result?

    Hi, I am looking around this all day but cant find out a way.

    Basically I have a html page with Jquery function that send data to a php file to retrieve data from a database.

    Retrieving data to the html page works fine.

    What I am trying to do is run a comparison between the value that is fetched from database and a set limit.

    This is what I have so far.

    Html file

    Code:
     
    <html>
    <head>
    <style>
    table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    th, td {
        padding: 5px;
    }
    </style>
    <script>
    
    
    function check(str1) {
    	if (str1 == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","test.php?q="+str1,true);
            xmlhttp.send();
        }
    	}
    </script>
    </head>
    <body>
    <form name= "myForm">
      Core Arm number:</br>
    	<input type="number" id="Arm" name="Arm"  onchange= "check(this.value)" >
    	</br>
     </form>
    <br>
    </div>
    <div id="txtHint"><b>info will be listed here...</b></div>
    
    </body>
    </html>
    and PHP file.

    Code:
    <?php
    
    $q = intval($_GET['q']);
    echo $q."<br>";
    $servername = "localhost";
    $username = "Admin";
    $password = "29N7Lp8SA6yWUaGM";
    $dbname = "coredb";
    $tbname= "ArmTable";
    
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error()."<br>");
    }
    echo "Connected successfully <br>";
    mysqli_select_db($conn,$dbname);
    
    $row = mysqli_fetch_array(mysqli_query($conn, "SELECT SleCount FROM ".$tbname." WHERE CoreArm = '123456'"));
    // This is in the PHP file and sends a Javascript alert to the client
    
    echo '<script language="javascript">';
    echo 'alert("message successfully sent")';
    echo '</script>'; 
    
    	if ($row['SleCount']>=2)
    echo "Exeeded Count ".$row['SleCount']." Segregate arm for Sleeve change. <script type='text/javascript'> alert('segregate');</script> <br><br><br>";
    
    mysqli_close($conn);
    ?>
    Any help would be appreciated.

    Also if you know a better way let me know.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    If your variable 'xmlhttp.respon seText' contains some text, than you can create a piece of JavaScript to act on that.

    Currently the value of 'xmlhttp.respon seText' seems to be a piece of Javascriptcode (or an error message).

    Try adding 'window.alert(xm lhttp.responseT ext);' between line #30 and #31 in your HTML file, and look at the result....

    Comment

    • MozgafD1
      New Member
      • Jul 2015
      • 4

      #3
      Thanks for reply, but this gives an alert even if the Php file has nothing to echo. Is there a way so that only when the if statement is true then echo is sent and alert is opened?

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        Yes, learn JavaScript, and you know how to do it.... ;)

        For a quick start click: here

        Comment

        Working...