Have an account? Sign in
Login  Register  Facebook
PHP & Javascript problem
hi am happy to join with you all..i hope i can get a solution to my problem.
am new to php and javascript.. i have a combo box which displays values from mysql database. say for an example,

Name Number
sam 5678
john 7896
sam 4567
julie 3287

the combo box will list the name. when the user selects particular name the number related to it will be displayed in a textfield. i have created a program which successfully lists name as items and when submit button is clicked it will echo the number values inside textfield.
but problem is that i need that value to be displayed in a textfield when combobox item is selected not when submit button is clicked. am i clear.
here is my code..

 function PrintCombo(value)
             {
                alert(\"hi\");
                form.numbers.value =value;               
             }
    $dbname=\'callreader\';
    $db_user=\'root\';
    $db_pass=\'\';
    $host=\'localhost\';
        $conn = mysql_connect($host, $db_user, $db_pass);
        mysql_select_db($dbname);
        $query=\"select distinct Name from billing_numbers\";
        $result=mysql_query($query,$conn) or die(mysql_error());
                    if($result)
                    {
echo \"<select id=\'Name\' name=\'Name\' onchange=\'PrintCombo(\"\" . $no .\"\"\')\';>\";
                        while($row=mysql_fetch_assoc($result))
                        {
                            echo \'<option value=\"\'.$row[\'Name\'].\'\">\'.$row[\'Name\'].\'</option>\';
                        }
                        echo \'</select>\';
 }
<input type=\"submit\" value=\"submit\" />
 if(isset($_POST[\'Name\']))
            {
                $query=\"select Number from billing_numbers where Name=\'\".$_POST[\'Name\'].\"\'\";
                $result=mysql_query($query,$conn) or die(mysql_error());
                    echo \'<b>\';
                    echo \"Group Name:\" . $_POST[\'Name\'] . \"n<br/>\";
                    echo \"Numbers:n\";
                    echo \'<b/>\';
                    $arrayresult=array();
                if($result)
                {                    
                    while($row=mysql_fetch_assoc($result))
                    {
                        $arrayresult=$row;
                        $no=$row[\'Number\'];
                        echo $no.\"<br/>\";
                      // $var=\'PrintCombo(\"\'.$no.\'\")\';
                    }
                 // echo \"<pre>\". print_r($arrayresult,true).\"</pre>\";
                }
            }
<input type=\"text\" name=\"numbers\" value=\"<?php echo $no; ?>\"/>
Started: September 16, 2011 Latest Activity: September 16, 2011 php javascript
1 Answer
ok its easy you should do some thing like this
the java script function:
	function PrintCombo(value){
       document.getElementById('numbers').value = value;         
    }
the select input:
 echo "<select id='Name' name='Name' onchange='PrintCombo(this.options[this.selectedIndex].value)';>";
and the numbers input:
<input id="numbers" type="text" name="numbers" value="<?php echo $no; ?>"/>

Posted: MacOS
In: September 16, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer