0

This is my html code

<select name="course" id="course" onchange="valuesOfAll(this.value)">
   <option value=""> select </option>
   <option value="1"> Diploma in Computing</option>
</select>
<input name="course_credits" id="course_credits" type="text" />

and my database table is like this

courseId   courseName            courseCredits
  1        Diploma in Computing    5

So my request is, if i change the value in the 'select' the 'courseCredits' value should appear in the textbox. for this how can i write jquery code?

4

4 に答える 4

1

スクリプトからhtmlを分離することをお勧めしますので、変更したいと思います:

<select name="course" id="course" onchange="valuesOfAll(this.value)">

<select name="course" id="course" >

その後、私のスクリプトは次のようになります(最新のjqueryの参照を追加することを期待しています)

<script>
$(document).ready(function(){
//bind change event once DOM is ready
$('#course').change(function(){});
getResult($(this).val());

});


function getResult(selectedValue){
//call  ajax method to get data from database
 $.ajax({
            type: "POST",
            url: url,//this  should be replace by your server side method
            data: "{'value': '"+ selectedValue +"'}", //this is parameter name , make sure parameter name is sure as of your sever side method
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (Result) {
               alert(Result.d);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                  alert(errorThrown);
            }
        });
}
</script>
于 2013-03-22T06:58:14.010 に答える
1

"Ajax with Jquery" is what your are looking for. It will work like this:

  • the user chooses an option from the select box
  • you submit via Javascript the chosen option to a PHP script
  • the php script fetches the data from the database
  • the php script returns the result as json encoded data
  • there is a callback function in your javascript code. This js code will manipulate the HTML in whatever way you want, e.g. "add the option to your select box"

There are tons of tutorials on how to do Ajax requests in detail, e.g. http://openenergymonitor.org/emon/node/107

Check out one of those tutorials - eventually you will want to update your question so that it becomes a bit more specific? :-)

于 2013-03-22T06:59:51.783 に答える
0

use $.post.. (ajax or get)... i am using post here....go through the docs if you want to read more about jquery post..

javascript

 function valuesofAll(val){
     $.post('test.php',{data:val},function(result){
         $('#course_credits').val(result.coursecredit)
     }, "json"); //expect response as json..
 }

test.php

$data=$_POST['data']; //this is the value posted 
//make you query in db get result..
$courseCredits= $row; //db returned courseCreadit value
echo json_encode(array('coursecredit'=>$courseCreadits)); //send response as json
于 2013-03-22T06:56:14.607 に答える
0

this will helps you .....

<script>
function valuesOfAll(value)
{
var base_url="http://.../../hello.php";
var ip=new Object();
ip.course=value;
var inputParam=JSON.stringify(ip);
var module="getcourseCredits"
 $.ajax({
                     type: "POST",
                 url: base_url,
                 data: {coursevalue:inputParam,module :module}, 
                     dataType: "json",
                     success: function(msg)
                     {
                           $("#course_credits").val(msg.credit);   
                         }
         });
}

</script>
<body>
<select name="course" id="course" onchange="valuesOfAll(this.value)">
   <option value=""> select </option>
   <option value="1"> Diploma in Computing</option>
</select>
<input name="course_credits" id="course_credits" type="text" />
</body>

In your PHP file

<?php
if(isset($_POST['module']))
{
    if($_POST['module']=='getcourseCredits')
    {
           $val=json_decode($_POST['coursevalue'],true);
            $courseval=$val['course'];
           // do the connectivity and query here and finally echo the result as json format that is the response to the ajax call 
             .
             .
             .
             .

        }
}
?>
于 2013-03-22T07:12:50.270 に答える