PHP Script Not Receiving Post Data - php

I have a php script that is processing a form for me. However, something is strange (or I'm just a bigger noob than I think).
The following code at the top of the script:
<?php
// Get Post Data to Insert into Database
print('<pre>');
print_r($_POST);
print('</pre>');
$vehicleID = $_Post['list3'];
echo "Variable vehicleID is: {$vehicleID}";
echo "Post variable list3 is: {$_Post['list3']};
?>
Results in the following "printout" when the form is submitted and form data intercepted by the script:
Array
(
[list1] => 7
[list2] => 3923
[list3] => 20745
[Submit] => Submit
)
Variable vehicleID is: Post variable list3 is:
I've only included the post array printout to see if post data is actually being received - which, apparently, it is. So, why is not passing into my variable so that I can USE it?
I know I'm just a PHP hack, but I feel like I must be losing it.

Your code has $_Post and should be $_POST?

Try changing $_Post to $_POST in the lower part of your code; that may be what's causing the problem.

The manual page on the HTTP POST data superglobal array describes it as $_POST.
And, elsewhere:
Basics
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Thus it follows that your use of $_Post should, in fact, read $_POST.
You should fix your error_reporting settings, because you should have seen an E_NOTICE about using a non-existent variable.

Related

Variable not carrying over to all libraries when using POST

Whenever I declare a variable inside a library like:
$Money="123";
The variable carries over to my other libraries like print page and export pdf.
However when I declare a variable using Post like:
$Money=$Post_["moneyNumber"];
My other libraries pull a null value. What can I do to carry on the variable? I need to be able to send the variable from a form input.
$Money=$Post_("moneyNumber");
is incorrect. You're using camel case for Post which should be all uppercase (see note about superglobals) and round brackets which should be square brackets, and you've a misplaced underscore also.
use
$Money=$_POST["moneyNumber"];
Also make sure your form is using a POST method, along with its element having a name attribute of name="moneyNumber"
Sidenote: "moneyNumber" is not the same as "moneynumber". Variables are case-sensitive, should that be the case in the rest of your unshown code. Same thing goes for $Money. So, if you're using $money elsewhere, then that is a probability.
$_POST is a superglobal:
http://php.net/manual/en/language.variables.superglobals.php
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Edit:
If you're trying to carry variables over more than two pages via POST, then that won't work. You will need to use sessions for this.
http://php.net/manual/en/reserved.variables.session.php
http://php.about.com/od/advancedphp/ss/php_sessions_2.htm

how to parse dynamic _POST variable in php

I'm stuck with a php/mySQL thing..
I have a dynamically created form and I want to parse the $_POST variables it generates. To be specific,I have a query in SQL which generates the fields in my form. Then, I need to process these variables in the php file, where the action of the form goes.
However, I cannot parse the dynamically created $_POST variables. Below is my code:
$sql="just-a-query";
$result = mysql_query($sql);
while ($data = mysql_fetch_array($result)) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
For example, if I have 3 variables that got through the form the values:
house=1
tree=3
car=2
I would like to save them via php like this:
$house=$_POST['house'];
$tree=$_POST['tree'];
$car=$_POST['car'];
However I can't get through it. It returns Undefined index error. Any thoughts?
If you want to find if a variable is defined before using it, it's as simple as using isset():
if( isset($_POST[$data['parameterName']]) ) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
If on the other hand, it's supposed to be defined (you see the form element), but then it's not getting defined in the postback. First check to make sure that your form submission type is post, then check to make sure you are using the name attribute in the form elements.
thank you for your time. My problem was that I was parsing wrong parameters from the HTML.
Yes, I'm an idiot and yes, var_dump() helped me to figure my error.
Thanks again!
btw, my code was working perfectly. Ha!

$_SERVER["PHP_SELF"] Apache/Xampp forbidden access

I am using an Xampp/Apache server on localhost.
I am trying to use an action on a form which posts to "register.php".
<form method="post" action= "<?php echo htmlspecialchars($_SERVER["register.php"]);?>">
I get a 403 error "Access Forbidden".
In the url it states:
<br%20/><b>Notice</b>:%20%20Undefined%20index:%20register.php%20in%20<b>C:/xampp/htdocs/2001/create_account.php</b>%20on%20line%20<b>45</b><br%20/>
(Error on line 45 which is the line of code above).
The file this code is in is a php file, create_account.php
I have tried changing apache httpd.conf permissions, no changes. I have tried finding the solution elsewhere but no luck with a fix.
You will need to read the manual page for $_SERVER carefully
http://www.php.net/manual/en/reserved.variables.server.php
you may find action="register.php" is all you will need
In your code example you've tried to call for an undefined key which array $_SERVER doesn't have and therefor you've got an error.
$_SERVER is a php superglobal and it's form is an array. Arrays can be indexed, associative or mutlidimensional. Associative arrays use keys to accesses values ('key' => 'value'). Example of an associative array:
$person=array('Name' => 'John', 'Last name' => 'Spencer', 'Age' => 22);
In order to get John's last name you'd first need to specify the array in which all of the information is being held ($person) and then request certain information from that array via key (['Last name']). Example of such request:
$lastName=$person['Last name'];
If you already know the page you'd like to accesses you should type it in HTML as plain text (method="register.php"), there's no need for any php functionality in doing this. Function htmlspecialchars() also isn't necessary since you are the one typing and there's no need to sanitize your own input.
Better than plain text however would be to use $_SERVER['PHP_SELF'] which returns relative path to the page you're currently on. This way is better than previous since if at some point you were to rename your file you wouldn't have to change to your form's method value unlike in previous example.
Finally if you're not leaving the page you're currently on you could also place an empty string as method (method="").
Recommended for further read:
php.net's documentation on arrays
w3school's documentation on arrays

putting php variables into SOAP request array

I'm trying to put some PHP variables into an array but I cannot get it to work without getting errors.
Original Code:
"DeclaredValue"=>0,
"Accessorials"=>array("OriginLiftgate"),
"OverDimensionPcs"=>0
What I was trying:
$RLPickupLiftgate = 'OriginPickup';
"DeclaredValue"=>0,
"Accessorials"=>array($RLPickupLiftgate),
"OverDimensionPcs"=>0
Sometimes the variable $RLPickupLiftgate will be empty and the error is triggered. I tried putting some logic into this section of the array but its not working.
As I side note I do understand that the variable does not contain quotes as the original code does.
Thanks
what about
if (!isset($RLPickupLiftgate) && empty($RLPickupLiftgate)) {
$RLPickupLiftgate = 'OriginPickup';
}

PHP Warning while passing $_POST to array_key_exists()

So I am using the following style of code if(array_key_exists('some_value', $_POST)){echo 'hi';}
For PHP 5.2.17 I am getting a warning from this style of code. This is the warning:
WARNING: argument 2 for array_key_exists() is not either an array or an object on line: 123
This seems strange to me because I believe that the $_POST array should always be defined. Is that not the case? I'm not sure what would cause the $_POST array to not be considered an array. I am not resetting $_POST to anything so it should exist as an array at all times. Does anyone have any idea what is wrong. Please let me know if more information is needed and thank you for the help.
Edit: I should note that this only happens on the production server. My local environment does not have this problem.
The Superglobals $_POST and $_GET are only populated if the script is POSTed to or GET from. In your example, the reason that you'd get that error is if there was not post action to the script. Before checking for a certain post value, you should check to make sure there was a post:
if(isset($_POST)) {
//The form was posted
}
In that fashion. From there, you can check for certain values using array_key_exist, or you can further check isset($_POST['myKey']).
Use if(isset($_POST['some_value'])) { echo 'hi'; } instead. Never had a problem with it.
Also check if you are not overriding or unsetting $_POST (or some framework you are using is doing it for you). I avoid to do so with superglobal variables since I think it is a bad practice and might give headaches like this one.

Categories