【问题标题】:how to get value from another page如何从另一个页面获取价值
【发布时间】:2014-09-29 10:09:03
【问题描述】:

我应该制作一个动态选择框,其中医生姓名的选择取决于在另一个选择框中选择哪个,即专业。

这里是 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>

仍然在同一个文件中,这是我的 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);
    }

}

我有一个单独的请求文件,其中包含 this 名为 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

       } ?>

我传递了 javascript 变量并使用此代码在另一个页面 getSpec.php 中将其转换为 php,但现在的问题是我无法从选定的 &lt;select name="docName&gt; 。它在另一个页面名称 getSpec.php 中有没有办法我可以得到它?

【问题讨论】:

  • 使用 POST 方法将选择字段包装在表单中,然后您可以通过 $var = $_POST['docName']; 在新窗口中访问它表单提交后。
  • 嘿!实际上,它已经以 post 作为方法包装在一个表单中。并且这里没有发生重定向,getSpec.php 仅执行其功能,然后将其内容返回到&lt;div = "getDoc"&gt;。有任何想法吗? :)

标签: javascript php jquery html url


【解决方案1】:

你的浏览器是什么? 操作 DOM 时不能依赖 innerHtml。它在每个浏览器上的工作方式不同, IE 处理 XMLHTTPRESPONSE 的方式也不同于基于 webkit 的浏览器。 我认为它是“完成”或“完成”而不是 4。

你用 jquery 标记了你的问题,为什么不使用这个库来解决你的问题。 $.get 和 $.ajax 的成功处理程序和 $("some css selector").html('your new html') 都是很好的工具。

编辑:我正在按要求添加更多详细信息。
正如您所问的,您可以使用下面的代码来获取 select & getDoc 的 innerHtml:('#' id 表示 id 和 '[sth=?]' 表示属性相等)

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

专门用于您的 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.
    });
}

好的,简短的代码:
$ 是 jQuery 的操作符,你可以使用 $.someFunction() 来使用静态函数。 jQuery 的元素选择器使用“CSS Selector”字符串,语法为:$('CSS_SELECTOR')
通过使用它,您可以选择页面的所有“标签”标签:$('label') 或 type='text' $('input[type="text"]') 或更多的“输入”标签。见API Doc selector Page

使用 css_selector 选择元素后,您可以运行特定于元素的函数,例如 html()。 jQuery 从不返回 void,而是返回元素 Object,因此我们可以在变量 / DOM 元素上使用每个操作函数,而无需使用 ';'。我将这个概念用于jqxhr 变量(内部:getSpecialty(strURL),jqxhr 是方法$.get 的返回对象),其中我调用了.done(...).fail(...).always(...)jqxhr

【讨论】:

  • 我正在使用 chrome。是的,但我仍然在缓慢地学习 ajax 和 java,因为我发现它们的语法令人困惑。另外,您能否推荐我或至少告诉我是否有可能从中获得价值的方法?只是通过使用 innerHtml 作为投影? :( 有什么选择吗?
  • 因为您的客户端可能不会使用 chrome,(可能是 Firefox、IE、...)您最好使用 jQuery 之类的跨浏览器插件。您可以使用 html() 来获取对象的 innerhtml。参考见:jquery Documentation
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多