PHP Form without additional File - php

Is it possible to to have a PHP form without having a seperate file,
e.g. Normal Form
<form action="send.php" method="post">
But instead of calling send.php have the code held in send.php in the same file as the form?

Sure, just supply the action attribute with an empty action
<form action="" method="post">
or with a $PHP_SELF call.
<form action="<?php echo $PHP_SELF;?>" method="post">
Both will submit to the current page.

Related

Where do I place the PHP file in Wordpress that I want to run on a form submission

I have a form on a WordPress page as follows:
<form id="submissions-form" action="submissions-upload.php"
enctype="multipart/form-data" method="post" name="submissions_upload">
...
</form>
Where should I place my submissions-upload.php file?
I specifically want to run the submissions-upload.php code, not code on my page.
I don't know where wordpress pages are meant to be stored. All I know is that it doesn't work on the root page.
You can use same page ... Like as
<?php
if($_POST){
print_r($_POST);
}
?>
<form id="submissions-form" action="#"
enctype="multipart/form-data" method="post" name="submissions_upload">
...
</form>

Keeping Current URL after form submit PHP

Say I have e.g: (foo.com/index.php?a=1) and a form on it to submit data, but when the submit is hit the url refreshes and it gets rid of the values from the attached.
How do I make the url on my page to stay same as before even when the sumbit button is clicked.
Thanks
Like this: action="yourpage.php?var1=value1&var2=value2". You can get these with PHP like so: $var1 = $_GET['var1'].
Example:
<form method="post" action="yourpage.php?var1=value1&var2=value2">
[...]
</form>
You can do this to add existing variables into the url:
<form method="post" action="yourpage.php?var1=<?php echo $_POST['var1'];?>&var2=<?php echo $_POST['var2'];?>">
[...]
</form>
<!-- Output:
<form method="post" action="yourpage.php?var1=value1&var2=value2">
[...]
</form>
-->
Change the form action in the HTML to post. i.e.:
<form action="..." method="post">
Assuming its a get at the moment. Then set action to your URL

How to post a form to self?

I have a page which is accessible via a url - something like this http://websitename/index.php?q=pagename. On this page, I have a form that I would like to submit to the same page so that I could do some post processing. I have tried the following things but couldn't get it to work -
<form name="formname" action="pagename.php" method="post">
<form name="formname" action="" method="post">
<form name="formname" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
In other words, I would like to submit the form to pagename.php file (where the form resides). The page (pagename.php) is not directly accessible. How do I get this done? I will greatly appreciate any help on this one. Thanks.
Try this:
In HTML5 you can just do <form> and its set to self.
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
OR
<form name="formname" id="mainForm" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI'];?>">
OR
<form method="POST" action="<?=($_SERVER['PHP_SELF'])?>">
The point is your $_SERVER variable contains all the information you would require to generate that specific page's URL. If you have decided not to go with the HTML5 way, then one of these options should work. Otherwise you're missing something else not mentioned in your question.
i think this will solve your problem
<form name="formname" action="#" method="post">
Try
<form method="post">/*your stuff here*/</form>
and you should add a submit button with attribute "name=submit" so the form could be submitted also track if its have been posted with php then do your actions like below
if(isset($_POST["submit"])){ /* your code */}

Issues in working of PHP form submit with GET data

I am working with PHP forms.
The opening form tag looks like this:
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
This generated the following HTML:
<form name="adv_search_form" action="index.php?&rel=common_listing&module=company&sort_option=&search_option=&show_max_row=15" method="GET">
When I submit this form, I land on a URL:
http://localhost/projectcode12may2014/ampanel/index.php?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
But what I want is that
?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
should be appended to the action URL on the landing page. Can anyone suggest any good method for doing this?
Thanks in advance.
If you are using method="get" the parameters in action are overwritten. Only what is in the from will be transmitted.
You need to add hidden fields for all these values. For example:
<input type="hidden" name="module" value="company" />
That is your from data.
You have method="get" in your form tag. This means that the form data is concatinated to the URL. Use method="post" to have send form data as a POST request and keep your URI clean.
So change this line
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
to this
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="post">
Then you can access your form data in PHP with the global $_POST var which holds an array with the submitted data.
This is a normal behavior. Using GET method discard all parameters specified directly in action attribute.
Use POST method instead or move parameters from action attribute of the form to input fields with type="hidden".

html form action="." not submitting to self, but instead submitting to root directory

I have a form in one of my pages and I want to make it submit to the same page. I am doing this:
<form method="POST" action=".">
But when I submit the form it submits to the root directory of my site instead of to the same page the form is on.
I know that the form is being submitted because I tried temporarily changing the method to GET and when I submitted the form, the URL showed the get variables.
Why is the form not submitting to the current page?
If it's relevant, I'm using PHP with xampp.
Try this:
<form method="POST" action="">
This is what I do
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
I'm a newbie though so if Juampi's answer works too , I'd go with it.

Categories