Copy link to clipboard
Copied
I have a page with a form that calls a php program via a "Submit" button (i.e. method="post" action="sendmail.php"). The php program works fine, and sends the email as it's supposed to, but then doesn't return to the calling page. I want it to simply display a "mail sent" message of some sort (preferrably a message bok with an OK button) - without displaying an entire web page - and then return to the page which called it. What gets emailed is the information entered into the calling form, and some text notifying the recipient that a pre-employment questionnaire is ready for them to fill out.
I've been working on this for over a week, and havent't been able to find out how to get back when a php program is called. The fact is that the php wouldn't even have to be in a separate file, except that all the information I've found regarding "action=" indicates that it must. I tried placing the php code in a function located in the <head> section of the calling page, but "action=functionname()" didn't work.
Can someone pleas help me with this?
Copy link to clipboard
Copied
>The fact is that the php wouldn't even have to be in a separate file,
>except that all the information I've found regarding "action="
>indicates that it must.
Nah, that's not true at all. You can have a form self post and have the script that processes it on the same page. The technique is to have the script check if the form has been posted. If it has, process the form and then display whatever you want. If it has not, then just display the form. I do this with .asp forms all the time. I can't help with the php code, but I'm sure one of the php guru's will reply shortly with some examples.
Copy link to clipboard
Copied
Well that certainly makes me hopeful, but how do I code the action="" in the HTML <form tag, to self post instead of a URL? I tried referencing a function there, but it gave me a "Form Not found" message when I ran it. I thought I could use the "onClick" in the <input, but it still wants a URL in the action-.
Copy link to clipboard
Copied
>but how do I code the action="" in the HTML <form tag, to self post instead of a URL?
Actually, action="" is all you need. The form will then post to itself. When the page loads, the php script will need to evaluate the form field states to determine if the form was posted.
Copy link to clipboard
Copied
Hi, and thanks for the reply. That actually sounds like the right answer for what I'm trying to do. There is, however, still a question in my mind about this. If, as you say, the form "self posts", how does it know where the php code is, and where do I put it? Do I set it up as a function and use the "onclick=" to call it, or is there some better way?
Copy link to clipboard
Copied
>If, as you say, the form "self posts", how does it know where the php code is, and where do I put it?
The script goes in the beginning of the page. The html form goes below. Every time the page loads, the script gets executed. Example: a visitor is opening the page from a link, the script executes and evaluates the form fields or another condition that will determine if the form posted. In this case, the condition would be false and the script would branch to the html form. If the visitor then posts the form, the script would run again, evaluate that the form was posted, and execute the routine that processes the form. You could then choose to display a message, and could display the form again if you want.
>Do I set it up as a function and use the "onclick=" to call it, or is there some better way?
Nope. The php script will simply evaluate whether the form was submitted or not, and then take the appropriate action. I'm sure it will be much clearer when someone here posts some code. In the mean time, you could probably search for 'php self posting' and find some code.
Copy link to clipboard
Copied
Wow! What took you so long. That's the fastest response I've ever experienced, and thank you for the explanation; you made it quite clear. Most of the code I already have. I think the only thing I have to research is the part about evaluation whether or not the form is posted, which I suspect is "if, else" logic.
Thank again, you've been extremely helpful.
Copy link to clipboard
Copied
bregent wrote:
>If, as you say, the form "self posts", how does it know where the php code is, and where do I put it?
The script goes in the beginning of the page. The html form goes below. Every time the page loads, the script gets executed. Example: a visitor is opening the page from a link, the script executes and evaluates the form fields or another condition that will determine if the form posted. In this case, the condition would be false and the script would branch to the html form. If the visitor then posts the form, the script would run again, evaluate that the form was posted, and execute the routine that processes the form. You could then choose to display a message, and could display the form again if you want.
>Do I set it up as a function and use the "onclick=" to call it, or is there some better way?
Nope. The php script will simply evaluate whether the form was submitted or not, and then take the appropriate action. I'm sure it will be much clearer when someone here posts some code. In the mean time, you could probably search for 'php self posting' and find some code.
I followed you instructions (for testing purposes, I didn't add validation code), but it didn't work. I coded the action="", and placed the php code at the very top, just before the <html> tag. I also tried it after the <html> tag, but before the <head> tag, and also just after the <head> tag. None of these worked. When I executed the form, there was "?>//" showing at the very top of the form, and when I entered the data and clicked the submit button the form simply reset to its original staste.
What am I missing or doing wrong? I was so hoping that this would be my solution.
Also, I Googled "PHP Self Posting" (and several permutations of that), but came up empty.
Copy link to clipboard
Copied
See if this helps:
http://www.html-form-guide.com/php-form/php-form-action-self.html
Note that I am not sure why you need the PHP_SELF variable. I've always thought that forms will always post back to the calling page if the action is blank. Maybe that's browser specific.
>When I executed the form, there was "?>//" showing at the very top of the form,
That sounds more like a coding error. But read the link I included first and if you are still having problems, include the example code. It may also be a good idea to start a new thread if none of the php experts reply to this one. Sometimes folks will ignore threads that already have activity thinking that it's being handled sufficiently.
Copy link to clipboard
Copied
HI,
Thanks for the response. After reading the explanation in that link, I was positive that it was the answer to my problems. I coded my php exactly as they showed, and I cut & pasted the action= part, to be certain I didn't make a coding error. Unfortunately it didn't work either.
In my xampp test environment (which uses the Apache server), the submit yielded:
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
When I tested it on my yahoo web hosting server, the submit yielded:
The web page cannot be found
The most likely causes
Etc., etc.
I'm still looking into this; it seems to me that when a reputable website publishes something like this, they must be correct, but I don't know what I'm doing wrong. If it only happened in the Apache server, I'd figure that the trouble was there, but when it happens in the live environment, then I must be missing something.
Thanks for your help. If you think of anything else, I'd appreciate hearing from you, and I agree with your idea about starting a new thread. I'll do that as well.
Thanks again.