I have written a form in html and wish to submit it and run some php code, however once the php file has finished it main job I want to use it to redirect to my homepage where I am using AJAX to change content (where the form is). Is it possible to redirect to a page with GET eg and then use that to call a javascript function?

Thanks for any help. I'm not sure that's the best explanation of my problem, please ask if you need clarification :)

Dani AI

Generated

— yes, you can redirect back to your homepage with a GET parameter and have the page’s JavaScript act on it. ’s idea to redirect and then read the URL is the right pattern; below are safe, practical ways to implement it and a few pitfalls to avoid.

Server-side (PHP) — do the work, then redirect using a query string (escape values and send the header before any output):

// after processing the form
$status = 'success'; // or some value you want to pass
header('Location: /homepage.php?status=' . urlencode($status));
exit;

Client-side (homepage) — read the query string and call the function that loads/updates your AJAX content. Modern browsers support URLSearchParams:

document.addEventListener('DOMContentLoaded', function () {
  const params = new URLSearchParams(window.location.search);
  const status = params.get('status');
  if (status) {
    showFormResult(status); // replace with your AJAX/callback
    history.replaceState(null, '', window.location.pathname); // clean URL if desired
  }
});

Notes and troubleshooting

  • Use the Post/Redirect/Get pattern to avoid double submissions.
  • Don’t put secrets in the URL; use session/flash messages if the result is sensitive.
  • If your AJAX router or content loader runs after the above code, trigger the action through the router’s ready callback or dispatch a custom event so the handler runs at the right time.
  • Always validate/sanitize the GET value on both server and client before using it to change state.

This expands on ’s parsing idea with a simpler, modern API and adds server-side and security guidance you’ll need for a reliable implementation.

Recommended Answers

All 2 Replies

I'm still kind of confused with what you want to happen. But here I go, if you wanted to redirect a page using a javascript function, you may call window.location.href = 'url';

Then once you've redirected that user to the page and wanted to run some javascript function base on some get parameters, you can access the url's paramters using some function, see sample code below:

function getValue(url, name) {
    var result = {};
    var paramParts;
    var params = (url.split('?')[1] || '').split('&');

    for(var param in params) {
    if (params.hasOwnProperty(param)) {
    paramParts = params[param].split('=');
    result[paramParts[0]] = decodeURIComponent(paramParts[1] || "");
    }
    }
    if (result[name] === undefined && result[name] === null) {
    result = '';
    }
    return result[name];
}

you may call that function like getValue(window.location.href, 'variable_name')

it will return the value of the parameter you wanted to check on

Cheers that's brilliant. Exactly what I was trying to do!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.