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);
}
Related
I have an input like this:
<input value="<?php echo $formdata['title'] ?>" type="text" name="title" id="Editbox2">
This is an edit page, I load database data into fields with echo, replace them, and hit submit to update them.
But when I hit submit it refreshes the old data onto browser's fields, how can I prevent this?
Submit your form using ajax request with jquery submit.
Use action="javascript:;" for the form tag
You need to handle the script with javascript, then prevent the default behaviour, which is refreshing the page. Here is an example:
*I haven't tested this, but from what I recall this is what I used to do. Let me know if it doesn't work, I'll give other suggestions.
<form>
<!-- elements inside -->
<input type="submit" id="submit-btn" value="Submit"/>
</form>
and in your javascript have the following:
<script>
$("#submit-btn").click(function(e){
e.preventDefault();
// handle form here with your JS
});
</script>
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.
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.
How can I submit a form to itself without clearing the data in the fields using HTML, javascript and PHP?
You could take different approaches (e.g. cookies, jquery, etc...), however HTML + a line in PHP are more than enough in this case. Try this example code:
<form name="test" method="post">
Your Name: <input type="text" name="YourName" <?php if (isset($_POST['YourName'])) echo 'value="'.$_POST['YourName'].'"';?> >
<input type="submit" value="Submit">
</form>
In the code above if something has been posted to the receiving page (that can be the same page, such as in your case), then the posted value is printed out in the corresponding field. You can use this approach for all the fields composing your form.
If you want, you can also use similarly the $_GET method in the form.
If you use the traditional form submit, you need to save the parameters and rewrite the form input elements when you write the form the next time. But a better way is to use AJAX -- then the field data is sent without a form submission, and the input elements retain their data. See this link: http://www.w3schools.com/ajax/default.asp
Lets say that I have an HTML page "myPage.php" with a form that uses the GET method and calls itself:
<form name="myForm" action="myPage.php" method="get">
<input type="text" name="input1" />
<input type="text" name="input2" />
.
.
.
<input type="submit" />
</form>
And also, in this page and outside the form, I have an anchor that also calls the page itself, but with a GET variable "myVar" added (a GET variable that is not one of the form's variables):
Now... I wish that all the variables will be persisted no matter whether the form was submitted or the link was pressed.
e.g., if a user was pressing the link, the URL will get the 'myVar=100' + the form variables (as if the form was also submitted together with pressing the link), and vise versa - if the user submits the form, the URL gets the form's vars as well as the "myVar", if indeed it was in the URL.
Is there a way?
Thank you :)
Hence, the easy way is to set the links with
Something
also, change the action in your form to
<form action"myPage.php?var=100" method="get" name="form_name" id="form_name">
...
and they will send the same form. Anyhow, you'll deal with form inputs one way or another, right?
EDIT: Well, if it depends on the link clicking, then:
1.) Receive the myVar in PHP
$myCurrval = $GET['myVar'];
2.) Assign it to a JS var
<script type="text/javascript">
var JOHNNY = <?php echo $myCurrval; ?>
</script>
2.) Add a hidden input
<input type="hidden" id="myVar" name="myVar" value="" />
2.) And change the link to
Something
3.) Then, create a JS method called, yeah, processForm(), who will decide if sets the myVal or not.
function processForm() {
if (JOHNNY != ''){
document.getElementById('myVar').value=JOHNNY;
}
}
This way, your myVar value will propagate only if it was received before (it means, the first time you send it, it will persist). Somehow, I would use $_SESSION to keep things between requests! In fact, it is safer and lot easier!!! :)
Hope it helps.