douyuan9512 2012-10-16 14:30
浏览 6

AJAX,PHP DB查找

Currently I have a form and when the user is entering data, this code calls another page to check if the username is in use. This is the code from the main page that calls the DB Page(test.php)

<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
  { 
  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;
        if (xmlhttp.responseText.indexOf("green") != -1) {
            document.getElementById("submit").disabled = false;
        } else {
            document.getElementById("submit").disabled = true;
        }
    }
  }
xmlhttp.open("GET","test.php="+str,true);
xmlhttp.send();
}
</script>

I want to change this so when it calls test.php it passes a value to test.php based on what form field the user is entering into.

That way I can use test.php to perform diffident DB lookups depending on the form field the user is completing.

Can anyone help ? Thanks :)

  • 写回答

1条回答 默认 最新

  • doucheng3407 2012-10-16 14:49
    关注

    An example with the Javascript jQuery ajax function. This function can be called whe user click on a component with id="myButton":

    $(document).ready(function() {
        $('#myButton').click(function() {
    
            var myValue = $('#myInput').attr('value');
    
            var request = $.ajax({
                url: "test.php",
                type: "GET",
                data: {
                    myInput : myValue
                },
                dataType: "json"
            });
    
            request.done(function(msg) {
                alert( "Request done: " + msg);
            });
    
            request.fail(function(jqXHR, textStatus) {
                alert( "Request failed: " + textStatus );
            });
        });
    });
    

    With this example you can take the value from a texfield with id = "myInput" and pass it with a GET call to the test.php page.

    In the test.php page you can take the value simply with:

    $_GET['myInput'];
    

    Just one tip: your test.php must return a json type.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序