0

So basically I have this program that sets a header (id="infoHeader") at the top of the page to some text retrieved from another site. I need this however to change when a button is pressed. As you can see it currently uses a PHP script to fetch the text from a given URL but I need to fetch from a different URL when a button is pressed. So basically

Button1 press -> infoHeader = Site 1 text

Button2 press -> infoHeader = Site 2 text

Is there a way to use a button to change the innerHTML of [id="infoHeader"] so that it runs the PHP script to get the text. It is kind of difficult for me to explain, if you need I can try and give more info.

<!DOCTYPE HTML>
<html>
<head>
    <title>KSA Flight Tracker</title>
    <link rel="stylesheet" type="text/css" href="KSAStyle.css"/>    
</head>

<body>
<div id=page>

    <div id=leftPanel>
        <p id=missionsHeader>Active Missions</p>
        <p><?php include("getList.php")?></P>
    </div>

    <div id=info>
        <p id=infoHeader><?php include("getData.php"); getTitle("http://www.blade-edge.com/images/KSA/Flights/craft.asp?r=true&db=dunai"); ?></p>
    </div>
</div>
</body>

2
  • you'd probably have to use javascript for this one Commented Feb 1, 2015 at 21:40
  • But I need to run this PHP script to get the text so how would I run that with javascript? Commented Feb 1, 2015 at 21:43

1 Answer 1

1

You need to use JavaScript + AJAX. JavaScript is needed to change the content of the div after the page is loaded/rendered and AJAX for loading a PHP file without the need to refresh the web page.

I recommend you to use jQuery and it's ajax function.

<!DOCTYPE html>
<html>
  <head>
    <title>KSA Flight Tracker</title>
    <link rel="stylesheet" type="text/css" href="KSAStyle.css"/>    

    <!-- include jQuery -->
    <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>

    <script>
      $(document).ready(function() { // wait for the DOM to finish loading

        // button1
        $("#someID").click(function() { // element with id "someID" (your button)
          $.ajax({
            url: "getData.php", // the content of the page which you want to load
            cache: false
          }).done(function(dataFromServer) {
            $("#infoHeader").html(dataFromServer); // set the data from the server (getData.php) as the content of the element with the id "infoHeader"
          });
        }):

        // button2
        // the same as above for the other button

      });
    </script>
  </head>

  <body>
    <div id="page">

      <div id="leftPanel">
        <p id="missionsHeader">Active Missions</p>
        <p> <?php include("getList.php"); ?> </p>
      </div>

      <div id="info">
        <p id="infoHeader"> <?php include("getData.php"); getTitle("http://www.blade-edge.com/images/KSA/Flights/craft.asp?r=true&db=dunai"); ?> </p>
      </div>
    </div>
  </body>
</html>

For your problem:

  1. Change content of div - jQuery
  2. http://api.jquery.com/jquery.ajax/

Here are some good resources for JavaScript, jQuery and AJAX:

  1. JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript
  2. jQuery: http://en.wikipedia.org/wiki/JQuery
  3. jQuery: http://www.w3schools.com/jquery/
  4. jQuery: http://www.codecademy.com/en/tracks/jquery
  5. jQuery + AJAX: http://www.w3schools.com/jquery/jquery_ajax_intro.asp
Sign up to request clarification or add additional context in comments.

5 Comments

I just did a quick google search on all this and I don't really get how to do what I want. (I only started HTML,CSS & PHP yesterday - I am learning it pretty quickly but I am stuck). So how would I use this to change the text with 'ajax'?
Ok so I tried reading through this but I still don't get what I am supposed to do. Where do I put it? What needs to be changed so it will work with my program? etc. Like I said - I am new and until 30 minutes ago had never even heard of AJAX or jQuery.
I tried reading them but it may as well be in Japanese - I just can't make any sense out of it...
Updated my answer with an example.
Thanks this helps a lot but could I get some clarification on what to change for it to work by using these words. ID of click detecting element: "someID" | File with function: "getData.php" | PHP Funciton: "getTitle(URL)" | Text to change: "infoHeader" |

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.