form action attribute set to? - php

When is the need to set from action attribute to ? like this
<head>
<title></title>
</head>
<body>
<form action="?" method="post">
<div>
<label for="joketext">Type your joke here:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40"></textarea>
</div>
<div><input type="submit" value="Add"/></div>
</form>
</body>

The need for setting the form action is so that the form can be submitted to whatever action you dictate, if you leave the action blank then the form will submit to itself (the same page it is on)
If you had a form handler which was not visible but handled all the processing, then you could define the handler address (url) in the form action or even send the data to another page if you so choose.
And wherever you sent it to, a form handler or itself or another page, that would take care of the data and deal with accordingly, as you so choose.

If you use:
<form action="myform.php" method="post">
Then the form redirects to myform.php And in this file there is the code that checks the form.
If you use:
<form action="myform.php?check" method="post">
Then the form redirects to myform.php but it also adds check to the $_GET array.
So you can write a piece of code that only works if there is a check element in your $_GET array.
if(isset($_GET['check']))
{
// your code here
}
In PHP every element after ? is a member of the $_GET array
For example: http://www.example.com?product_id=1&product_name=acme means that the $_GET array currenty has two elements:
product_id
product_name

I guess the below link should help.
http://www.w3schools.com/tags/att_form_action.asp
It allows you to specify where you want to post your form data

when you want form data to be stored, you can set it to a php file and save the data to a database, text file, or xml.

Related

php get data from another form using a different form

Is it possible to get data from another using a different form?
I don't want to use one form
<?php
echo $_POST['2'];
?>
<html>
<head>
</head>
<body>
<form method="post">
<input type="submit" name="submit" />
</form>
<form method="post">
<input type="text" name="2" />
</form>
</body>
</html>
No, that's not possible because browsers will only ever submit one form at a time (the one containing the clicked submit button, typically).
They can't possibly submit multiple forms at once because each form has its own action and method attribute which determines the request to send.
As #peter said, you can submit only one form at a time. But there are some workarounds for your needs.
Method 1
Post your form to a php script(say form_1_action.php) and then store the form input in a Session variable.
$_SESSION['form_data_1'] = $_POST;
Then you will be able to access it in different pages. Like,
$_SESSION['form_data_1']['field_name']
Method 2
Post your form to a php script(say form_1_action.php) and then store the form input in a PHP variable.
$formData1 = $_POST;
Then you can use the data from the first form in the second form (the second form should be on the same file form_1_action.php) like
<input name="name" value="{$formData1['field_name']"}>
You should pass the data from the first form in a hidden field on the second form if you need it on the form_2_action.php.
Method 3
Use Javascript to accomplish your requirements in a more userfriendly way.
try using jquery to Prevent the other form from submiting and try updating the value using event listening of the first form and update that input.
$( '#Submit' ).click( function ( event ) {
event.preventDefault();
var value = <?= $postedValue ?>;
$('input[name="input_name/2"]').val(value);
}

Pass form value to PHP variable using AJAX and redirect

Is it possible for a user to enter a value into a form and then, on submit, have the page redirect to a new one with the value entered into the form stored in a PHP variable?
This if my form code;
<form id="loc-search" method="post">
<input type="text" id="search-by-location" name="custom-location" value="" placeholder="Sheffield, UK"/>
<input type="submit" id="submit" value=""/>
</form>
Once the user has entered a value in #search-by-location the page needs to redirect to weather.php with the value stored in a PHP variable called $location
AJAX / JS aren't my strong suits so if someone could point me in the right direction that would be great
Add the argument action="weather.php" to your form tag. Then, when clicked on the submit button, you will get redirected to that page. Depending on your method, in your case POST, the input values will be available in the superglobal $_POST array in PHP.
In your example, $location = $_POST["custom-location"]; will suffice. Note that the name, not the ID, determines the array key in the target PHP document.
Javascript or AJAX are not needed to achieve this.
This is just a normal form so why not just use $_POST after the redirect on the weather.php page:
$location = $_POST["custom-location"];
As #Tacticus pointed out you also need to have the form redirect (if you did not already do this in JS). By adding action="weather.php" in the form:
<form id="loc-search" method="post" action="weather.php" >
...
</form>
As stated in other answers you should modify your form to look like this:
<form id="loc-search" method="post" action="weather.php">
<input type="text" id="search-by-location" name="custom-location" value="" placeholder="Sheffield, UK"/>
<input type="submit" id="submit" value=""/>
</form>
In your weather.php file, you can get the value from the $_POST global variable, just like this:
<?php
$location = $_POST["custom-location"];
//Interpret data
?>
Note that you can access the value of an input tag from your form witch was passed, with the input's name. In html you specify the following:
<input name="yourname" />
And when you want to access that value, you simply refer to his name.
$_POST['yourname']
If you use GET method for the form to pass the values, then you do the same, only the value will be stored in the $_GET global variable, so in your case with a GET method the variable initialization would look like this:
<?php
$location = $_GET["custom-location"];
//Interpret data
?>

form in subpage doesn't work

I have my page as follows:
<form id="filterQuery" action="#" method="post">
...
<input type="submit">
</form>
<?php
// use $_POST to determine query restrictions and conditions
?>
Now my problem is with the action of the form, because if I make it "#" self, nothing happens when clicking submit.
I'm using a template which shows subpages as include in <div>s. In other words, my page is not a standard HTML page which contain <head> and <body> etc, but is in the following format: basic.php#!/my_sub_page.
Trying to make action "basic.php#!/my_sub_page" results in an empty _POST
I tried your example and everything works fine with /index.php#!/smth in action and post variables.
<form action="#!/something" method="post">
<input type="text" name="var1" value="123">
<input type="submit"></form>
<?php
print_r($_POST);
?>
This is how I check it.
Are you sure you didn't forget name attributes in inputs?
Do you see their values in url after ? if you change method to get?

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.

Send form results to another page php mysql

Hello I have a form working properly using
php echo $_SERVER['PHP_SELF']
The results are shown on same page as form.
I need to be able to send the form data and results to another page (eg results.php) when the user submits the form.
How is this achieved?
Just point the form action to results.php
<form action="results.php">
<!--Inputs-->
</form>
Or if you need to do a redirection store $_GET at $_SESSION['parameters'] .
Here are some attributes of form tag <form> you need to set them..
<form action="controller.php" method="post">
<!--form elements-->
<input type="submit" name="submit" value="Submit Form" />
</form>
Here when ever the submit button clicked, it will submit the form to its action i.e. controller.php by post method (i.e. form elements' value won't display in query string).
Later you can access their value on controller.php by $_REQUEST['element_name'] or $_POST['element_name'] or $_GET['element_name'] according to the form method type.

Categories