0

I want solution for reading a value from dropdown list and send it to the same page usng GET method. I think ajax and jquery will fulfill this.

if i select 'Inpatient' from dropdown list then it automatically available on same page.

<div id="div_for_patienttype">
Choose patient type:<select id="select_patient_id"name="patient_type">
                    <option >Select patient type</option>
                    <option value="InPatient">InPatient</option>
                    <option value="OutPatient">OutPatient</option>
                </select>
</div>

I coded this and working fine but one problem is when I selects patient type the page gets refreshed and value in dropdown is 'Select patient type'.I actually wants value of dropdown remains same i.e selected.

code:

 $(document).ready(function(){
 $("#select_patient_id").change(function(){
 var selected_patient=$(this).val();
 sendType(selected_patient);
 });
 });
 function sendType(type)
 {
 $.ajax({
 type:"GET",
 url:"dispres.php",
 data:({patientType:type}),
 success:function(success)
 {
    window.location.href="dispres.php?patientType="+type;
 }
 });
 }

And how can I optimize this jquery and ajax code?

3
  • 4
    we all do want a lot of things... but stackoverflow is not here, to get others to work for you for free, but to help you out on problems during the coding... so show us, what you have tried so far ... stackoverflow.com/help/how-to-ask Commented Mar 23, 2015 at 12:10
  • What is the purpose of making a GET request to the same page in the first place? Normally with an AJAX request you would expect to change just a small part of the page, perhaps displaying some additional information from the server relevant to the selection you have made. Commented Mar 24, 2015 at 13:01
  • I used it for this purpose: $patient_type=$_GET['patientType']; if($_GET['patientType']!='') { $where_clause = " where patient_type='$patient_type' "; } and what you are saying I didn't getting it. Commented Mar 24, 2015 at 13:12

3 Answers 3

3

Basic jQuery to read the drop-down value:

var patient_type = $('select[name="patient_type"]').val(); // value on submit

Basic get request:

$.get( "my_page.php", { patient_type : patient_type } );
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure what you actually want. I believe on change of dropdown selection you want to call a web method(service).

//Code has not been tested

$(document).ready(function(){
    $("select[name=patient_type]").change(function(){
        var selected_patient = $("select[name=patient_type]").val();
        $.ajax({
            type: "GET",
            url: "process-request.php?patient="+selected_patient
        }).done(function(data){
           //do something
        });
    });
});

or you can write

$(document).ready(function(){
        $("select[name=patient_type]").change(function(){
            var selected_patient = $("select[name=patient_type]").val();
            $.ajax({
                type: "GET",
                url: "process-request.php",
                data:{patient:selected_patient}
            }).done(function(data){
               //do something
            });
        });
    });

On PHP side based upon the framework you are using just fetch querystring value

Comments

0

Assuming you are trying to make a GET request with the option value as a parameter

$('select[name="patient_type"]').on('change', function(){

    var selected = $('select[name="patient_type"]').find(':selected').val();

    $.ajax({
        url: window.location.href + '?' + selected,
        type: 'GET'
        success:function(data) {
           handleData(data);
        }
    });        
}); 

function  handleData(data){

    // do something here with the data obtained from your get request

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.