简体   繁体   English

这个javascript函数到底能做什么? 以及如何捕获select中的值以将其重新插入数据库?

[英]what does this javascript function exactly do? and how do i catch the values from select to insert it back into the database?

I was looking around for a ajax script which could populate triple drop down list using PHP. 我正在寻找一个可以使用PHP填充三重下拉列表的Ajax脚本。 And I came across this piece of code although the code works pretty well I would like to know what is actually happening behind the scene, as I am a newbie to AJAX or JavaScript I am unable to understand what does the below function exactly do. 我碰到了这段代码,尽管代码运行得很好,但我想知道幕后实际上发生了什么,因为我是AJAX或JavaScript的新手,所以我无法理解以下功能的确切作用。 Here is the code: 这是代码:

<script language="javascript" type="text/javascript">

function getXMLHTTP() { //function to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }

    function getState(countryId) {      

        var strURL="findState.php?country="+countryId;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('statediv').innerHTML=req.responseText;                     
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }       
    }
    function getCity(stateId) {     
        var strURL="findCity.php?state="+stateId;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('citydiv').innerHTML=req.responseText;                      
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }

    }
</script>

and here is the code from finsState.php 这是finsState.php中的代码

<?php
$countryId=intval($_GET['country']);
$host = "localhost";
$username = "username";
$password = "password";
$database = "test";
$connection = mysql_connect($host,$username,$password) or die(mysql_error());
$database = mysql_select_db($database) or die(mysql_error());
$query = "SELECT * FROM states WHERE country_id = ".$countryId;
$result = mysql_query($query) or die(mysql_error());
?>
<select name="states" onchange="getCity(this.value)">
<option>Select State</option>
<?php while($row = mysql_fetch_array($result)) { ?>
    <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>

and this is findCity.php 这是findCity.php

<?php
$stateId=intval($_GET['state']);
$host = "localhost";
$username = "username";
$password = "password";
$database = "test";
$connection = mysql_connect($host,$username,$password) or die(mysql_error());
$database = mysql_select_db($database) or die(mysql_error());
$query = "SELECT * FROM cities WHERE state_id = ".$stateId;
$result = mysql_query($query) or die(mysql_error());
?>
<select name="city">
<option>Select City</option>
<?php while($row = mysql_fetch_array($result)) { ?>
    <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?> </option>
<?php } ?>
</select>

My questions : 我的问题:

a) Can anyone please summarize the first, second and third javascript function and how does it select all the values from findState.php and findCity.php a)任何人都可以总结一下第一个,第二个和第三个javascript函数以及它如何从findState.php和findCity.php中选择所有值

b) If I was to catch the value of the stateId and CityId the user selected how do i do it. b)如果我要捕获stateId和CityId的值,则用户选择了我该怎么做。 as because the list is populated from JavaScript I am unable to catch the value from PHP ($_POST['state']). 因为列表是从JavaScript填充的,所以我无法从PHP($ _POST ['state'])捕获值。

The first function is attempting to open up and XML HTTP request, allowing the clients-side scripts to interact with server-size scripts. 第一个功能是尝试打开XML HTTP请求,从而允许客户端脚本与服务器大小的脚本进行交互。 Since various browsers handle that differently, it try s to use the standard method before attempting a modern IE implementation, an older IE implementation and finally giving up. 由于各种浏览器的处理方式不同,因此它会先try使用标准方法,再尝试使用现代IE实现,较旧的IE实现并最终放弃。

The second two do roughly the same thing. 后两个做大致相同的事情。 They define the script they wish to interact with an use the first function to connect. 他们使用第一个函数进行连接来定义希望与之交互的脚本。 If the connection was successful, it sends over the information using an HTTP GET request (traditionally GET is used for getting information, POST is used for setting it). 如果连接成功,它将使用HTTP GET请求发送信息(传统上,GET用于获取信息,POST用于设置信息)。 When information is sent using XmlHttpRequest, the onreadystatechange event fires at key points during the connection and gives access to the readyState (referring to how the stage of the request) and the status which is a standard server response (you're probably familiar with "404" meaning "file not found"). 使用XmlHttpRequest发送信息时, onreadystatechange事件会在连接过程中的关键点触发,并提供对readyState (指请求阶段的方式)的访问以及作为标准服务器响应的status (您可能熟悉“ 404”表示“找不到文件”)。 A "200" status means "everything is fine", so when that is received the script knows it can act on the information it was given. 状态为“ 200”表示“一切都很好”,因此脚本收到消息后就知道它可以对给出的信息进行操作。 Any other response will create a popup that tells the user what went wrong. 任何其他响应都将创建一个弹出窗口,告诉用户出了什么问题。

Without seeing the actual page this script interacts with, I don't know the best way to get stateId and CityId. 没有看到该脚本与之交互的实际页面,我不知道获取stateId和CityId的最佳方法。 At a guess, they will be the value of an input when a certain button is pressed. 猜测,它们将是按下某个按钮时的输入值。 If that's the case, something like the following piece of code would do it: 如果是这种情况,则可以像下面的代码这样执行:

document.getElementById('id-of-submit-button').onclick = function() {
    var stateId = document.getElementById('id-of-stateid-input').value,
        CityId  = document.getElementById('id of cityid-input').value;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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