Different between actions - php

I want to know different between <form action="#" method="post"> and <form action="name of file" method="post">
I am always using # but don't know disadvantages.
Can you explain why I should use # or file name?
Thanks

form action = file name
It is used to send a request on the other page(i.e your file name) containing your form fields(inputs) with methods like GET and POST.
example my HTML page is having a form then and my PHP page is having all the backend code. Whatever I need to do with form inputs. I will give the file name of my PHP page in action. the action attribute of the form is used to send the form request to the destination we want to with methods like the POST and GET. If you do not want to send a request to another Page and want it to your default page. You can leave action ='' attribute of the form empty as I did.
An action of # indicates that the form stays on the same page, simply suffixing the URL with a #. A similar use occurs in anchors. Link for example, will stay on the same page.
Thus, the form is submitted to the same page, which then processes the data etc

The content of action allows you to know where you will put the code that will process the request.
If you put the name of a file it, then his file will process the request.
For example: you have your form on the index.php page and you want to put the PHP code of the form in a process.php file. You will put process.php in action (action="process.php").
If you do not put anything it is like sending the content of the request to the same file (index.php).

Related

Wordpress form is not submitting

Hello I am very new to WordPress on my requirement I have created a couple of php files in wordpress theme. Where detailsform.php consists
<form method="post" name="details" action="customerdetails.php">
where after clicking submit button the form has to redirect to customerdetails.php in php it is working fine but in wordpress it is giving 404 error(page not found) I kept all new php files in the existing theme folder.
Please suggest me it is killing my time.
As wordpress has its very own specific way to handle ajax calls, wordpress has it too for post http request by form submitting. In the codex on wordpress there is more details about that.
In a brief explanation, using your functions.php file on your wordpress theme:
The first thing you need to do is set in your action attribute from your form tag, point the following url:
<form action="http://www.example.com/wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="my_handler_function">
</form>
Once pointed, you need to add a hidden input with the name attribute action and a value attribute specifying the name of your action. Then, you need to build your handler function on your functions.php file. This is where your going to write the code you need for treat the data you will received by the global $_POST or $_REQUEST variable.
function my_handler_function() {
var_dump($_REQUEST);
die();
//request handlers should die() when they complete their task
}
Next step is to bound this function to your form by using the action hook admin_post_nopriv_ or admin_post_. The difference between these, is where your form is placed. If your form is for a custom functionality for the admin of wordpress then is private and you use admin_post hook action. If this form is part of your public content then use the admin_post_nopriv_ like in the following example:
add_action( 'admin_post_nopriv_my_handler_function', 'my_handler_function' );
As it is show in the code example above, you need to call the add_action function. In the first parameter, It is needed to pass the action hook provide by wordpress combined with the action value specify in the hidden input named action in the form, like admin_post_nopriv_$action_value. In the second parameter, you need to placed the function name you build on you functions.php file. Both are mandatory.
For matters of conventions, generally, the name of the function handler is set it as same as the value of the action input to avoid misunderstandings and gain more readability.
Onced everything is put it all together, all you have to do is test your code.
Happy coding!!
PD: If you want to clarify about this procedure of wordpress, please take a look in the wp-admin/admin-post.php file, but don't even dare to modify it.
// Put your file customerdetails.php in current theme and use following path in action:-
<form method="post" name="details" action="<?php echo get_template_directory_uri() ?>/customerdetails.php">
404 error means the action is wrong for form.
<form method="post" name="details" action="customerdetails.php">
^ ^
Correct the action to exact path of customerdetails.php
Issue must be with path (form redirection URL on submission), try with full url.
Can you share the URL at which you have put your form ??
edit your php file with template.
Ex. http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/
<?php
/*
Template Name: Customer Details Page
*/
get_header();
?>
// your php file as it is.
<?php get_footer(); ?>
now create new page with "customerdetails" name in wp admin & select "Customer Details Page" in right side column & save.
now your form action path will be as below
<form method="post" name="details" action="<?php echo get_site_url(); ?>/customerdetails">
Now your form is getting submitted & customerdetails page also receive post data.

How does a PHP page retrieve form data?

I am new to PHP and am taking the PHP course on W3Schools. In the form handling part of the course, I read that there are two methods of handling form data: POST and GET. I understand that depending on the method used, a superglobal variable is created which stores the data as key-value pairs. And at the destination page, the data is retrieved from this variable (using $_POST["key"] or $_GET["key"]).
But my question is: Where is the superglobal variable ($_POST or $_GET) stored? If it is on that same page on which the form exists, how can it be accessed by another destination page specified in the action attribute of the form tag?
Is it that the same set of these superglobal variables are accessible by all the pages on the server (contrary to my present belief that each page has its own set of those)?
The code below should make my question more clear:
File index.php
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
File welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
W3Schools explains as follows:
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.
To display the submitted data you could simply echo all the variables.
How do the variables $_POST["name"] and $_POST["email"] reach welcome.php?
Superglobals do not refer to the lifetime of a variable, but the scope in which the variable can be accessed.
Session variables can be accessed by other pages.
Also, regarding your comment "If that is the case, how can the second.php display the form data entered in first.php?"
first.php should save data to a database. second.php should read data from the database. If you are very new, I would suggest looking in to MySQL as a database. It plays very well with PHP.
When you send a request to a web server (i.e. apache, nginx), the request is parsed into pieces to be understood. Then, in PHP's case, the file requested is sent to the PHP interpreter with the data from the request.
In your example, you want a form to send a POST request to the page welcome.php. (If you're new to requests methods GET/POST/etc, then w3schools has a simple explanation on that: http://www.w3schools.com/tags/ref_httpmethods.asp) Your form has the method set to post, this can be get but it generally never is. If a user pressed a submit button within a tag, then the form sends to the request to the page set in action using the method set in method. Your form will send a POST request with the data name="..." and email="...".
When this request reaches PHP, all data sent to it will be stored in the method global variable. All post request place their data in $_POST, all get requests place their data in $_GET.
So when you submit your forms POST request, the page welcome.php will be requested by your browser (Firefox/Chrome) using the method POST and sending the data name and email in that request. On your server, PHP will open the file welcome.php and place the data into the $_POST array. Then, since $_POST is global, anywhere within welcome.php will have access to this variable.
If you manually go to the page welcome.php, you will be performing a GET request. Generally, only way to send a post request is through a form. So manually going to that page will have $_POST set to an empty array.
When I was learning PHP, I almost always had these lines at the top of the page so I knew what was going on:
<pre><?php print_r($_GET);?></pre>
<pre><?php print_r($_POST);?></pre>
This will print out the arrays in a pretty format, and the tags will make sure they're formatted correctly in the browser (since print_r uses newline characters, and browsers ignore those unless in pre tags).
You could also swap print_r with var_dump to get way more info about the variable.
This is a simple answer, there's way more to web technologies. And once you understand PHP enough I'd move into frameworks like CakePHP or CodeIgniter which takes away a lot of the little bits. It's important to understand the basics first though.
Regarding your edit:
<form action="welcome.php" method="post">
The method="post" bit means that when the user hits "Submit" (or whatever triggers action="welcome.php") the data will be collected from the form element and sent as POST data to the action target -- since it's a php file, you can then access this data through the superglobal, $_POST, as you've noted.
The name property in each of the inputs inside the form sets the key for each element of the POST data that is sent, and whatever the user entered in the input will be the corresponding value to that key.
For instance, if the user entered "foo" for the name, and "bar" for the email, your $_POST data in welcome.php should look something like:
Array
(
[name] => foo
[email] => bar
)

Php post function with header()

I'm new to PHP. I want to send data from one page to another page by post.
if(isset($_REQUEST['submit']))
{
header("location:nextpage.php");
//here i want to send data coming from my text box's by post function
}
You should just target the right page to begin with.
<form action="your-processing-script.php" method="post">
Keep in mind the action is relative, so depending on your file structure, you may want a slash before the file url to prevent confusion.
/your-processing-script.php

what is meant by action="#main_body" in a HTML form

I have form and some fields and I want send these fields to the next page via done.php using action="#main_body".
What are the differences between these two forms?
<form id="formElem" name="formElem" action="/ifs/form/index.php" method="post">
<form id="formElem " class="ifs" method="post" action="#main_body">
The complete action of the form is the URL of the page containing the form at the time of loading the form + the hashtag. So submitting the form will load the same page, but with a ahashtag (anchor) of #man_body. This is a side effect of action attributes being realtive if not definitly given as absolute.
Please be aware, that it is browser-dependant and header-dependant wether the page will actually reload or just scroll.
in the first case you send the values of your inputs to a specific page called done.php. In the second way you're calling the same page in which you have your form (plus an hashtag)
In the second link you are calling the same page with a hashtag of "main_body". it will work something like a 'TOP' link provided in lengthy pages which scrolled back to top of the page.
a difference is here the page will scroll(or reload) to "main_body" when you submit the form.

how to submit a form and show the result in that page

im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though

Categories