Loading data from database with php and pure ajax in a textbox

前端 未结 2 1063
感动是毒
感动是毒 2021-01-17 08:08

i have one dropdown list which has data for \"subjects\" loaded from database. when i clicked on one subject what it should do is load related \"subject_id\" value inside te

相关标签:
2条回答
  • 2021-01-17 08:30

    Your subject_id is not displaying because you have not printed your book_id after fetching results from database in getbook.php

    After this $result=mysql_query($sql);

    Write echo $result['your_book_id_field_name'];

    0 讨论(0)
  • 2021-01-17 08:35

    The mistake was done at ajax part document.getElementById("bookid").innerHTML and must be replaced with document.getElementById().value since I had to put data to Html Element that cotain value i.e Textbox(as textbox contain value attribute).

    InnerHTML is used to manipulate the html elements that does not contain value, ** div,h1, ** etc. for details see below link.

    http://www.verious.com/qa/what-39-s-the-difference-between-document-get-element-by-id-quot-test-quot-value-and-document-get-element-by-id-quot-tes/

    ajax code

      function show_bookid(str)
    {
        var xmlhttp;
        if(str.length==0)
        {
            document.getElementById("bookid").value="";
            return;
        }
        if(window.XMLHttpRequest)
        {
            xmlhttp= new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXOjbject("Microsoft.XMLHttpRequest"); 
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("bookid").value=xmlhttp.responseText;
            }
        }
            xmlhttp.open("GET","getbook.php?q="+str,true);
            xmlhttp.send();
    
    }
    

    getbook.php

    <?php
    $b=$_GET['q'];
    include('includes/security.php');
     include('includes/dbconnect.php'); 
    $database=new MySQLDatabase();
    
    $sql="select * from tbl_bkcat where book_id='".$b."'";
    $result=mysql_query($sql);
    $row=mysql_fetch_array($result);
    echo $row['Book_id'];
    ?>
    

    addbook.php

                <form name="bookadd" action="" class="jNice" method="post">
                <fieldset>
    
                    <p><label>Subject</label>
                     <select name="subject" onChange="show_bookid(this.value);">
    
                     <?php
    
                        while($sel_rows=mysql_fetch_array($subresult))
                        {
                     ?>
                        <option value="<?php echo $sel_rows['book_id'];?>">
                        <?php echo $sel_rows['subject']?>
                        </option> 
                        <?php 
                            }
                        ?>
                    </select>
                    </p>
    
                    <p>
                    <label >Book ID</label>                 
                   <input type="text" id="bookid" name="book"/>
                    </p>
    
    0 讨论(0)
提交回复
热议问题