简体   繁体   English

如何从另一个页面获得价值

[英]how to get value from another page

i was supposed to make a dynamic select box where the choices of the doctors name depends on which is selected in another select box namely specialty. 我应该做一个动态选择框,其中医生姓名的选择取决于在另一个选择框即专业中选择的名字。

heres the html 继承人的HTML

    <select name="docSpec" id="docSpec" onChange="getSpecialty('getSpec.php?spec='+this.value)">

            <option>pick a specialization</option>
            <option value="General">General</option>
            <option value="pediatrics">pediatrics</option>
            <option value="Physician">Physician</option>
            <option value="Cardiologist">Cardiologist</option>
            <option value="Pulmonary">Pulmonary</option>

    </select>   




        <div id="getDoc"> <!-- the contents from getSpec.php are displayed in this div! -->
            <select>
                <option>select doctor</option>
            </select>
        </div>

still in the same file, heres my javascript.. 仍然在同一文件中,这是我的JavaScript。

    function getXMLHTTP() { //fuction 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 getSpecialty(strURL) {     

    var req = getXMLHTTP();

    if (req) {

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

}

i have a separate file for the request which contains this named getSpec.php 我有一个单独的文件包含这个名为getSpec.php的请求

<?php $spec=$_REQUEST['spec']; // i converted javascript variable to php by passing it to url
    require_once('dbcon.php');
    $query="select doctorName from doctors where specialty= '$spec'"; // this is why i passed it with jquery
    $result=mysqli_query($conn,$query);
?>
<select name="doctor">
    <option>Select doctor</option>
    <?php 

        while( $row = mysqli_fetch_row($result)) { 

            echo "<option value='$row[0]'>$row[0]</option>"; // for contents of the dropdown

       } ?>

i passed the javascript variable and converted it to php in another page getSpec.php with this code but the problem now is i cant get the value from the selected of <select name="docName> . its in another page name getSpec.php is there a way i can get it? 我通过此代码传递了javascript变量并将其转换为另一页getSpec.php中的 php,但现在的问题是我无法从所选的<select name="docName>获取值。有办法可以得到吗?

What is your browser? 您的浏览器是什么? you can't rely on innerHtml when manipulating DOM. 您在处理DOM时不能依赖innerHtml。 it works differently on each browser, also IE handles XMLHTTPRESPONSE differently than webkit based browsers. 它在每种浏览器上的工作方式都不同,IE与基于Webkit的浏览器对XMLHTTPRESPONSE的处理方式也不同。 I think it is "complete" or "completed" instead of 4. 我认为它是“完成”或“完成”而不是4。

you tagged your question with jquery, why not use this library for your problem. 您用jquery标记了问题,为什么不使用此库解决问题。 success handler of $.get and $.ajax and $("some css selector").html('your new html') are good tools. $ .get和$ .ajax和$(“ some css selector”)。html('your new html')的成功处理程序是很好的工具。

Edit: I'm adding more details as were requested. 编辑:我正在按要求添加更多详细信息。
As you asked you can use code below to get innerHtml of select & getDoc: ('#' id for id and '[sth=?]' for equality of attributes) 如您所要求的,您可以使用下面的代码来获取select和getDoc的innerHtml :(“#” id代表id,“ [sth =?]”代表属性相等)

var old_get_doc_innerHtml = $('#getDoc').html();
var old_select_innerHtml = $('select[name="doctor"]').html();

specifically for your getSpecialty(strURL) code use: 专为您的getSpecialty(strURL)代码使用:

function getSpecialty(strURL) {
    var jqxhr = $.get(strURL, function (data) {
        // runs only if http fetch has succeeded.
        $("#getDoc").html(data);
        alert("Load was performed.");
    })
    .done(function () {
        // another success implementation type
    })
    .fail(function () {
        // runs only if http fetch has failed.
    })
    .always(function () {
        // runs always both in fail and success.
    });
}

Ok, a little code brief: 好的,简短的代码简介:
$ is the jQuery's operator, you can use static functions using $.someFunction() . $是jQuery的运算符,可以使用$.someFunction()使用静态函数。 jQuery's element selector uses a 'CSS Selector' String and the syntax is: $('CSS_SELECTOR') . jQuery的元素选择器使用“ CSS选择器”字符串,语法为: $('CSS_SELECTOR')
By using it you can select all 'label' tags of your page: $('label') or 'input' tags having type='text' $('input[type="text"]') or more. 通过使用它,您可以选择页面上的所有“标签”标签: $('label')或具有type ='text' $('input[type="text"]')或更多类型的'input'标签。 see API Doc selector Page 请参阅API文档选择器页面

After selecting your element(s) with css_selector, you can run element specific functions like html() . 使用css_selector选择元素后,您可以运行特定于元素的函数,例如html() jQuery never returns void instead it returns element Object so we can use every manipulation function on our variables / DOM elements without using ';'. jQuery永远不会返回void,而是返回元素Object,因此我们可以在变量/ DOM元素上使用每个操作函数,而无需使用';'。 I used this concept for the jqxhr variable (inside: getSpecialty(strURL), jqxhr was return object of method $.get), where I called .done(...) , .fail(...) , .always(...) of jqxhr . 我将这个概念用于jqxhr变量(内部:getSpecialty(strURL),jqxhr是方法$ .get的返回对象),在这里我称为.done(...) .fail(...).always(...)jqxhr

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

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