Can't set variable from $_POST - php

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].

Related

PHP Undefined Variable, but it's defined and has a value

I am working on a PHP project - I had one form post a date to another form
I made some changes (although none to the input in question)
Now all other inputs are updated with their Posted values, except the date
If I manually set the date in HTML it works:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="2009-01-01"></div>
If I set it to the following, it doesn't:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo (isset($DateCourse))?$DateCourse:'';?>"></div>
The below:
$DateCourse = ($_POST["DateCourse"]);
var_dump($_POST["DateCourse"]);
var_dump($DateCourse);
Returns:
string(10) "2019-01-05" - means the post value is set
Notice: Undefined variable: DateCourse in /home/bitecons/bts.biteconsulting.co.za/v2/editccr.php on line 119 - how can it be undefined, I just defined it
NULL
What on earth am I doing wrong? Apart from using PHP :P
Flow as requested:
Records.php:
This is the function to prepopulate my posted fields:
function Prefill(x) {
TabletoEdit = x.closest('table').id;
SelectedRow = x.rowIndex;
document.getElementById("EntryEditing").value = x.cells[19].innerHTML;
document.getElementById("DateCourse").value = x.cells[0].innerHTML;
document.forms["records"].submit();
}
Then I also have:
<form action="editrec" method="post" id="records">
<input type='hidden' name='Period' id='Period' />
<input type='hidden' name='Month' id='Month' />
<input type='hidden' name='res' id='res' />
<input type='hidden' name='CustName' id='CustName' />
<input type='hidden' name='DateCourse' id='DateCourse' />
</form>
The Prefill gets called, then submits the form
I have tracked and DateCourse has data, but when getting to the other form, it "disappears":
if(!empty($_POST)) {
$DateCourse = ($_POST["DateCourse"]);
$CustName = ($_POST["CustName"]);
}
For example, CustName is filled in, but not DateCourse?
Side question:
Would this return true if another post var is not set (unrelated to this one):
if(!empty($_POST))
I think You use wrong Code
you Must Submit First Form And Then Use $DateCourse this In another Form in POSTBACK
One of the best way is to define $DateCourse too like
<?php
$DateCourse = "";
if(!empty($_POST["DateCourse"])) {
$DateCourse = ($_POST["DateCourse"]);
}
?>
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo $DateCourse;?>"></div>
Okay, apologies folks, but it may help others in the future.
I had a function call to an old function - this failed, causing my variable to never get defined... I knew it was something stupid, but sometimes one needs a sound board...

Reorder GET variables

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];

getting the value from a textbox in php

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.

Basic problem of getting values of my textbox

I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"

posting hidden value

hey there,
i have three pages:
(1) bookingfacilities.php
(2) booking_now.php
(3) successfulbooking.php
and they are link together.
i want to pass data from bookingfacilities.php to successfulbooking.php by using hidden field/value. however, my data doesn't get print out in successfulbooking.php.
here are my codes:
from 'booking_now.php':
$date="$day-$month-$year";
from 'successfulbooking.php';
<input type="hidden" name="date" id="hiddenField" value="<?php print "$date" ?>"/>
i would greatly appreciate your help as my project is due tomorrow :(
You should never assume register_global_variables is turned on. Even if it is, it's deprecated and you should never use it that way.
Refer directly to the $_POST or $_GET variables. Most likely your form is POSTing, so you'd want your code to look something along the lines of this:
<input type="hidden" name="date" id="hiddenField" value="<?php echo $_POST['date'] ?>" />
If this doesn't work for you right away, print out the $_POST or $_GET variable on the page that would have the hidden form field and determine exactly what you want and refer to it.
echo "<pre>";
print_r($_POST);
echo "</pre>";
Maybe a little late to the party but why don't you use sessions to store your data?
bookingfacilities.php
session_start();
$_SESSION['form_date'] = $date;
successfulbooking.php
session_start();
$date = $_SESSION['form_date'];
Nobody will see this.
You have to use $_POST['date'] instead of $date if it's coming from a POST request ($_GET if it's a GET request).
I'm not sure what you just did there, but from what I can tell this is what you're asking for:
bookingfacilities.php
<form action="successfulbooking.php" method="post">
<input type="hidden" name="date" value="<?php echo $date; ?>">
<input type="submit" value="Submit Form">
</form>
successfulbooking.php
<?php
$date = $_POST['date'];
// add code here
?>
Not sure what you want to do with that third page(booking_now.php) too.

Categories