0

I am a beginner in PHP.Here, I wish to get result in text field while selecting in dropdownlist. The problem is to getting the value in text field. Please help me. I searched a lot for the solution.I created another code for getting values in multiple dropdown list. Thanks in advance. Here is the code which I tried

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
    }
}
?>
  • I would suggest to look into Jquery to get values and its function `$.ajax()` for AJAX calls, you would thank me for years. [Ajax tutorial for post and get](https://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get) – Daniyal Nasir Mar 07 '18 at 07:24

2 Answers2

0

Please try your query with , in select fields names

$query =mysql_query("SELECT distinct, course FROM tblcourse");

without , query will only select distinct field not course

PPL
  • 6,357
  • 1
  • 11
  • 30
0

You are sending your value by GET from XHR, but in your PHP you are using POST:

if(!empty($_GET["course"])) {
    $query ="SELECT * FROM tblcourse WHERE course = '" . $_GET["course"] . "' limit 1";

Or you have to POST your value using XHR.


NB: Please have a look to How can I prevent SQL injection in PHP? to secure your queries.

Please, also note that mysql_* function are deprecated. Take a look to MySQLi or PDO.

Syscall
  • 19,327
  • 10
  • 37
  • 52