Вопрос

I am propably being a complete idiot and not seeing the big picture here. I am trying to write a little ajax thingie that would pull information from a database but i cant seem to get the thing going ... please tell me what i am doing wrong.

I put the code together by following a couple of online tutorials and i am no ajax genius. my ajax skills could propably start the next world war.

Here is my code. please feel free to tell me im an idiot if i missed something small.

     <html>
<head>
    <script>
        function showDetail(str) {
            if (str=="") {
                document.getElementById("txtHint").innerHTML="";
                return;
            }
            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","process.php?q="+str,true);
            xmlhttp.send();
        }
    </script>
</head>
<body>

<form>
    Wallet Number : <input type="text" name="wallet01" id="wallet01"  />
    <br />
    <input type="submit" id="submit" name="submit" onsubmit="showDetail(wallet01)">


</form>
<br>
<div id="txtHint"><b>Kit info will be listed here.</b></div>

</body>
</html> 

and here is my php

<?php
$q = $_GET['q'];

$con = mysqli_connect('10.1.75.131','markdv','qwerty123','MTNAutomation');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"MTNAutomation");
$sql="SELECT serialNumber FROM Kit_T WHERE serialNumber = '$q'";
$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Wallet Number</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['serialNumber'] . "</td>";

    echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?> 
Это было полезно?

Решение

There is an error in your HTML. You put 'onsubmit="showDetail(wallet01)"', but 'wallet01' is not a javascript variable.

I would suggest changing it to this:

<form onsubmit="return showDetail()">
    Wallet Number : <input type="text" name="wallet01" id="wallet01"  />
    <br />
    <input type="submit" id="submit" name="submit">
</form>

I added the "onsubmit" in the form, using the function with "return", to prevent the default submit from form. But remember to always return false in the function.

Than in the function you can get the value for the field.

    function showDetail() {
        var str = document.getElementById('wallet01').value;

        if (str=="") {
            document.getElementById("txtHint").innerHTML="";
            return false;
        }

        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","process.php?q="+str,true);
        xmlhttp.send();

        return false;
    }

Or you can use the ajax from jQuery, it's easier. See an example here

    function showDetail() {
        var str = $('#wallet01').val();

        if (str=="") {
            document.getElementById("txtHint").innerHTML="";
            return false;
        }

        var jqxhr = $.ajax( {
                url: "example.php",
                type: "GET", 
                data: {q: str}
            }).done(function(data, textStatus, jqXHR ) {
                alert( "success" );
            }).fail(function(jqXHR, textStatus, errorThrown) {
                alert( "error" );
            }).always(function( ) {
                alert( "complete" );
            });

        return false;
    }

See the JQuery Ajax docs You also have the function "beforeSend" to start a gif "loading".

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top