0

I got really confused when trying to get PHP functions into javascript. In my scenario, I just extract some lines from XML file in PHP and convert it into a table, and the presentation of the whole table is in the function getNews(). Into the function are echo sentences like

 function getNews(){
    $xmlResponse = new SimpleXMLElement('xml.addr',0,TRUE);
    foreach($xmlResponse -> children()as $contents){
        echo "<table id ='news'>";
        echo "<a href='".$links."'>".$contents -> item[$num] -> title."</a>";

HTML tags are involved to build a news table. Then I was asked to implement a button, when clicking on the button, the button image changes and the table should be shown below. Obviously the onclick function should be written using javascript, when click on it, I need to call the anotherButton() function and show the news table

   echo  "<script>function anotherButton(){
      document.getElementById('news').innerHTML='<div class='newStyle' 
      onclick='anotherButton1()'>click to hide stock news
           <img src='http://image-addr'></div>'; 
             }</script>";

All these things were done in PHP and I need to insert function getNews() somewhere in the above echo, so how I can put the function in it ? Thanks in advance.

Mark Dong
  • 59
  • 2
  • 8
  • Are you trying to make a previously hidden element visible? possible duplicate: https://stackoverflow.com/questions/6019845/show-hide-div-on-click-with-css Simple answer: https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp – Erik Oct 19 '17 at 23:55
  • yes. the element is the news table, when i click on it the button changes and the news table shows below, when again clicks on it the table hides and the button also changes. – Mark Dong Oct 19 '17 at 23:56
  • 1
    Edited my comment above, you should be able to figure it out from there. Next time think about what you want to achieve, break it down in it's smallest possible step and google the step you don't know. =) – Erik Oct 19 '17 at 23:58
  • please show more code. especially your `getNews()`. Is this a in php or in javascript? – Jeff Oct 19 '17 at 23:58
  • You add getNews() as a promise callback in your ajax call – miknik Oct 19 '17 at 23:58
  • 1
    @miknik Where is there an ajax call? And do you really think - according to the level of the question - "as a promise callback" is understandable to OP? – Jeff Oct 20 '17 at 00:00
  • 1
    You need to get a better understanding of the timing difference between PHP and Javascript, see https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Barmar Oct 20 '17 at 00:01
  • He wants to click a button and add dynamic content, there clearly has to be an ajax call. No, I don't think he'll understand, but I've given him enough to Google and learn. – miknik Oct 20 '17 at 00:04
  • I just update some codes and AJAX is involved to get the xml file from website. – Mark Dong Oct 20 '17 at 00:05

1 Answers1

1

What you need is the following:

  1. Javascript sends the data to server using AJAX in the client's browser.
  2. PHP decodes the request, processes it then responds with the information requested.
  3. Javascript receives the response and displays it accordingly.

Using Asynchronous Javascript And XML (or AJAX for short); you can essentially fetch data from the server dynamically without the client performing a browser refresh.


Here is a video by Codecourse which will get you started in the right direction.

Tenbo
  • 144
  • 1
  • 10