Just learning about passing variables from page to page in php, and trying to find the best way to do so for me, as I have to pass ~10 variables between 5 pages. On the first page, does it make sense to have a form:
<form method="post">
<input type="hidden" name="test" value="<?php $test ?>" />
</form>
Then on the next page could I receive this variable using POST? I would not like to have an ACTUAL form, just use it as a storage area for my variables. Also, what do I use for action= if the second page is called second.php.
Any help is appreciated, thanks
Short Answer: Forms only work when submitted. You probably want to use sessions.
Longer Answer:
It does not make sense to have a form without user input. That's what it exists for.
the action= attribute on a form reflects where the form would be submitted. If the processing page is second.php then the action= attribute should point there.
Sessions are not the only possibility. PHP can also set cookies, and if the server doesn't care about the data (only being used as a medium), you can use HTML5's localStorage.
Really, if you need to be passing 10 variables through all five pages, you're probably better off using sessions. You can store all of them as part of the $_SESSION variable and access them from any page as long as the session is kept alive.
You can't use $_POST variables to store data in a user's session.
You should use:
Sessions
Cookies
HTML5 storage
It depends on what you want to do, you have various options:
1. Using a form and post as you outlined. In this case, on page 1 your action="second.php"
2. Passing the data via URL using GET
3. Sessions, as stated by the previous two posts
4. Cookies
If I understand your question correctly you could try the following to POST your values without having the form appear on the page or actually be on the page at all. You need to have the jQuery library referenced in order to use this code.
function hiddenPost(param1) {
$('<form />')
.hide()
.attr({ method: "post" })
.attr({ action: "http://my-URL-here.com/SomePage.php" })
.append($('<input />')
.attr("type", "hidden")
.attr({ "name": "post_data" })
.val(param1)
)
.append('<input type="submit" />')
.appendTo($("body"))
.submit();
}
You can retrieve the POST values on the page you POST to in the same manner as if it was a regular POST with a form tag.
For PHP:
var postData = $_POST["post_data"];
Hope that helps.
Related
I am working on a project where i need to access clean URL's always i.e no parameters can be passed in the URL for getting values on another page.
Can anyone please suggest an alternative other than 'Form Submit' method.
Thanks In Advance.
Use $_SESSION for this purpose.
Using $_SESSION you can use variable in multiple pages wherever you want.
// assigning variable
$_SESSION['name']="variable";
//retrieving
echo $_SESSION['name'];
write session_start() at the top of page wherever you want $_SESSION variable
For Clean URL's i prefer you may use HTTP POST Method. Hope that helps.
<form name="frmApp" method="POST" action="/action.php">
</form>
Else, you can use AJAX with jQuery to submit the values to another page.
$.ajax({url:"action.php",success:function(result){
$("div").html(result);
}});
Check out w3schools to get started with AJAX : http://www.w3schools.com/jquery/jquery_ajax.asp
No support for SESSION since i don't like writing php code inside my web page.
Sessions can help, or ajax call.
for using clean url you can use post method in form
<form name='' method='POST' action=''>
You can try mapping resources to the url.
Suppose you want to get mailing address of a customer then you can write.
http://[baseurl]/[customerId]/mailingaddress.
Read more here
I am working on a project where i need to access clean URL's always i.e no parameters can be passed in the URL for getting values on another page.
Can anyone please suggest an alternative other than 'Form Submit' method.
Thanks In Advance.
Use $_SESSION for this purpose.
Using $_SESSION you can use variable in multiple pages wherever you want.
// assigning variable
$_SESSION['name']="variable";
//retrieving
echo $_SESSION['name'];
write session_start() at the top of page wherever you want $_SESSION variable
For Clean URL's i prefer you may use HTTP POST Method. Hope that helps.
<form name="frmApp" method="POST" action="/action.php">
</form>
Else, you can use AJAX with jQuery to submit the values to another page.
$.ajax({url:"action.php",success:function(result){
$("div").html(result);
}});
Check out w3schools to get started with AJAX : http://www.w3schools.com/jquery/jquery_ajax.asp
No support for SESSION since i don't like writing php code inside my web page.
Sessions can help, or ajax call.
for using clean url you can use post method in form
<form name='' method='POST' action=''>
You can try mapping resources to the url.
Suppose you want to get mailing address of a customer then you can write.
http://[baseurl]/[customerId]/mailingaddress.
Read more here
I am that kind that spends more time looking for bugs in web projects and correct them, but I still have one question about the use of the GET and POST method
To summarize , I often use the GET method for queries that may be coming from links or simple buttons example :
Click me
and for forms (signup,login, or comment) I use the post method. but the question is:
sometimes (Multi-steps signup for e.g), I may need to pass the info collected from the form in page 1 to a page 2 (where the user can find a captcha for e.g). in order to send them to the database if the captcha test is Okey. But the question is , how to pass those info to a next page via a POST method without using a hidden form?
Do I need to recreate a POST method from scratch with a socket?
thank you
You can use JavaScript (jQuery):
First u need to load jQuery ( using google as host or you download it):
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js"></script>
Then...
<script type="text/javascript">
$(document).ready(function() {
$('#Link').click(function() {
$.post("example.php", { n: "203000"} );
});
});
</script>
<a id="Link" href="#">Click me</a>
Edit:
after that save it in the SESSION in example.php
$ _SESSION['N'] = (int) $_POST['n'];
When this value will be stored on the server side. And tied to the client session, until he closes browser or that it set the time for that session on the server side runs out.
Edit2:
There is also another possibility to post requst, yes ..
But I do not like this method myself ...
And here is the form used, something the OP did not want.
<form name="myform" action="example.php" method="POST">
<input type="hidden" name="n" value="203000">
<a id="Link" onclick="document.myform.submit()" href="#">Click me</a>
</form>
Use sessions to store the data until you submit them:
http://de.php.net/manual/en/intro.session.php.
Using sessions has a big advantage,
once you have verified the data you can store it.
Always keep in mind that users may manipulate POST requests!
If the problem is to pass info between pages like in a multi-step form you should use session (if you are using PHP).
By the way for send a POST request without form you need to use CURL like in this example
The statement is a HTML language statement used by a browser to initiate a POST/GET data relation. The Browser is the execution environment.
You can use other languages (and their execution environment) like Java, Java Script, C#, etc. to initiate HTTP POST/GET data relations.
Sorry if I'm not understanding you correctly, but from what I'm reading, you want to access form data entered on page 1 (using a form with a post method) on page 2? If so, use the $_POST autoglobal array. For example, $nameOnPage2 = $_POST['nameFromPage1']. You don't have to create a form on the second page for this.
EDIT: Answer found! Thank you very much people, a lot of answers worked, I chose the hidden field answer as it was easiest :D
I am creating a commenting script and I came across a problem. I am having to use $_POST and $_GET on the same page, which I don't think makes sense.
I am very new to php and am training myself.
I have a page named viewVerses.php - this has a lists of verses. When someone follows the reply link,
echo '<br />Reply';
I'm passing the verseid (commenting on bible verses) into the reply.php, so that a query may be made with that verseid. (This is so that the user can still see the verse he/she is commenting on).
Now reply.php has the form in it for posting a reply. The form goes to postReply.php
This is in postReply.php
$title = $_POST['title'];
$body = $_POST['body'];
$verseid = $_GET[verseid];
Can I get the verseid from the url and the POST the values from the form in the same page?
If not, is there a way I can do this better? Remember, I am new at php and probably won't implement a solution that is super hard. I have to get it for my to put it in my site.
I hope this is clear
I would add a hidden input to the comment form:
<input type="hidden" name="verseid" value="
<?php echo $_GET['verseid']; ?>
" />
That way, in postReply.php, you can access it using $_POST['verseid'].
Yes you can. The method of a form (in a html page) can be POST and the action URL can contain "GET" arguments being something like process.php?vid=1001 so to say. So in process.php you'll have vid as $_GET and the rest of data from the form as $_POST.
Sure you can, just set the action of the form to postReply.php?verseid=id_of_the_verse this way when an user submits a reply, in the POST array will be the reply related data and in the GET the id of the verse.
Yes, it is possible to mix both GET and POST values with one request. The problem you have is probably that you pass the GET value to reply.php, which then passes POST values to postReply.php. So, unless you tell reply.php to send that GET value as well, it will get lost.
You can do this by either specifying the GET value in the action parameter of the form tag, or you could even switch to a POST value with that, by adding a <input type="hidden" name="verseid" value="<?php echo $verseid; ?>" /> to the form.
Although it may seem counter-intuitive an HTTP request can come in with both Form and QueryString data. Like robertbasic says you can access them both via there respective arrays.
Using a form with a hidden input (<input type="hidden" name="verseid" value="..." />) is probably the cleanest way of doing things.
PHP also defines the $_REQUEST global array in addition to $_GET and $_POST. In general you should use either $_GET or $_POST but in this case where verseid is being passed for both methods, it might be more convenient to use $_REQUEST['verseid']. This way you need not care about the HTTP method being used on your script.
I've never really thought about this, but it helps with some security of something I'm currently working on. Is it possible to submit GET data without an actual input field, and instead just getting it from the URL?
If so, how would I go about doing this? It kind of makes sense that it should be possible, but at the same time it makes no sense at all.
Perhaps I've been awake too long and need some rest. But I'd like to finish this project a bit more first, so any help you can offer would be appreciated. Thanks
Yes. If you add some query-string to yourl url, you can obtain that in php using $_GET without form submitting.
Going to this URL adress http://yoururl/test.php?foo=bar cause echoing foo (if there will be no foo query string, you'll get warning).
# test.php
echo $_GET['foo'] # => bar
Is this what you mean?
Link
// page.php
echo $_GET['type']; // foobar
This is what I understand of your question:
You have a <form method="get" action="foo.php">-like tag on your page
You have a series of <input type="text" name="bar"/> in your page
You want to pass additional GET parameters that are not based on an input from the form
If so, it is possible, but I hardly see how it could help with security. Input from a client cannot be trusted, so even if you hardcode the GET value, you have to check it serverside against SQL injection, HTML injection/XSS, and whatnot.
You have two ways:
Use a hidden input: <input type="hidden" name="myHiddenGetValue" value="foobar"/>
Add the GET parameter to the form action: <form method="get" action="foo.php?myHardcodedGetValue=foobar">
If what you meant is that you want to have a GET request without a form, you just need to pass all the GET parameters to the href of a link:
Click here!
Yes it's possible. Just append the GET data to the link.
For example:
<a href="main.htm?testGet=1&pageNo=54>Test</a>
You can also use Javascript to build the url.
If you happen to be using jQuery and want to build the GET data dynamically you can do this:
var getParams = { testGet:1, pageNo:54 };
$(".myLink").attr("href", url + "?" + $.param(getParams));