How to send message back to previous page?

Hello! I have no idea is this possible with simple php/javascript and have no idea where to start!

Imagine i have page.php and there are link tag that will open open.php (opens in new tab) there on some successful form submit (and this is where it gets tricky for me) open.php need to redirect to other URL (different domain). <<< all good till this!

The question is: How to show message with result (like “all done” or any custom dynamic value) in page.php (in alert or something like that)?

I have not tried anything, have no idea how to search this in google :man_shrugging: so any tip/help is needed :smiley:

Thanks in advance!

Have you tried to use localStore or sessionStore?

Hi, have no idea what are they :man_shrugging:

Maybe i m not getting this but i do not see how this helps dynamically add mesage in page.php after successful action in open.php :man_shrugging:

To store a value you can use in the browser memory. Can be used to communicate between pages.

/* store the value */
window.localStorage.setItem('msg', "Everything OK");

/* To retrieve the value */
alert(localStorage.msg)

https://jsfiddle.net/397okrwg/2/

1 Like

How will page.php know about this?

Just readed post where this is suggested with ajax.

So i would save in database (or maybe session also works) All Good in open.php

And in page.php i would call ajax to check for new messages (in database or session). …
Maybe this is better?

1 Like

I don’t think you understood me :frowning:

  1. In page.php i click link that opens open.php in new tab
  2. I click submit button on open.php that redirects me to google.com
  3. What i want is to pass and show success message from open.php to page.php if open.php was redirected

Keep in mind that page.php is not refreshed. …

I am not aware of any ways to communicate but directly using parameters (arguments), references, pointers or indirect via variables, some storage or micro services. In web development is the information stored in several layers (using different languages) which makes communication harder. So I pass this.

1 Like

:frowning: Ok thanks for trying! :slightly_smiling_face:

1 Like

It’s technically doable, but not really recommended, as many browsers and plugins would block your popup (which is what it would have to be) from appearing.

That said, two distinct paths appear to my mind immediately;

  1. a javascript controlled popup in which you monitor the window location until it became unavailable to your original script (because at that point it becomes blocked by Cross Origin Policy). This has the advantage of not requiring extra runaround, but does experience false positive results (if a user simply closes the popup instead of filling out the form, etc.)

  2. when the link is opened, it contains a URL variable that is a uniqueID. Your javascript on page.php starts polling amidone.php asking it if uniqueID has submitted their form, waiting for it to tell you yes. Meanwhile, the form on open.php adds the uniqueID as a hidden form field. When they submit the form, a database entry gets added for uniqueID; and amidone.php can find it and answer yes to page.php and clean up the database entry (if required). At that point, page.php knows that the form was submitted, and can display whatever it needs to.

2 Likes

Thanks for answer!

Yeah your #2 looks like my idea of using ajax and it looks like the best solution to do it as i wish!

I m not experienced with anything outside php/javascript so for now this will be it i guess. …

Give it a try, and if you run into trouble, come back and show us what you did and what the problem is, and we’ll help you through it :slight_smile:

1 Like

First, instead of trying to send a message (to page.php) think in terms of how page.php can get the result. I think that all the suggestions here are doing that.

Have you considered some type of single page application?

As @sibertius suggested, you can use the web storage for page communication; on page.php, you’d just listen to storage events like so:

on page.php

<a href="open.php" target="_blank">open</a>
<p id="message" hidden>Done!</p>

<script>
  var message = document.getElementById('message')

  window.addEventListener('storage', function (event) {
    if (event.key === 'submit' && event.newValue === 'OK') {
      message.hidden = false
      window.localStorage.removeItem('submit')
    }
  })
</script>

on open.php

<form action="submit.php" method="post">
  <button type="submit">submit</button>
</form>

on submit.php

<p>Success!</p>

<script>
  window.localStorage.setItem('submit', 'OK')
</script>

When going the AJAX route OTOH, you might open('open.php') using JS instead of a regular target="_blank" link, where again you’d submit the form using AJAX and communicate back to the opener window using postMessage(); this would be a bit more involved but you’d have a direct channel between the two pages.

2 Likes

One solution could be to use - php header() function to redirect the user - and store
a value in session cockie. but kinda depends what you collecting and sending ofc.

more on PHP header() Function -
php.net - https://www.php.net/manual/en/function.header.php

do a google search and you find lots of ways to use the header function in php.