1

I am making an interactive ticket system. I have two pages: ticket.php and myaccount.php.

On the 'myaccount' page is a ticket management system where a admin can assign themselves tickets. What I want to do is when an admin views the ticket and makes a response and updates the status of the ticket on ticket.php, I want myaccount.php to reflect the update of the status (without refreshing the page of course).

As of now, my ajax call works and it gets the POST value from another page. However, I want to update the div in myaccount.php FROM ticket.php.

Here is the ajax which is stored in a file called action.js, this function is being called in ticket.php and I want it to update a div in myaccount.php:

 function current_ticket() {
  $.ajax({

       type: "POST",
       url: '../user/phpfunc/ajax.php',
       data:{current_ticket: 'current_ticket_list'},
       success:function(html) {
         alert(html);
         //This function is called in ticket.php. I want it to update a div
         //in myaccount.php
       }
  });}

Here is the button being pressed that calls current_ticket() in ticket.php:

<input type="submit" name="submit" value="submit" onclick="current_ticket();">

Here is ajax.php as requested:

if(isset($_POST['current_ticket'])) {
echo 'test';}

I have looked for an answer and I can only find code which gets content FROM a div, but I want to send content TO a div in another file. Help is appreciated, thank you!!

apples99
  • 33
  • 4
  • You can use the jQuery.append() function http://api.jquery.com/append/. For example, by adding $('#result-div').append(html) in the success function – Ali Alwash May 23 '17 at 12:08
  • @AliAlwash I tried that. For example, $(".some-class").append("test") does not work unless its a div in the current page. I am trying to append to a div in another page and not the current one. – apples99 May 23 '17 at 12:11
  • youll need to show us the content of `ajax.php` as its results might be breaking your JS – CodeGodie May 23 '17 at 12:11
  • what do you mean by updating the results on a different page? Do you mean something like what Facebook does with notifications? – CodeGodie May 23 '17 at 12:12
  • @CodeGodie I updated the question. It simply has a test for echo. When I click the submit button, the alert shows 'test', so I know that it is working. However I want to update another page from the one im currently on without refreshing the page. – apples99 May 23 '17 at 12:13
  • Possible duplicate of [How to append current page contents to another page?](https://stackoverflow.com/questions/32242659/how-to-append-current-page-contents-to-another-page) – Alexandre Neukirchen May 23 '17 at 12:13
  • confusing.. this is what i understand: (sample) you are on page1.php, you click a button, that button makes an ajax call which echos "test". That string "test" should be placed on a different page like other.php ? .. so that if someone else was on other.php, they would see the changes? ... I think what you want is websockets. – CodeGodie May 23 '17 at 12:18
  • @CodeGodie That sounds about right. I would want the update to be reflected without refreshing the page. ajax seems to be good enough for appending content on the current page, but there is no available method to append to another page? – apples99 May 23 '17 at 12:20
  • You cant just append data to another page.. When you append data, it is not rewriting the HTML. It is just currently appending information in the current DOM in the browser. When the page is refreshed, that appended information is gone.. What you need to do on other.php is have some type of polling that checks for changes – CodeGodie May 23 '17 at 12:20
  • Look into WebSockets: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API – bamtheboozle May 23 '17 at 12:21
  • @CodeGodie I understand that, I am using a database that would reflect changes. As of now I have two pages open, the ticket and the account page. When I hit submit, the append should take place but it doesn't seem to appear on the div. – apples99 May 23 '17 at 12:21
  • So if it is not possible to append data from page a to page b without refreshing the page, then web sockets it is then? – apples99 May 23 '17 at 12:22
  • If you are going to write a Single Page Application (SPA), then your page must be fit, in every respect, to receive whatever dynamic content it is expected to display. If it's not, then you have failed in your quest to write an SPA. – Roamer-1888 May 23 '17 at 12:23
  • you can only append those changes on the current page where the button action is taking place. What you need to do is store that change in the database, then have another JS script on other.php that checks for that change.. Or work with node.js using sockets, its prob the best way – CodeGodie May 23 '17 at 12:23
  • for what you need, I would work using the MEAN stack (MongoDB, Express, Angular2, Node.js) . This will handle a great UI. – CodeGodie May 23 '17 at 12:24
  • @CodeGodie Ok thank you, I've just picked up jquery but I guess ill have to add more tools. I appreciate it. Its a learning experience :) – apples99 May 23 '17 at 12:28
  • Yup definitely. Never stop, it pays off in the end. – CodeGodie May 23 '17 at 12:32

1 Answers1

0

$("#div").append(YOUR_RETURN_HTML); would do the job.

Moreover, you have set the type='submit' and you have added an event onclick as well.

Either disable the default submission or change the type to button.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • I've tried that already. Let me ask this, does this method actually let me append to another page and not the one im currently on? For example, I'd call the append method in ticket.php, however I want that content to be appended to myaccount.php. It doesn't seem to be doing anything for me. $("class-in-myaccount").append("appending-from-ticket"); – apples99 May 23 '17 at 12:17
  • @apples99 do you need to append html or what? – Danyal Sandeelo May 23 '17 at 12:34