I have seen where you can open an external php file when clicking on a Submit button in a form, but how can I activate a local php file only when the Submit button is clicked?
Lawrence
| SitePoint Sponsor |

I have seen where you can open an external php file when clicking on a Submit button in a form, but how can I activate a local php file only when the Submit button is clicked?
Lawrence
What do you mean by 'local php file'? And by 'activate'?
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget

I have a form and beneath the form I have a php file that loads information into a data base. I don't want the php file to run until the form is complete filled in, and the Submit button is clicked.
By local, I meant a php file on the same page as the form, not an external php file.
Lawrence
In that case, check for the existence of the $_POST array. Put the processing above the form, not below it:
PHP Code:<?php
$errors = array();
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
//form validation and handling here
if(count($errors) < 1) header('location: http://www.yoursite.com/success.php');
}
//show the form. You can use the $errors array to inform the user of any errors.
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona

I have other forms on this page with their individual submit buttons. Will this still work?
It appears this is calling an external php file, I want to run a php file that is located in the same file as the forms. The reason for this is I want to use some other variables located on the same page but not as a result of a form.
Lawrence

If you want to post back to the same page (called a postback) make sure your form action looks like this:
File: index.php
ORPHP Code:<form method=POST action=index.php name=myform>
File: something.php
And be aware that if you want to catch whether the form was submitted by clicking a submit button, clicking another image or button and doing being submitted by Javascript OR if a user presses Enter from their keyboard - you will have to do what Arkinstall just said, or look for the value of one of your form elements that should not be empty.PHP Code:<form method=POST action="" name=myform>
If each submit button has to trigger a different php script, then you'll have to give each a unique name or value, and check for it's existance/value.
Just put your script where it says 'form validation and handling here'.It appears this is calling an external php file, I want to run a php file that is located in the same file as the forms.
Attention! The script is executed before the form is shown to the user. The result of that (a HTML page) is sent to the user's browser. Once the user clicks on the submit button, the form field values are sent back to the server and the script mentioned in the form's "action" parameter is executed (or the same script again if none is mentioned).The reason for this is I want to use some other variables located on the same page but not as a result of a form.
So, any values set in the script the first time, but not saved in some way (hidden fields in the form, session values or whatever), will NOT be there anymore when the form is sent.
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget

Use of PHP_SELF was considered harmful. Has that changed?
Leaving it blank seems to work for me though.

It goes back a few years but reading about PHP_SELF attacks stopped me using it.

If you can't use "PHP_SELF" for the action, and you can't leave it blank, and it is recommended that you show an action, what is the approved method of submitting the form data to php code listed on the same page as the form?
A search for "form submit action" on Google provides very little information on this.
Lawrence

Thats the preferred option, but its less flexible as you are developing, moving files around, refactoring, renaming them etc.- Have the value hard coded in there
Plus, in these days of Ajaxified interfaces the form action is becoming less relevant.
BTW, I am generalising about Admin interfaces, where I tend spend 90% of my time.
On public webs, with concrete urls almost sacrosanct, then I am pretty sure I hard code the Action without question.
Perhaps that clarification makes the suggestion to leave it blank makes it seem less radical.

i all always submit the PHP files online to test, use the loacal php file it seems no useful


I have used usually $_SERVER['SCRIPT_NAME'] instead of PHP_SELF.

Bookmarks