2

how to change PHP include ("link.html") by click buttons.

<?php include('link1.html')?>
BUTTON 1 change <?php include('link1.html')?> to <?php include('link2.html')?>
BUTTON 2 change <?php include('link1.html')?> to <?php include('link3.html')?>
BUTTON 3 change <?php include('link1.html')?> to <?php include('link4.html')?>

how to do this without refreshing page. using ajax?

4
  • Does link1.php contains HTML content? If you want to change the HTML content then define a div with unique ID and content may be change using ajax call. Commented Oct 7, 2016 at 8:48
  • yes, all the php files contains only HTML contents., EDIT: i change extension. Commented Oct 7, 2016 at 8:49
  • @RahulPatel let me try this. Commented Oct 7, 2016 at 8:54
  • Please try with the answers. Commented Oct 7, 2016 at 9:04

3 Answers 3

2

Wrap include('link1.html'); with a DIV with unique ID. And on click on button call a ajax and replace the DIV content.

Please try with below code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="DIVID">
    <?php
        include('link1.html');
    ?>
</div>
<button onclick="btnclick('link2.html')">Button 1</button>
<button onclick="btnclick('link3.html')">Button 2</button>
<button onclick="btnclick('link3.html')">Button 3</button>
<script type="text/javascript">
    function btnclick(_url){
        $.ajax({
            url : _url,
            type : 'post',
            success: function(data) {
             $('#DIVID').html(data);
            },
            error: function() {
             $('#DIVID').text('An error occurred');
            }
        });
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You can do that with jquery, example:

PHP Code:

<div id="mainDiv"><?php include('link1.html')?></div>
<button onclick="change2()">Button 1</button><br />
<button onclick="change3()">Button 2</button><br />
<button onclick="change4()">Button 3</button><br />

JQuery code

function change2() {
  $('#mainDiv').load('link2.html');
}

function change3() {
  $('#mainDiv').load('link3.html');
}

function change4() {
  $('#mainDiv').load('link4.html');
}

Don't forget to include de JQuery

<script src="jquery/jquery.min.js"></script>

Comments

0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  
<button onclick="include( $( this ) )" value="2">Button 1</button>
<button onclick="include( $( this ) )" value="3">Button 2</button>
<button onclick="include( $( this ) )" value="4">Button 3</button>
<div id="included_page"><?php include('link1.html')?></div>

function include(elem)
{
  var page = elem.val();
  $.ajax({
    url: "link" + page + ".html",
    type: "GET"
  }).done(function(msg) {
    $('#included_page').html(msg);
    })
  })
}

I am not great at ajax but something along these lines may work, if you get any problems let us know.

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.