send GET request without loading page in php - php

Hi i have a form after the submit i want to run url in background with out goin to the page it self
if(isset($_POST['submit'])){
http://url/default/en_US/send.html?u=admin&p=hodamai&l=1&n=01211200671&m='
}

I think you want an AJAX-Request to your server. That means your browser makes a background-request to your server and execute the script while you're still on your homepage. Then you'll get the response as JSON. If you want, you could use fetch as well, but I prefer AJAX because even IE supports that. I think both are easy to use ;)
https://www.w3schools.com/xml/ajax_intro.asp

Related

PHP redirection with post values

Is it possible to redirect user to external server without using:
header('Location:address.com?var=1');
or
HTML form with JS auto submit
Seems really simple but can't figure it out. I do not want to use GET, I would rather send it via headers/post if possible.
Making ajax request is slowing it down and I believe there should be some kind of solution in pure PHP.
CURL is not an option as I want to go to that link with posted data.
Cheers
You can do this using this method session_set_cookie_params(). It is used to transfer data from 1 url to another url using session. To get the result you have to write this function before every session_start().
session_set_cookie_param();
#session_start();

POST in PHP AND redirect the user to that page?

I'm a bit confused. How do I POST data to a URL and then redirect the user's browser to that location - all in one operation?
I see
header('Location: page.php?' . http_build_query($_POST));
but that is GET, not POST and ppl think thats really bad practice - PHP open another webpage with POST data (why?)
My kludgy workflow involves setting up a form and then submitting it via javascript - anything has to be better than that...
I think I can do a set of header() stmts but this action happens for the user way after the page has been geenrated, so i dont think that would work
You cannot redirect POST requests. As simple as that. Any redirect will always turn into a GET request.
If you want to receive POST data, then send that data to another page, you have two choices:
if both pages are on the same server, use sessions to save the data server-side, don't make the client carry it over
if the destination is on another server and you need to send the client there together with the data, set up another intermediate form like you are
Use AJAX to save the data before you leave the page. Use the answer you get back to fire a redirection to the new url right within the current page. Don't be affraid of Javascript and ajax. Try this light AJAX library: http://www.openjs.com/scripts/jx/

Monitoring for postbacks with PHP?

I have a PHP page with content that my users can view. When this page receives a POST request from a certain external URL, I'd like to redirect the user to another page.
The problems I'm' having are:
How can I monitor the page for requests being sent in an efficient way?
How can I actually redirect them since header() doesn't work.
Thanks.
For the second question, header() will work if you use it before you output any information to the page. However, if you echo, print, or have HTML before it, it will give an error.
Edit: In response to Toad's comment, then you'd have to do as Aaron Harun suggested. Have the page save the $_POST data to the database or a file (make sure you sanitize it!), just like you would with any $_POST data. You would then need to use AJAX to get a response from a second PHP page that simply checks for the existence of updated data from wherever you saved it. If the response comes back true, then you redirect using a JavaScript redirect.
The only other way to do it without using AJAX would be to refresh the page using an HTML meta refresh element at timed intervals to check if $_POST data has been received. HTML would perform the refresh, PHP would do the checking, and you could use either for the redirect.
You can use an AJAX script to "ping" the server intermittently to see if there have been any changes. If there are, redirect the user with JavaScript.
It's pretty much the only way.
we can put this snippet to check the post request
if($_POST['flag']==1) {
header("location:newpage.php");
exit();
}
But If you want to check the request regularly without user interaction than you will have to use AJAX

jQuery Post to PHP to Redirect?

I'm trying to figure out if I can do the following:
User submits form to script via jquery post
Depending on what was processed, script may or may not return errors
If the script did not process any errors (i.e. SUCCESS), I want to redirect to a SUCCESS page
I know it's possible to redirect the browser via javascript, but I'm worried if some users have javascript disabled the entire flow might get messed up.
Thanks in advance.
As you want to evaluate the form (currently) with jQuery, you can safely assume that, when the form is evaluated, JavaScript is available.
For those without JavaScript, you should add some server side routine, that does the job. And then you can use a redirect via HTTP header, or a <meta> redirect.
To keep both ways working, hard code the server side routine into the code, and replace it via jQuery when the page is loaded. JavaScript users will execute the jQuery replacement and get the jQuery evaluation; other's will just stay with the normal version.

PHP Render/Redirect AJAX Call

I am working on a PHP application and missing some of the functionality that Rails has. I currently have an AJAX form that when submitted accesses my_page_save_ajax.php. After I process the form and save it, I would like to redirect the AJAX call to either my_page_show_ajax.php if successful or back to my_page_edit_ajax.php if an error occurred.
I have thought about using an include my_page_..._ajax.php, but I have always had problems with the file paths and PHP not knowing what to render. Not to mention, both of those files include utilities.php and I'm afraid there might be conflicts. I guess I could use include_once but it seems like there might be a more elegant solution.
How can I process the form and return the output of another PHP page?
Thanks very much!
If you redirect the AJAX response, it won't actually redirect the user's browser anywhere. It will simply affect what data comes back through the AJAX call. This may be a good instance to simply not use AJAX, since it sounds like the user may go on only one of two paths.
If you still want to redirect the user, you could send back a javascript snippet which redirects the user via setting window.location

Categories