简体   繁体   中英

Running PHP Code Without Loading A New Page

This might be a dumb question, but I'm new to transferring data from the client side to the server side. I'm using the code here to create a cookie. Then I was going to use $_COOKIE in a separate PHP file to read the data from the cookie. Is it possible to run my PHP code without loading a new tab/window?

You can have the server return a 204 No Content response , or make the request with fetch or XMLHttpRequest , or by loading an image, or by loading a new page in an iframe, or by making any other HTTP request that doesn't trigger a whole new page load.

… but there seems little point in storing data in a cookie (which is used when you want to include data in every subsequent request ) and then sending it to PHP without caring about what the response it. Possibly you would be better of forgetting about the cookie and just using fetch or XMLHttpRequest to make a POST request with the data in the request body instead of in a cookie.

You can send a POST/GET request to the target page using AJAX.

    $.ajax({
      url: "test.php",
....

Read more about Jquery's Ajax Here.

You don't really need to use jQuery, you can use the standard vanilla way.

As for server-side, you can fetch data sent via AJAX with $_POST['foo'] , $_GET["bar"] ...

Where 'foo' and 'bar' are the field names passed thought AJAX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM