0

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.

17
  • your code is correct.If user selects NO then nothing wil happen as you do nothing . So what is ur problem?Be confident dude. Commented Apr 17, 2014 at 10:49
  • Please include jquery library. It is missing in your code. Commented Apr 17, 2014 at 10:50
  • 1
    @loganphp ,he just gave sample html ,he is smart enough to include JS library . :) dnt scare him Commented Apr 17, 2014 at 10:51
  • @PratikJoshi, cheers mate.. yeah, I deffo need some confidence :).. but, would this code, remove the included php file if the user selects Yes first and then No? Commented Apr 17, 2014 at 10:51
  • First, does this include.php have anything to do with your current page? What does it do? Second, you cannot exclude php the way you think. You need to explain what you are actually trying to do. Commented Apr 17, 2014 at 10:53

3 Answers 3

2

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('');
            }
        });
Sign up to request clarification or add additional context in comments.

9 Comments

I haven't tried this yet.. but this looks like what I am looking for. I will select this as the answer as soon as I've tried it.
@user3454730 ,hello i also gave u answer ,why not to select my ans?
@PratikJoshi, tried yours now.. and it worked as it should mate... nice one.
@PratikJoshi Please. First of all the class you are using is nowhere mentioned in the OP's code and like Adam said, the OP is not trying to make the page empty, only the container.
@ICanHasCheezburger, Patrik is not showing how to empty the page!! he's actually giving a class .class so that could be an ID #id instead. it worked for me... I'm sure you haven't tried it and just going by what Adam said!!!
|
1
If yes is selected then 
    Include file    
else
$( ".page" ).empty();
//make the page content empty Simple

2 Comments

The OP doesn't want to make the whole page emtpy (and doesn't have anythign with a page class). Only the #content div
@Adam, +1 For actually reading the question and understanding it.
1

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

2 Comments

Or by calling .empty()
Actually empty() would be better yeah. Thanks

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.