0

Hope your are fine!

I'm would like to execute a request (SQL) like this one:

SELECT Name FROM course WHERE IdSectionFK = '.$idSectionFK.';

I have the list of sections:

while($row = $selectAllSection->fetch(PDO::FETCH_OBJ)){
   echo "<option value=".$row->IdSection.">".$row->Name."</option>";
}

And I would like to display the data only for the selected value. I tried something like this to get the value of the list:

<script>

function displayVals() {

        var idSection = $("#sectionListe").val();

        $.post('index.php', { 'idSection': idSection },function (){
          alert("success");
        })

        .success(function() { alert("second success"); })
        .error(function() { alert("error"); })
        .complete(function() { alert("complete"); });
    }                                    

    $("select").change(displayVals);
    displayVals();

</script>

So, the variable "idSection" is equale to the PHP variable "idSectionFK" in my SQL request. But how can I execute the right SQL request ?

Thank you so much for your help!

Lapinou.

1

1 Answer 1

1

AJAX is the solution.

JS code:

function displayVals() {
    var idSection= $("#sectionListe").val();
    $("p").html(idSection);
    $.post('/url/to/php/file', { 'idSection': idSection }, function (response) {
        // do something with the response here
        // e.g: $('select').append(response);
        console.log(response);
    });
}                                    

$("select").change(displayVals);
displayVals();

PHP code to process the received data:

$idSectionFK = 0;

if (isset($_POST['idSection'])) {
    // get the ID from ajax, run SQL here
    $idSectionFK = intval($_POST['idSection']);

    $query = "SELECT Name FROM course WHERE IdSectionFK = '.$idSectionFK.';";

    // .... your code ...
}

Just to give you the basic idea.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your help ;) But it doesn't work. I have no POST response... I don't understand why :/
Do you really need POST response? I haven't put that in the code before. Answer updated, could you check now?
No I don't. I mean, when I check if(isset($_POST['idSection'])), it always return false... Like if $.post doesn't work...
I edited my script to check it. It seems ok but $_POST['idSection'] is not set :/
Are you sure that index.php is the file contains the SQL query? Also any error in the console (using Firebug / Chrome dev tool)?
|

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.