I use PHP.
I have a form and after submit I want it to go to a URL with a $_POST variable at the end (as a $_GET), like this:
http://example.com/page.php?my_key=my_value
The problem is that "my_value" is created witin the form which means it does not know about it before the form is posted. Any ideas?
<form method="post" action="/page.php?my_key=">
<input type="text" value="my_value" name="my_key">
<input type="submit" name="submitter">
</form>
After sending form in PHP you can simple make 302 redirection to url you want.
In PHP file page.php (or in other file that is front controller) you can simple do:
if (isset($_POST['my_key'])) {
header('Location: http://'.$_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI'].$_POST['my_key'],true,302);
}
Use Javascript to change the action attribute.
I.E. Using jQuery:
jQuery('form').submit(function(eve) {
var action = eve.target.attr('action');
action = action + eve.target.find('input[name="my_key"]').val();
eve.target.attr('action',action);
});
A little rough, needs to be checked and maybe debugged.
You can Not pass an POST value through an url :
what you can do is something like this:
<form action="/page.php?my_key=" name="pre" method="POST">
<input type="hidden" name="my_key" value="my_value">
</form>
<script type="text/javascript">
setTimeout("document.forms['pre'].submit();",0);
</script>
now this will act as an link and will auto submit form as an POST.
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);
}
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>
i have two submit button on my index page namely International and Domestic. i want that two different button to point to different pages namely int.php and dom.php when i click on the buttons. can you help me out. thank
while it is allowed only to define single action = "" for form element. but if i have to do that, i would do it this way.
<form action ="somepage.php" method="post">
<!--all your input elements-->
<input type="submit" name ="international" value="international"/>
<input type="submit" name ="domestic" value="domestic"/>
</form>
determine which button have been clicked and act accordingly.
if(isset($_POST['domestic']) {
//include dom.php
}
else if(isset($_POST['international']) {
//include int.php
}
and then you can include the necessary file.
or the other way is to go with AJAX/jQuery way
you can just use switch in php for differ or
use javascript
Do it with jquery! First, dont create submit buttons just create
<input type="button" />
Than give them an id like:
<input type="button" id="InternationalBTN" />
<input type="button" id="DomesticBTN" />
and with jquery bind the action
$(document).ready(function(){
$("#InternationalBTN").bind('click',function(){
$('#idOfYourForm').attr('action','setTheDestinationPageHere');
document.forms['nameOfYourForm'].submit();
});
});
That will not be possible since your form's action attribute can only point to one location at a time and both buttons are in the same form(but possible if you use AJAX).
If you wanted to use pure PHP (i.e. no Javascript involved), you'd have to write two different handlers for the different button clicks, like below:
if(isset($_POST["name_of_international_button"])){
//actions to perform for this --
}
if(isset($_POST["name_of_domestic_button"])){
//action to perform for this
}
In the actions part of each of the handlers, you could then do a redirect, with the URL containing the data to be processed either in the int.php or dom.php scripts
You can do it in this way:
In form tag please leave empty action action=""
2 buttons to send:
<input class="btnSend" type="submit" name="International" value="International" id="International"/>
<input class="btnSend" type="submit" name="Domestic" value="Domestic" id="Domestic"/>
and use ajax:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function(){
$('#International ').click(function() {
// send to file 1 using ajax
});
$('#Domestic').click(function() {
// send to file 2 using ajax
});
});
</script>
Here is how to send data using ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Your form action would have to contain some sort of conditional statement to redirect users based on which submit button is clicked
<form action="<?php if(isset($_POST['international'])){echo 'international.php';}else{echo 'domestic.php';}?>" method="post">
<input type="text" name="field1" />
<input type="text" name="field2"/>
<input type="submit" value="international "name="international"/>
<input type="submit" value="domestic "name="domestic"/>
</form>
Or you could set up your conditionals on a page specified by the form actionand have them redirect based on which button was clicked,
Just put a form tag, and set the action to the page. Then the submit button will navigate to that page where the action tag is pointing to...
Easy as that :D
HTML:
<form id="continue" action="summary.php" method="post">
<input type="submit" value="Continue" >
</form>
JS/JQuery
$('#continue').submit(function(){
var data = ["jon", "steve"];
var nameArray = JSON.stringify(data);
$.post("summary.php", { nameArray: nameArray });
});
PHP
$name = json_decode($_POST["nameArray"]);
print_r($name);
When I click the "Continue" button, I want to be redirected to the summary.php page and list the names. Currently, I get an "Undefined index: nameArray error when the summary.php page is loaded.
I checked firebug and there is no POST. So it looks like nameArray isn't even being posted either. What's the solution?
This is not the correct way of handling data.
You are posting a page (leaving it) and doing an ajax post within? If you want to stay on the page, you use an ajax post. If you are posting data normally, you use a form post.
If you want to post the name jon or steve as an array you can use the following html
<input type="hidden" name="nameArray[]" value="jon" />
<input type="hidden" name="nameArray[]" value="steve" />
If you want to use ajax, then just dont do it on the submit of a form. You can do it on a submit, but then return false at the end of the function so the page will not be submitted (you will stay on the current page)
I have a problem. I need to transfer a message that a user types to the same page (cause I need to save it to the database). I have a form and a 'submit button'.
If I use the command 'post' I am able to save transfer the data to the same page and save it after extracting the post variables.
Problem is that I dont send the page_id in the url (I could acheive it with GET method, but I cant use it, cause the users message could be too long to go into the url).
I tried to do some javascript code, to transfer the 'submit' button to simple button and do a redirect..:
<script type="text/javascript">
document.getElementById('messageSent').addEventListener('click', function () {
window.location = 'article.php?articleID=<?php echo $_REQUEST['pageID']; ?>';
}, false);
</script>
But then the POST variables are lost.
Basically, what I want is to achieve the effect of post and get, I want to post the the variables while encoding the url to look like this:
home.php?pageID=<?php echo $_REQUEST['pageID']; ?>
The page should repost to itself as post request with the pageID set..
How do I achieve that?!?
You'll have to dynamically build a form, populate it with the POST values, and submit it using JavaScript:
<form id="myform" action="target.php" method="post">
<input type="hidden" value="<?php echo $_POST['pageID']; ?>">
</form>
<script>
// ... eventually....
document.getElementById("myform").submit();
</script>
You should use an URL containing the page id in action of form. For example:
<form action="home.php?pageID=<?php echo $_REQUEST['pageID']; ?>" method="post">
</form>