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.
Related
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);
}
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
?>
<html>
<script>
function changeText()
{
document.getElementById("input1").value = <?php echo '"'.$_POST['input'].'"'; ?>;
return true;
}
</script>
<form name="mainform" action="" method="post">
<input type="text" name="input" id="input1" />
<input type="submit" onclick = "changeText()" name="Submit" value="Submit!" />
</form>
<html>
i have this code here. can you make it work as intended ?
everytime i click Submit! i want to change the value of the textarea to the last input the user inserted.
PHP code is parsed by a PHP interpreter before any HTML output is sent to the browser.
If your form action is the same page and the same form will be shown before and after submission, then you can let PHP print the value of the input field directly into it.
<input type="text" name="input" id="input1" value="<?php echo htmlspecialchars($_POST['input']);" />
If you're trying to revert the value of this input field whenever a user clicks the submit button, then your code (even if it's prone to code injection) should work but this is useless since the page will be requested again when submit is clicked.
I assume you need to fill in
action=""
By the name of your file, like
action="myFile.php"
Few tips :
NEVER trust the user. The user can manually change the value of the input and send some dangerous values in your $_POST variable. You need to check it using filter_input() by example.
Like #Charles said this is pretty simple problem, use google next time.Here for example
Am using this onclick='document.forms['form_name'].submit(); return false;' but this doesn't work as am having href=results.php?page_no=1 etc, has dynamic links, examples show to make this work I need to use href="#" but any idea how I can submit the form and than navigate to page2? Because I want to save the check box values in a session
Add class to your hrefs (class="pagination") and id (id="form") to your form. Then you can use Jquery framework for this stuff.
$(".pagination").click(function(){
// get page id, set form action with params
$("#formId").submit();
return false;
});
You have a bad design.
You can't perform multiple competing actions on a form click and expect it to work.
You need to either let the link be clicked and let it load another page, or if you are just setting some session variable (although it would be far better to set this with a querystring parameter or a cookie), you can use an Ajax request to send that off asynchronously.
Here I substituted page2 with Google just for test
Submit
<form method="get" action="https://www.google.com/search?q=test" name="test">
<input name="Checkbox1" type="checkbox" />
</form>
edit:
Submit
<form method="get" action="" name="test">
<input name="Checkbox1" type="checkbox" />
</form>
without encodeURIComponent(this.getAttribute('href') the parameters are missed.
Some options:
1) Use jQuery AJAX, serialize and post the form data and then redirect (location.href) on the onSuccess callback. Something like this:
$.post("submitform.php",
$("form").serialize(),
function(data){location.href='results.php?page_no=2';}
);
2) Post the form to a named hidden iFrame using "target" on the form tag. If this is really just a best effort sort of recording you shouldn't need to wait for the page to load, the request should be enough and you can continue to the next page. Something like this:
<iframe="targetname" style="display:none;" />
<form name="myform" target="targetname" method="post" action="submitform.php">
....
</form>
<a href="page2.php" onClick="document.forms['myform'].submit(); return true;">
Click Here
</a>
How do you use a value "submitted" by a form in javascript?
Facts:
It is a PHP document
I'm using JavaScript because I need some timing factors I don't think I can get from serverside-scripts :)
To simplify; what I want is, that when this form is submitted or a button is clicked:
<form method="POST" action="test.php">
<input type="text" name="foo" size="30">
<input type="submit" value="Click me"> //it doesn't have to be submitted
<input type="button" action="some_action" value="Click me"> //an alternative solution
</form>
the value of the text-input named "foo" is displayed elsewhere.
NOTE The form doesn't have to be submitted, what I realy want is, that when you press a button the value can be used elsewhere
Should I use GET instead? Can I just use the $_POST array? Should I use AJAX (which I am completely useless at)? I don't know what to do in this situation.
Since you mentioned that it does not depend fully upon whether the form is submitted or not, so it's more easier to catch the value w/o POSTing / GETing the form. After you have written your interface logic in the body section, you need to write the following code in the footer page at the end:-
anypage.php:-
<form method="POST" action="test.php">
<input type="text" name="foo" id="foo" size="30" />
<input type="submit" onclick="return writeFoo('foo_placeholder', 'foo');" value="Click me" /> //it doesn't have to be submitted
<input type="button" onclick="return writeFoo('foo_placeholder', 'foo');" action="some_action" value="Click me" /> //an alternative solution
</form>
The above code is your code only with some minor modifications, including calling a JS function "writeFoo()" on the "click" event of either a button / submit. This function takes 2 arguments:-
arg - It mentions the destination placeholder ID of the HTML element, in which the value is to be printed.
source - It mentions the source ID of the HTML element, from which the value is to be grabbed / taken.
rightpart.php:-
<div>
<span id="foo_placeholder"></span>
</div>
The above HTML code can be used for any panel, but must be included when the "anypage.php" page is to be shown to the user. This is because the placeholder element must be present when the "foo" element is being called. Be careful to use the same ID both in the "writeFoo()" function calling time & in this page.
footer.php:-
<script type="text/javascript"><!--
function writeFoo(arg, source) {
if(document.getElementById(arg) != null) {
document.getElementById(arg).innerHTML = document.getElementById(source).value;
}
}
// --></script>
And this page should contain the above JS code containing the definition of the "writeFoo()" function.
EDIT, as for #Latze:-
See you can include that "rightpart.php" page either in the same block of "anypage.php" page or in any block of any other page (like "header.php" / "footer.php" page). But the main logic is that both the source ID (from which the value is taken) & the target / placeholder ID (where the value is to be shown) must be present when you are viewing that particular page (in this case, it means when you are viewing the "anypage.php" page).
Hope it helps.
You can read the value from the $_POST or $_REQUEST array on the server side, and insert it into the output anywhere you like - even inside javascript, if you want to. Example:
<?php
$myValue = $_POST['foo'];
?>
<script type="text/javascript"><!--
function writeMyValue() { document.write('<?php echo $myValue; ?>'); }
// --></script>
PHP runs on the server. JavaScript runs on the browser. These two languages do not talk to each other; they don't even run at the same time, not to mention on the same machine. As soon as the user submits the form, the browser requests test.php from the server and the current page is gone forever, scripts and all.
It's really hard to figure out what you want to do exactly, so I'll provide you with some general hints:
JavaScript can intercept a form submission. You need to attach an onsubmit event handler to the <form> element. The function assigned to the event can do whatever it needs and then return true (and let the submission go on) or return false (and cancel the submission).
JavaScript can read and write almost any page element. You need to use the so called DOM methods.
PHP can generate whatever you need, including HTML input fields.
Example:
<?php
$foo_value = date('Y-m-d H:i:s');
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<script type="text/javascript"><!--
window.onload = function(){
var documentForms = document.getElementsByTagName("form");
for(var i=0, len=documentForms.length; i<len; i++){
documentForms[i].onsubmit = function(e){
var currentForm = e.target;
var fooValue = currentForm.elements.foo.value;
var p = document.createElement("p");
p.appendChild(document.createTextNode("Aborted submission: " + fooValue));
currentForm.appendChild(p);
return false;
};
}
};
//--></script>
</head>
<body>
<form method="POST" action="test.php">
<input type="text" name="foo" value="<?php echo htmlspecialchars($foo_value) ?>" size="30">
<input type="submit" value="Tryk her">
</form>
</body>
</html>
Update
A little note about this:
documentForms[i].onsubmit = function(e){
};
When you assign an event handler, the spec requires that whenever the function gets called it will receive an event object as its first argument. That object represents the event that triggered the function call and it can be used to obtain additional information, such as the original DOM node that triggered the event. It doesn't matter how you call it inside your function; I use e because I never know how to name stuff :)