user3454730
user3454730

Reputation: 281

PHP include using jquery AJAX from a dropdown select option?

I am trying to include an external php (on my own server) in another php page using jQuery AJAX from a select form.

basically I want to let the user include the external php file if they select YES option from the dropdown select form and remove it if they select NO.

I have the following code:

<html>
<head>
<script>
        $('#test2').change(function(){
            var selectedValue = $(this).val();
            if (selectedValue === 'Yes') {
                $("#content").load("include.php");
            } else {
                //WHAT DO I NEED TO PUT HERE TO REMOVE THE "include.php" if it has been inlcluded?
            }
        });
</script>
</head>
<body>

<form>
<select id="test2">
<option value=""></option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</form>
<br>
<div id="content"></div>

</body>
</html> 

First question: am I doing this right?

Second question: what do I need to do in order to remove the include.php file if they select NO option?

any help would be appreciated.

Thanks in advance.

P.S. i do have jquery included in my page.

Upvotes: 0

Views: 1332

Answers (3)

Pratik Joshi
Pratik Joshi

Reputation: 11693

If yes is selected then 
    Include file    
else
$( ".page" ).empty();
//make the page content empty Simple

Upvotes: 1

Adam
Adam

Reputation: 6733

The $('#comment').html('') in the else block are right. However, your if-condition is wrong (.val() returns the value not the text of the chosen option). Use:

$('#test2').change(function(){
            var selectedValue = $(this).val();
            if (1 == selectedValue) {
                $("#content").load("include.php");
            } else {
                $('#content').html('');
            }
        });

Upvotes: 2

Abed Hawa
Abed Hawa

Reputation: 1362

Your code looks like it's requesting include.php just fine. You can clear the loaded data by calling

$('#content').html('');

in the else section

Upvotes: 1

Related Questions