performing php script without reloading the page - php

<form action ="send.php" name ="userdetails" onsubmit="display(); return false;">
although this does perform the send.php script, it loads the send.php page and the content of the current page is lost.
I need to perform the following, once the form is submitted - an email should be sent. However the current page is not to be changed. What is the best way to accomplish this? Thank you

Setting a value for action="..." will always redirect you to a new page.
For what you want to do, AJAX is perfect. It allows you to run a PHP script without leaving the page you are on. I would check out jQuery's AJAX capabilities.

Related

Preventing the action tag from switching the current page?

I currently have two PHP files set up, one that functions as the form and the other that functions as the processor of the information provided. They are linked together like so:
<form action="processing.php" method="POST" enctype="multipart/form-data">
When I hit submit, the page switches to the 'processing.php' file. I recently implemented required fields that users must enter, but they cannot see this warning because the page switches. I am testing this in WAMP.
I think a solution may be to include the processing script in the same file as the actual form, but the script is pretty long and I'd really like it to stay in two separate files for readability.
Is there a way that I can prevent the page from switching to the page indicated in the action tag?
The HTTP POST from a form goes to whatever form action you set. If you want logic applied that's in a different file, you need to restructure things so that your logic is in processing.php, or you need to change the form action.
If you want to prevent page refresh all together, you need to use ajax.
If you only want to prevent the page switch if the required fields aren't properly filled, you need to create an onSubmit javascript call, then return false if field validation fails.

Form Submit with Different Redirect Location than Action

Problem:I have an RSS feed. As some of you may know, RSS feeds do not always update promptly (I'm using FeedBurner) so I'd like to provide the option on my webpage to update the RSS feed. This is a simple process, and I just need to ping an address. The catch is this: I'd like to stay on the initial page, and ideally refresh it.
I've seen some "solutions" around with using hidden iframes, and javascript, Ajax, etc.. What I am wondering is if there is an elegant way to do this using php/html.
Below is a flowchart illustrating exactly how I would like the system to function.
EDIT:
Here is the simple form code which I currently have:
<form action="http://url.to.ping" method="post">
<input type="submit" value="Refresh" />
</form>
This is a standard form, performing an action on submit. I require now that the browsers destination (as seen from the user) is a different url than that in the action. It is worth noting that the action page is not in my domain, and is not part of a domain which I own or have access to.
Thanks!
What i meant was,
/contactme.php
once they've submitted and come back to the page is there any additional variables like
/contactme.php?thanks=1
basically is there anything to declare they have just submitted and come back to the original page, if so..
You could do;
<?php
if(isset($_GET['thanks']))
{
$pingServer = file_get_contents('http://www.the.server.to.ping.com/pingit.php');
unset($pingServer);
}
?>
at the bottom of the page and it'll just hit that page.
This way you are not relying on JavaScript being enabled and the user is not hopped around multiple URLs.
What I have done when I needed the landing page to be different from the processing page is add a JavaScript redirection where one would put their "thanks for filling out my form" material.
So, the code process would be:
user fills out form, clicks submit
server-side validation and processing.
if success then location.href(URL, 0); else do error case
user is redirected to new URL (your refresh page)

How do you make the browser re-post form data when using the back button?

I guess my question is in understanding the $_POST handling.
So I have two pages that handle 2 forms. Page 1 asks for some information that will be used in page 2. When submitted the form action uses the same page then redirects to next page upon validation, but only handles the data when $_GET variable ?usersubmit=1.
<form action="<?=$_SERVER['PHP_SELF']?>?usersubmit=1" method="post" name="form1">
<input type="text" name="field1">
</form>
So say I have page called form1.php. Upon submit its sent to form1.php?usersubmit=1. The page now assigns the posted data to session variables and then redirects to form2.php via header('location:form2.php').
<?
if($_GET['usersubmit']){
if($_POST['field1']){
#if valid then assign session variable and redirect to next form
$_SESSION['field1'] = $_POST['field1'];
header("location:form2.php");
}else{
#if invalid send error message
$error = true;
}
}
?>
My problem is in when users hit the 'back' button on their browser to edit data from a previous form. The browser doesnt re-post this data it just shows them a blank form. I'd prefer not to use $_SESSION data to fill out the forms because I suspect the re-post method may be a quicker and less problematic fix.
I also tried a javascript redirect instead of a header but browsers are smart enough to not send you back to a page that wants to redirect you so it doesnt work.
any help in understanding this would be greatly appreciated.
thanks.
The only way to handle it is via a session... HTML5 allows for storing of that kind of data but to be honest I wouldnt even look into it as a possibility just yet, altough it does work.
typically the back button will use all of the same get and post variables as was used on the previous locations page load. server side header redirect will not allow you to use the back button to get to the previous page since this redirection is done on the server side.
there are two ways to redirect using javascript window.location which will put your previous location into the browser history and therefore the back button will work, and location.replace which will not put a link in your history, and therefore not allow you to use the back button to get to the previous page.
alternatively, you could just use page two to process page ones form...
Also, you should use htmlentities() in your code. It closes a security vulnerability (see http://www.html-form-guide.com/php-form/php-form-action-self.html for more details).
<form action="<?=$_SERVER['PHP_SELF']?>?usersubmit=1" method="post" name="form1">
should change to something like this below.
<form action="<?=htmlentities($_SERVER['PHP_SELF'])?>?usersubmit=1" method="post" name="form1">

how to post data from one php to another without reloading it?

I have a html page with two iframes in them .there is a button on first iframe which should reload second iframe with some data which is sent on the click of the button ..but the first iframe containing the button should not reload.how can i make it possible ?
<?php
// code to send data without reloading page but instead reloading another iframe with sent data?>
In the first iframe, use javascript to submit the form by some sort of ajax method:
<form [...] onsubmit="sendAjax(); return false;">
The return false part prevents the form from submitting (as long as you don't have some sort of errors in your sendAjax() function.
Since it is going to take a bit of time for the server to process the request that should show up when iframe 2 refreshes, you could possibly use evilone's suggestion and use sessions to see if some sort of session variable has been set. Then in frame 2, make the javascript wait say 500 ms before refreshing and then refresh the page. You could loop this refreshing process to make sure that the session variable has been set and that the correct output will be sent.

Can I have PHP redirect to an in-page link when I process a form?

I have a vertical scrolling website (lots of in-page links). I also have a contact form script I'm working on.
I'm trying to set it up so when someone completes/submits the contact form, it redirects them to #contact_area (on the same page), but calling the header function after is throwing a "Cannot modify header information" error.
Any suggestions on how to redirect after a script is processed from within ?
Thanks!
A header redirect needs to happen before PHP prints any output. If you want to direct the user to an anchor on the current page you have two options:
Submit the form as normal. Your PHP script processes the data and does this before any output: header("Location: /my_same_page#contact_area"); The page will be reloaded but they'll end up in the right spot.
Submit the form data via AJAX and then scroll to the #contact_area anchor.
The second options is probably the cleanest but the first one should be a lot easier for you to implement.
You can work around this using output buffering, few examples here
I am assuming you are posting the form back to the same right?
Maybe submit the contact form to another page for example contactus.php and once its been successful place your header location code into that OR you could use the jquery form plugin (submit() function I have used) which runs in the background and then you could maybe jquery scrollTo() your hashtag.
Go got option 1 and if you have time then play with the jquery version maybe.

Categories