1

I am creating a MySQL query that will be execute when user select options from more a dropdown lists.

What I want is, on selecting a dropdown list option a query related to that option should be automatically executed using ajax/javascript on the same page. As I have the both html and php code on the same page.

Earlier I was using form submit options for dropdown list but as the number of dropdown option are more than five for filtering the result so queries became complicated to implement. That's why I want to refine result of each dropdown individually.

Any help is appreciated. Thanks in advance!

My HTML code for dropdown list is:

<p>
<label for="experience">Experience :</label>
<select id="experience" name="experience">
    <option value="" selected="selected">All</option>
    <option value="Fresher">Fresher</option>
    <option value="Experienced">Experienced</option>
</select>
</p>

PHP code for executing related queries is:

<?php
if (isset($_GET['exp'])) {

    switch ($_GET['exp']) {

        case 'Experienced':
        $query = "SELECT job_seekers.ID, job_seekers.Name, job_seekers.Skills, job_seekers.Experience FROM job_seekers where job_seekers.Experience!='Fresher'";
        break;

    case 'Fresher':
        $query = "SELECT job_seekers.ID, job_seekers.Name, job_seekers.Skills, job_seekers.Experience FROM job_seekers where job_seekers.Experience='Fresher'";
        break;

    default:
        $query = "SELECT job_seekers.ID, job_seekers.Name, job_seekers.Skills, job_seekers.Experience FROM job_seekers";

    }
}
$result = mysql_query($query) or die(mysql_error());

echo "<ul class=\"candidates\">";

while($row = mysql_fetch_row($result))
{
    echo "<li>";
    echo "<p> <b>ID:</b> <u>$row[0]</u> </p>";
    echo "<p> <b>Name :</b> $row[1] </p>";
    echo "<p> <b>Key Skills:</b> $row[2] </p>";
        echo "<p> <b>Experience:</b> $row[3] </p>";
    echo "</li>";
}

    echo "</ul>";               

?>

3 Answers 3

4

When you want to AJAX call a php script, you should you $.ajax provided by Jquery

http://api.jquery.com

so you can use it like so:

$.ajax({
    url: 'ajax.php',
    data: {
        //put parameters here such as which dropdown you are using
    },
    success: function(response) {
        //javascript and jquery code to edit your lists goes in here.
        //use response to refer to what was echoed in your php script
    }
});

this way, you will have dynamic dropdowns and the refined results you want

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

4 Comments

Don't use data twice, that could be confusing. Use something like result as the variable of the returned data.
thanks. i have that habit of calling all my ajax data data because its easy to remember.
@Mic1780 I don't have much knowledge about Ajax or JavaScript. Can u please provide an example or any tutorial link so that I can understand in details. Thanks!
@Mic1780 For the url in above code I have used $_SERVER['PHP_SELF'] as my php code is in same html file. On selecting a option from dropdown, it load the complete page inside the <div> including header, menu and footer; rather then just query result.
0
<?php
if (isset($_GET['experience'])) {

    echo $_GET['experience'];
        /* do mysql operations and echo the result here */
    exit;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
function myFunction(value)
{
    if(value!="All")
    {
        $.ajax(
        {
            type: "GET",
            url: '<?php echo $_SERVER['PHP_SELF']; ?>',
            data: { experience: value},
            success: function(data) {
                $('#resultDiv').html(data);
        }
    });
    }
    else
    {
        $('#resultDiv').html("Please select a value.");
    }
}
</script>
</head>

<body>
<p>
<label for="experience">Experience :</label>
<select id="experience" name="experience" onChange="myFunction(this.value)">
    <option value="All" selected="selected">All</option>
    <option value="Fresher">Fresher</option>
    <option value="Experienced">Experienced</option>
</select>
</p>
<div id="resultDiv">
Please select a value.
</div>
</body>
</html>

2 Comments

Thanks. I have tried your solution. But when I select a option from dropdown, it load the complete page inside the <div id="resultDiv"> including header, menu and footer; rather then just query result.
@YogeshRawal, just echo the contents and then add "exit" after that content. for eg: after echo "</ul>"; add exit.
0

You cannot re-execute a PHP part on the same page. Rather use Ajax request to perform the action.

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.