Understanding Self Referencing forms - php

I am trying to learn Self Referencing Forms. I have read that a html form embedded in a php script is a self referencing form. I am still unable to pick the concept. Almost all the forms that I see in php codes are built with html. Is there something more specific about Self Referencing forms then just html forms being embedded in php script?

You probably mean something like this:
<?php
if (count($_POST)) {
echo 'You have submitted the string: '.$_POST['string'];
}
?>
<form action="" method="post">
<input type="text" name="string">
<button type="submit">Submit!</button>
</form>
The empty action attribute causes the browser to submit it to the same URL as the one that is loaded. Via count($_POST) we check whether the form was submitted and act accordingly.

Related

PHP Form not posting the submit button correctly

I was doing an application form and the form is not working. When I click the submit button it doesn't even run the if statement to POST the variables.
My form tag looks like this:
<form method="post" enctype="multipart/form-data">
My submit button looks like this:
<button name="submeter" type="submit" class="btn btn-gfort">Submeter</button>
And the if statement in my form looks like this:
if(isset($_POST['submeter'])) {
I even tried to run a JS alert just to see if it actually enters the if statement but it doesn't. No console errors as well.Any help is appreciated
Have you added method="post" to the <form>-tag?
Well your codes looks fine. it seems you are submitting the form using post method on some php file. Please share the complete form element and the codes of the target file that you have defined in the action attribute of form tag.
the submit button sends the data to server and there you can process the data using any server side scripting language. your codes must look something like
<form action="process.php" method = "post">
<!-- your form controls -->`
<button name="submeter" type="submit" class="btn btn-gfort">Submeter</button>
</form>
then create another file on same location where html/php file containing this form is saved with name process.php, in which you can use following codes.
if(isset($_POST['submeter'])) {
// php codes
}
in case you want to subimt the form on the same page. make sure your file is php use action="#" in form tag and place the file of your webserver as you can not run php files directly from your filesystem but you need to run it through your webserver

Basic form in Wordpress template

I need to simply get a search term from a form into a variable. I have set up a basic form within a template file, that template file is then linked to a page within Wordpress admin. The problem I am getting is that the form doesn't submit so I am unable to use the variable. If I remove get_header(); from the template then the form will submit but obviously it break wordpress stuff.
Here is my form:
<form action="<?php the_permalink(); ?>" method="post" autocomplete="off" >
<label>
<input placeholder="Search…" name="qcsearch" type="text">
</label>
<input type="submit" name="submit" value="Submit">
</ul>
</form>
I have tried leaving out the action, using the template name which is qccerts.php and using $_SERVER['PHP_SELF']
Here is then what I am trying to do with the output:
if(isset($_POST['submit'])){
$searchterm = $_POST["qcsearch"];
}else{
$searchterm = '';
}
Its basically a simple search which tells the users if there is a file by the name they search. So I need to populate $searchterm so I can use it later down the page.
Any help appreciated.
It's difficult to determine what your exact problem is without a reproducible scenario. For example, without seeing your problem, I'm not sure whether the form is really not submitted at all, or submitted, but you did not see it being executed, or there is some Javascript which prevents your form from submitting. There is a possibility that the form is submitted to the wrong action as well.
However, if you intend to keep your search term accross the pages, you could add it into session. Let's imagine these functions:
function storeSearchTerm($searchTerm) {
$_SESSION["searchterm"] = $searchTerm;
}
function getSearchTerm() {
return isset($_SESSION["searchterm"]) ? $_SESSION["searchterm"] : "";
}
By calling these functions you can manage the search term, initializing it via storeSearchTerm($_POST["qcsearch"]) or something.
As about your actual form, if it does not work, then you can submit the form in Javascript, such as
document.getElementById("myForm").submit();
and make sure that this is triggered either via an onclick attribute, or a click event listener on the button created via addEventListener.
EDIT
It turns out that a class name was not well formed (case-sensitivity issue).

Processing Form Data using GET Request in PHP

I'm new to php and I am writing code to get form data using get method. Following is my code in index.php file.
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="index.php">
<p>Enter Name</p>
<input type="text" name="fname" />
<input type="submit" />
</form>
<?php
if($_SERVER['REQUEST_METHOD'] === 'GET')
{
$name = $_GET['fname'];
print $name;
}
?>
</body>
</html>
when I run my code, it calls the php code before submitting form. How can I process the data using GET Method without creating a new php file.
it calls the php code before submitting form
Because when you load a page, that's a GET request. And the code explicitly states to execute on a GET request.
How can I process the data using GET Method without creating a new php file
You'd need to determine the difference between when the page is loaded and when the form is submitted. If the form must use GET then the request method isn't that difference. One option could be to check for the existance of a submitted value. For example:
if (isset($_GET['fname'])) {
// your code
}
A common approach would be to use the name of the submit button being clicked as well, which can also be used to distinguish between different buttons in the same form. But any submitted value will do.

form type="url" POST to PHP

So I have a form that requires a user to submit their website to a form. Here is the html line:
<input type='url' name='link'>
And I'm using <input type="submit" value="submit" formmethod="post"> to submit the form to a php
And I'm trying to retrieve the values in my php file with:
$link = $_POST['link'];
Why isn't this working? At first I thought it was because I had htmlspecialchars() but it's not coming through without it either. I can't find anything in any google search that even mentions anything related to this kind of problem (with a type="url" form)
What do I need to do to process form data with type of "url" in PHP with a $_POST?
Get your form method to be set to post e.g
<form method=post>,
if you submit the form and in the url in your browser u can see some more inf then be sure 2 check your form method
I think this is wrong,
method="post"
Its only method, not formmethod
Also make sure, you dont have one more for element name with link.

Send values from HTML Form to PHP

How do I link the PHP to the HTML form? I understand how to do the PHP and how to do the HTML, but how do I link the php to the html or is that automatic,
how does the HTML form know about the PHP?
In your form set the action attribute to the path of your php script eg:
<form action="/path/to/php/script.php" method="post">
...
</form>
You set your action="" in your form to point to your PHP script. When the user clicks the submit button in your form, the PHP script will be called and the formdata will be handed over to the PHP script.
The method you choose when making your form is how PHP will gather the values passed in.
As such:
<form action="handler.php" method="post">
<!-- OR -->
<form action="handler.php" method="get">
The action tells where the form values will be sent to and the method tells how the values of the items in the form will be passed back to the server. The post method will send the values back so they may be retrieved by the $_POST array (both post and get can be retrieved by the $_REQUEST array). For example:
<input type="text" name="myInput">
Will post back to the server and can be retrieved by
$var = $_POST['myInput'];
It's always best to test if there is actually an input, and the following can be used
if(isset($_POST['myInput'])) { /*do something if set*/}
else{ /*do something if not set*/}
If the form was submitted by the get method, the values of the form is passed back in the URL, like such:
http://www.domain.tld/handler.php?myInput=someValue
The value is then retrieved by using the $_GET array:
$var = $_GET['myInput'];
Once again, you should test that it exists.
For good examples and explanations, please read a PHP book or search for PHP and HTML forms. This is the very basics of PHP.
Try this in a php file
<form action="" method="post">
<input type="text" value="html form data" name="name" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit']))
echo 'I am php. I know this value is from html - '. $_POST['name'];
?>
If you are talking about refilling your form with the php values: inside your input fields, just add the request variable.
<input type="text" name="input1" value="<?=$_REQUEST['var_name']?>" />
If you are talking about sending date to php, just point to a file using the form action.
<form action="file.php" method="post">
</form>
Then you process all the data in that php file.

Categories