Transfering form data in the url with post and get - php

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>

Related

How to save form post to session and stay in the same html page?

I have a form in html that post income to incomesave.php where session is initiated and save all the form contents to session.
The problem is that I want to stay in the same page when submitting form all the while saving data to session and use that session values to populate the income history on the same page. How can I stay in the same page and process the data with php?
This is the form inside html:
<form action="lib/php/incomesave.php" method="post">
$: <input type="number" name="price">
type: <input type="text" name="source" >
<input type="submit" value="Submit">
</form>
<div class="row">
<div class="twelve columns">
<div class="title">
Income history
</div>
<div id="incomehistory">
</div>
</div>
</div>
and this is the incomesave.php to save the form data
<?php
session_start();
$price = $_POST['price'];
$source = $_POST['source'];
$_SESSION['price'] = $price;
$_SESSION['source'] = $source;
echo $_SESSION['price']; // print price
echo $_SESSION['source'];
?>
I want to stay on the same page, run the incomesave.php and populate the income history area of the html page with $_SESSION data. How can I do that? any help would be appreciated!
Submit a AJAX requet to the server and modify the DOM with the result.
xhttp.open("GET", "incomesave.php", false);
xhttp.send();
document.getElementById("incomehistory").innerHTML = xhttp.responseText;
Use ajax to post the data form without changing the page and after the ajax call, save the data inside incomesave.php and after that, use callback or success method to change the html using .innerHTML
thank you to all the comments to lead up to the answer!
If you don't want the page to refresh you have to use ajax to complete this. I'm not gonna post a code example since I don't know how familiar you are with ajax post.
Do a research on ajax posting and result processing. If you are familiar with ajax post, let me know so I can do some code example..
IF you just want to stay on same page, check the answer I posted earlier on sessions
Simple session example not working
if you set form action it will redirect you to action url page. On that page you initialize all session data and then redirect it to same page using header like
header('URL');
url will be html page url.

PHP $_POST form with $_GET after submit

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.

How do I submit my form using href?

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>

php post via html <a> tags

I have built a site using php and want to try keep it one page.. The site displays pictures and so far i have it making links from folders in a folder each folder contains images so what i want is to make a post/get tag in the url and when the page loads it uses this to get the images from that folder.
So I want to use the generated links to post to the same page with a value via self_post is this possible and if so how?
my get section is
if(empty($_post['foldername']))
{
$directory = "portfolio/homepage/";
}
else if(isset($_post['foldername']))
{
$foldername = $_post['foldername'];
$directory = "portfolio/".$foldername."/";
}
and my link is like this
echo '<li><a id="" class="" href="'.$_SERVER["PHP_SELF"].'">'.$b.'<input type="hidden" name="foldername" value="'.$b.'" /></a></li>';
Thanks
What's wrong with GET?
Click me
The only way to make a POST request using a <a> tag would be to have it submit a form via javascript:
<form method="post" id="hidden_form" name="hidden_form" action="script.php">
<input type="hidden" name="foldername" value="<?php echo $b ?>" />
</form>
...
post me
You can also update the values of the hidden element(s) from javascript as well so when you click a particular link, it sets one of the values to something specific to the link.
The only way is doing it through JS. You can either send an AJAX request specifying POST, or you can create a hidden form and submit it. Here's an example
document.getElementById('my-link').onclick = function(){
// Code to submit the hidden form or to send an AJAX request specifying POST
return false; // to prevent the default behavior
}
I know of no way to do this with vanilla anchor tags. You could establish click event handlers in javascript to submit an XHR request. However, I have accomplished this in the past by using multiple form tags with a single submit entity (<input type='submit', <button type='submit', etc.).
Your forms would look like so:
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="hidden" name="foldername" value="YOURVALUEHERE">
<input type="submit">
</form>
Like drew010 said if you absolutly need the POST method. Otherwise most single-page website uses things like index.php?a=1&b=2 where you can get "a" and "b" with $_GET["a"] ...

Pass javascript array to server using jquery .post on submit and then print array in PHP?

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)

Categories