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

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.

Related

Different between actions

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).

Form submit takes to Search page

Any idea why submitted form data loads the search.php page instead of submitting form?
URL: http://domain/contact-us/
On form submission it redirects to: http://domain/?s=&wpforms%5Bfields%5D%5B0%5D%5Bfirst%5D=iuiou&wpforms%5Bfields%5D%5B0%5D%5Blast%5D=uoiu&wpforms%5Bfields%5D%5B1%5D=amigoow%40yahoo.com&wpforms%5Bfields%5D%5B6%5D=General+Feedback&wpforms%5Bfields%5D%5B3%5D=7987&wpforms%5Bfields%5D%5B2%5D=ou&wpforms%5Bhp%5D=&wpforms%5Bid%5D=87632&wpforms%5Bnonce%5D=df749058ef&_wp_http_referer=%2Fcontact-us%2F&wpforms%5Bsubmit%5D=wpforms-submit
I have changed the plugin from ContactForm7 to WPForms but it turns out it's something not related to plugin.
Well your form opening tag looks like this:
<form method="get" action="http://newdev.propakistani.pk" role="search">
So its doing exactly as per this tag. You need to amend action to the URL of the php file that processes the form.

How to return on the same page after submitting a form in php.?

I am making an e-commerce website where I have lots of products. If a user goes to any product items page and submits any form there then they should come on the same page.
So, how to come on the same page?
On the formular target page set:
header('Location: http://www.example.com/same_page');
Leave action attribute of form blank. Like so:
<form action="" method="post">
Or
<form action="#" method="post">
On your opening form tag add
action="submit.php"
then once it goes to that page when the submit button is hit add this
to the bottom of that php page:
header("Location: success.html OR success.php");
If you want to submit various forms on same page and then go back to the page where the form is submitted, you must also send the form URL of the page where it was sent, preferably in a hidden element. And after processing form redirect to URL stored in hidden.
You can use this :
header('Location: filename.php);
If you get any $_POST errors put it in a condition: if(isset[$_POST])
Thank You All. I Got My Answer
$_SERVER['REQUEST_URI']; will give the current URL with query strings.
Like my page is 'products.php?Product=20'
echo $_SERVER['REQUEST_URI']; =>/products.php?Product=20
So, we can directly use this in header location.

Convert $_GET variables to $_POST and redirect page

I'm sending info through a link read in an email via $_GET (i.e. link in email is in form http://website.com?dogs=cats"). But I want the site URL to not have the appendages visible. So I've tried:
Linking to a page which saves the $_GET in a hidden form fields, then automatically submits the form; problem is that the back button then leads back to this intermediary page
Same as above, opening intermediary page in new tab, then having the form load another new tab (_blank), and closes itself; works fine, except in IE these are windows, which are annoying
I'm considering saving the $_GET results in a cookie, then redirecting the page with a header(), then extracting data and expiring the cookie.
Is there an easier way that I'm overlooking?
How about starting a session and storing them to the $_SESSION variables?
Here is a sample implementation of how you can make a hidden arguments on links. This sets a custom handler on the links which will copy hidden argument into the form and send it through post request. It is not a substitute to the session, but it can have it's own uses.
<form id="form" method="post" action="">
<input id="dogs" type=hidden name="dogs">
</form>
Sample link
<script>
$(function(){
$('a').click(function(ev){
ev.preventDefault();
$('#dogs').val($(this).attr('data-dogs'));
$('#form').attr('action',$(this).attr('href')).submit();
}
});
</script>

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