I have one question, how can I reoder the GET variables.. it is very complicate to explain, but i think is much better to show the code.
http://URL.com/?product_id=23&year=2013&seite=reporting&action=jahres&report=jahres
but i really want is like this.
http://URL.com/?seite=reporting&action=jahres&report=jahres&product_id=23&year=2013
<input type="hidden" name="seite" value="reporting"/>
<input type="hidden" name="action" value="jahres"/>
<input type="hidden" name="report" value="jahres"/>
the action of the form is just the URL of the page.
The GET variable ?seite=reporting, <--- this i need to be always on the front from all the others variables, how could i do?
Thank for you help!
If you are passing them in the url yourself, it does not matter what order you place them.
It does not matter. You can add more query string parameters, or remove them in any order. Then access them using $_GET["product_id"] etc and handle accordingly.
GET variables aren't ordered, you can place them in any order you would like and it would still give the same results.
If you have Hidden inputs you can order the view of these variables in your URL, reording the position of these variables, for example:
<input type="hidden" name="seite" value="reporting"/>
<input type="hidden" name="action" value="jahres"/>
<input type="hidden" name="report" value="jahres"/>
You can do like this:
<form>
1. First Level in your form
<input type="hidden" name="seite" value="reporting"/>
2. Last level in your form the other variables hidden or not.
<input type="hidden" name="action" value="jahres"/>
<input type="hidden" name="report" value="jahres"/>
</form>
If you want to access to the variables:
$seite = isset($_GET['seite']) ? $_GET['seite'] : null;
And when you press the submit button the form will put the first input hidden, GET in your URL, but really doens't matter, is just for stetic!
You could put your GET Variables in an array
$variables = Array (
[0] => $_GET['one']
[1] => $_GET['two']
[2] => $_GET['three']
);
then just echo them out one by one in URL output
$url = "http://website.com/script.php?get1=" . $variables[0] . "&get2=" . $variables[1];
Related
I am trying to create a form that gets a user directions to a pre-defined location from the location that they entered. I am using Bing Maps 'create a custom url map' to achieve this.
Based on msdn (http://msdn.microsoft.com/en-us/library/dn217138.aspx) I know I need the form to pass a single value such as the following: rtp=adr.'addressfromuser'~adr.'predefined address' . I just don't know how to merge the form values (or insert adr. in front of the user input). I have been unable to find a way to do this so any help is appreciated.
my code for my form is below:
<form action="http://bing.com/maps/default.aspx" method="get" target="_blank">
<label for="rtp">From:</label>
<input class ="input" type="text" name="rtp" value="adr." /> <!--user input-->
<input type="hidden" name="rtp" value="~adr.Mission,TX" />
<!--predetermined destination-->
<input class="btn btn-primary" type="submit" type="submit" value="Get directions" />
</form>
Ding this gets a result that is close to what I want but not quite there (would like to hide the initial "adr." and not generate a "rtp=" before the 2nd rtp being passed. Note: if I comment out the user inputs I successfully get a map with the final destination as point b, but no point a defined
If what I'm trying to do is possible help is appreciated!
You would need to array your ftp input:
EDIT: YOU NEED TO REMOVE adr. in the value="adr." on both rtp inputs!
<form action="http://bing.com/maps/default.aspx" method="get" target="_blank">
<label for="rtp">From:</label>
<input class ="input" type="text" name="rtp[]" value="" /> <!--user input-->
<input type="hidden" name="rtp[]" value="Mission,TX" />
<!--predetermined destination-->
<input class="btn btn-primary" type="submit" value="Get directions" />
</form>
Then, when you process the form, you would implode() that rtp[] array like so:
<?php
/* $_GET['rtp'] will look like this before implosion:
array(
0 => FromCity,UT
1 => ToCity,CA
)
*/
// You need to pre-append the adr. value to both strings by looping array
foreach($_GET['rtp'] as $value) {
$_rtp[] = "adr.'".$value."'";
}
/* Now $_rtp looks like this:
array(
0 => adr.'FromCity,UT'
1 => adr.'ToCity,UT'
)
*/
// Now implode this new array with "~"
// You could assign a new variable to the implode, but if you wanted to,
// you could override the same $_GET['rtp'] variable with it's imploded self.
$_GET['rtp'] = implode("~",$_rtp);
// Now the $_GET['ftp'] = adr.'FromCity,UT'~adr.'ToCity,UT'
?>
use javascript/jquery
$('form').submit(function(){
//do your actions here
$('input[name=rtp]').value()
return false; // prevents submit by default action.
});
I have user inputs as follows:
<form action="special.php" method="post">
<input name="first1"> <input name="last1"> <input name="age1">
<input name="first2"> <input name="last2"> <input name="age2">
<input name="first3"> <input name="last3"> <input name="age3">
<input name="first4"> <input name="last4"> <input name="age4">
<input name="first5"> <input name="last5"> <input name="age5">
<input name="first6"> <input name="last6"> <input name="age6">
...
N
</form>
The amount of user inputs in the form is determined by the user; meaning, the user can add 5,10,20 additional lines to the code above, creating new input elements (following the pattern above) as they fit.
My question is, once the form gets submitted, what is an easy way to iterate and print out all the SET POST variables?
Something like:
for($i=0; $i < $numPostVars; $i++){
if(isset($_POST['first".$i."'])){
//echo all first names post variables that are set
}
}
// do the same from last names & age in separate loops
I think the trick is to name your variables slightly different, and take advantage of PHP's feature which will unpack them as arrays for you. Just use the syntax: first[1]. Then in PHP, $_POST['first']['1'] is where you will find it. You can then iterate all your "first" inputs with
foreach($_POST['first'] as $first_input) {
// ...
}
Also keep in mind that browsers may not send the field if it is empty when the user submits.
Here is what the inputs should look like in HTML:
<input name="first[1]"> <input name="last[1]"> <input name="age[1]">
As noted by user #DaveRandom, consider also a more hierarchical structure (think "rows" like from your db):
<input name="people[1][first]"> <input name="people[1][last]"> <input name="people[1][age]">
Inputs can be treated as arrays with a syntax very similar to that used in PHP:
<input name="name[1]" value="value 1">
<input name="name[2]" value="value 2">
This would result in a $_POST['name'] that looks like this:
array(
1 => "value 1",
2 => "value 2"
);
This principle can be expanded to incorporate multi-dimensional and associative arrays. So if you were to name your inputs like this:
<input name="rows[1][first]"> <input name="rows[1][last]"> <input name="rows[1][age]">
<input name="rows[2][first]"> <input name="rows[2][last]"> <input name="rows[2][age]">
...you would be able to easily iterate over $_POST['rows'] with a foreach construct. The data structure will be very similar to a set of database results.
foreach ($_POST['rows'] as $row) {
// do stuff with $row['first'], $row['last'] and $row['age'] here
}
A couple of things to note:
Unlike PHP, associative array keys in HTML do not require quotes, and using them will produce a result you may not expect. It will work, but not in the way you might think. You still need to use quotes in PHP though.
As far as I am aware, this syntax is not a W3C standard. PHP, however, always handles it as expected.
I can't set a variable from a post array.
I have a simple form with a hidden field in it:
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
This hidden field gets sent off to a second file (exec.php) where I have the following code:
$sid = $_POST['sid'];
For some reason, when trying to set $sid, it gets a NULL value. For haha's, I ran the following:
foreach($_POST as $var => $value)
{
echo $var . ' : ' . $value . "<br>";
}
This provided a correct value of 1938 for sid. I've looked at this for 3 hours and can't find what is happening. I expect something extremely stupid...any thoughts?
Here is the form on enter.php
<form name="form1" method="post" action="exec.php">
<input name="sid" type="hidden" id="sid" value="<? echo($sid); ?>">
<input name="ticket_totals" type="hidden" id="ticket_totals" value="<?=$ticket_totals?>">
<input name="emp" type="hidden" id="emp" value="<?=$emp?>">
<input name="submit" type="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Close">
</form>
Here is the POST output on exec.php:
type : Other
ticket_totals : 0
emp : 105
sid : 1939
submit : Submit
Okay - this was poor syntax on my part but now I'm curious as to why.
I left out quotation marks - the solution is as simple as this:
$sid = $_POST["sid"]
Now it works like a champ.
Any takers on why? I'd guess there is a setting in the php.ini that requires the quotes. Strangely enough, I have other variables called from the POST array that i'm not using quotes for and they're working fine...
Use Console in FireBug to inspect the POST request to see what is the sid value that is being sent.
If the sid value in request is ok, use var_dump($_POST["sid"]); to see the results on the server.
EDIT: it's considered good PHP style to use the quotes when accessing the associative array because quote-less keys are indistinguishable from constants:
define('myVar',3);
echo $array[myVar]; // retrieves $array[3], not $array['myVar'];
Try to echo the $sid instead of the <?=:
// Change that
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
// With that
<input name="sid" type="hidden" id="sid" value="<?php echo $sid; ?>">
also for the test time try to change the input type from hidden to text in order to be 100% sure the $sid contains a value.
Using quotes for associative array keys is mandatory, and while it may work without them, it's incorrect and erratic behavior is expected.
I had this same problem, trying to use $_POST[sid] as a variable. I'm am thinking that "sid" is a reserved or restricted variable name, because I changed my variable to $_POST[snid] and it worked just fine. This was my code
$sid = $_POST[sid];
$recipient = "($sid) ($_POST[sid])";
if ($_POST[sid] > 0)
{
$recipient = "It Worked";
}
print $recipient;
When I posted "&sid=15", the result was:
() (15)
Unbelievable. Impossible, right? All I did was change from using "sid" as the index to "snid", and it worked no problem.
So, don't ever use $_POST[sid].
i am trying to create a donate page. the hole page is php,
there is a textbox that hold the amount value which should be send via hidden input with the url to the the payment gateway. i have tryed many time but it is not working. i am still a beginner in this could any one please help me in fixing my code here
<div class="donate">
<?php
$amount = $_REQUEST['amount'];
$txtCurrency = 840;
$txtAmount = number_format($amount, 2, '.', '');
echo $amount;
$key = "TEST";
$txthttp = "http://test.com/you.php";
?>
<form action="payment.php" name="form1">
<input type="text" id="amount">
<input type="submit">
<input type="hidden" name="txtAmount" value="<?= $txtAmount; ?>">
<input type="hidden" name="txthttp" value="<?= $txthttp; ?>">
<input type="hidden" name="signature" value="<?= $key; ?>">
</form>
</div>
In general, you should avoid using $_REQUEST when you can use $_GET or $_POST. $_REQUEST allows variables to be set by either an HTTP GET or POST, which can pose a security risk, since your site will presumably use one or the other. With that said, here's what I would do:
Add method="post" to your form tag.
Access the input elements by looking at $_POST['txtAmount'], $_POST['txthttp'], etc.
In general, you can view all variables set in the POST by doing this:
var_dump($_POST);
You can access these values from payment.php with $_POST['txtAmount'], $_POST['txthttp'], $_POST['signature']. How you handle them will be up to your code. I see that you used the $_REQUEST array, which will work, however I believe it's better form to be specific and use the $_POST array.
Let me explain.
Normally when hidden fields are passed from a form to the page specified in the action of the form, those hidden fields can be accessed on the processing page like so:
The Form:
<form action="process.php" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="hidden" name="loginTime" value="1:23PM" />
<input type="hidden" name="userIp" value="173.23.22.5" />
<input type="submit" name="submit" value="Submit" />
</form>
Processing Page (process.php):
<?php
if isset($_POST['submit']) {
echo $_POST['username'];
echo $_POST['loginTime'];
echo $_POST['userIp'];
}
?>
You see how I had to call the two hidden fields by name and individually. Is there any way I can call all hidden fields that are passed to a page from a form all at once despite what the field names of those are or how many there are?
In other words how do I make PHP do this:
// echo the contents of all hidden
fields here (if there were any)
EDIT
Additional info:
The form is designed in such a way (not the one above of course) that field names will be of the following sort:
product_name_1
product_quantity_1
product_price_1
product_name_2
product_quantity_2
product_price_2
and so incremented so on...
Depending on the user action there can be 3 hidden fields or thousands, there are no limits.
Make an array of valid hidden field names, then iterate through $_POST and if the $_POST field name is in the array of valid field names, echo them.
$valid = array('first_name', 'last_name');
foreach ( $_POST as $key => $value ) {
if ( in_array( $key, $valid ) ) {
echo $_POST[$key];
}
}
PHP does not care whether the field was hidden or not, HTTP doesn't tell PHP how it appeared on the website.
The closest thing I would come up with was saving all names of the hidden fields inside an array and echoing them all in a loop.
You can try the following:
<form action="process.php" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="hidden" name="group_hidden[loginTime]" value="1:23PM" />
<input type="hidden" name="group_hidden[userIp]" value="173.23.22.5" />
<input type="submit" name="submit" value="Submit" />
</form>
And then print it:
echo htmlspecialchars(print_r($_POST, true));
This might give you a clue about how to solve this.
there's no way to tell the type of the input built-in, so instead you'll need to come up with a way to identify the ones you want yourself. This can be done either by coming up with a special naming scheme, or by storing a list of the names of the hidden fields in another field. I'd recommend the former option, since you don't have the risk of losing data integrity somehow. Look at using array_filter to parse through the array to get the specially-named fields out.
Maybe, assuming that your hidden fields will be in sequence (i.e. 1,2,3 and not 1,2,4) following all of the end users' actions (adding and taking away fields), you could try something along the lines of
$i = 1;
while(isset($_POST["product_name_$i"]))
{
echo $_POST["product_name_$i"];
echo $_POST["product_price_$i"];
$i++;
}
Or something along those lines?