在选择下拉列表时获取文本框中的值

时间:2018-03-07 07:05:58

标签: php mysql ajax

我是PHP的初学者。在这里,我希望在下拉列表中选择时在文本字段中获得结果。问题是在文本字段中获取值。请帮我。一世 我搜索了很多解决方案。我创建了另一个代码,用于在多个下拉列表中获取值。提前致谢。这是我试过的代码

的Javascript

<script>

function getXMLHTTP() { 
        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 getCurrencyCode(strURL)
{       
    var req = getXMLHTTP();     
    if (req) 
    {
        //function to be called when state is changed
        req.onreadystatechange = function()
        {
            //when state is completed i.e 4
            if (req.readyState == 4) 
            {           
                // only if http status is "OK"
                if (req.status == 200)
                {                       
                    document.getElementById('cur_code').value=req.responseText;                     
                } 
                else 
                {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
         }          
         req.open("GET", strURL, true);
         req.send(null);
    }           
}
</script>

HTML:

<form style="text-align:center" method="post" action="" name="form1">
<p style="color:#000099 ">When you change the dropdown list, the respective currency code of the country will be displayed in the textbox which is fetched from PHP using Ajax. </p>
<p>Country : <select name="country" onChange="getCurrencyCode('find_ccode.php?country='+this.value)">
    <option value="">Select Country</option>
    <?php
    $query =mysql_query("SELECT distinct course FROM tblcourse");
while($country=mysql_fetch_array($query)) {
?>
<option value="<?php echo $country["course"]; ?>"><?php echo $country["course"]; ?></option>
<?php
}
?>  
        </select><br/><br/>
 Currency :   <input type="text" name="cur_code" id="cur_code" ></p>
</form>

findccode.php:

<?php
mysql_connect("localhost","root","");
mysql_select_db("internal");
if(!empty($_POST["course"])) {
    $query ="SELECT * FROM tblcourse WHERE course = '" . $_POST["course"] . "' limit 1";
mysql_query($query);    
?>

<?php
    foreach($results as $state) {
?>

    <?php echo $state["sem"]; ?>
<?php
    }
}
?>

2 个答案:

答案 0 :(得分:0)

请在选择字段名称

中使用void init(Contact* contacts) { contacts = (Contact*)malloc(sizeof(Contact) * 5); printf("%d\n", sizeof(Contact) * 5); //prints 200 (char[20] name + char[20] phoneNum) printf("%d\n", sizeof(contacts)); // prints 4 <-???????????? } 尝试查询
,

没有$query =mysql_query("SELECT distinct, course FROM tblcourse"); 查询只会选择,字段而不是distinct

答案 1 :(得分:0)

您是通过course从XHR发送您的价值,但在您的PHP中,您使用的是GET

POST

或者您必须使用XHR发布您的值。

注意:请查看How can I prevent SQL injection in PHP?以确保您的查询安全。

请注意,if(!empty($_GET["course"])) { $query ="SELECT * FROM tblcourse WHERE course = '" . $_GET["course"] . "' limit 1"; 函数已弃用。请查看MySQLiPDO

相关问题