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';
}
Related
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!
I have a function that receive a $_POST (your content: a variable 'xml' in a xml format).
I've tried this:
function call($_POST) {
$xml = $_POST['xml'];
$doc = simplexml_load_string($xml);
}
But when i try write the final variable in a file, nothing is written.
(And i've already tried treat the post as an array, but doesn't work too).
Thanks,
I think by declaring $_POST as an input parameter of the function, you've redefined it in the local scope and lost the global data because you try to re-assing it ($_POST is a superglobal). For what you've done, some good existing Q&A material are:
PHP: $_GET and $_POST in functions? (Aug 2009)
passing POST array to php function (Nov 2009)
Even those are older, the information they give about the topic are still valid in PHP because those superglobals haven't changed meanings over the years.
However in all current stable PHP Versions (that is PHP 5.4 and up) your code produces a fatal error (Online Demo in 80+ PHP Versions):
Fatal error: Cannot re-assign auto-global variable _POST
This might explain that you don't get any output. See Nothing is seen. The page is empty and white. (also known as White Page or Screen Of Death) in the Reference - What does this error mean in PHP?. You might not see any error message because you don't display it or you don't look into the log-file, see How to get useful error messages in PHP?.
So what can you do? Next to understanding the basics of PHP error handling - and I suggest you to enable error logging and locate the error log to get fluent with it - you need to change the code as outlined in the two reference questions linked above.
Either get rid of $_POST as an input to the function, or change the name to something else like $postData.
function call(array $postData)
{
$xml = $postData['xml'];
$doc = simplexml_load_string($xml);
}
The just call the function with:
call($_POST);
Or if you only pass some little parameters with that array, write them out:
function call($xmlString)
{
$doc = simplexml_load_string($xmlString);
}
Usage:
call($_POST['xml']);
This will allow you to use different form names with the same code. Very useful because you don't tie things too hard together but leave some wiggling room.
God knows how reassigning $_POST as a function argument name will screw things up. Try changing your code to:
function call($xmlstr) {
$doc = simplexml_load_string($xmlstr);
// should probably be a return here, but I'm guessing
// your function does more than just parse XML...
}
call($_POST['xml']);
Or simply:
function call() {
$doc = simplexml_load_string($_POST['xml']);
}
call();
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.
After hours of frustration, I finally found the line of code that has been causing an error, but now I need to know why.
jQuery was throwing this error: Uncaught Error: Syntax error, unrecognized expression: .
I've researched it and found that this is a Sizzle error that occurs when jQuery cannot find the selector that is referenced. As was suggested in other questions on SO, this was not actually an error in my jQuery code, it was elsewhere.
I was defining a variable to use as a target element to load content, and was using PHP to dynamically define the variable, like so:
var $container = $(".<? echo $target ?>");
This did not work, as the . is flagged as an unrecognized expression. However, replacing the PHP variable with a static string works fine:
var $container = $(".target");
This was so hard for me to find because I couldn't pinpoint the line that was throwing the error, and in the source from the browser, the initial line above looks just like the second line.
Why does the first example not work? Does it have to do with order of execution? And, how can I use a dynamic variable as a selector?
you have to use
<?php echo $test; ?>
or the shortcut:
<?= $test ?>
You can try trim($target) before doing this. If it works you probably have some unwanted spaces in that variable of yours.
Also consider using json_encode to pass variables from php to javascript. Like so:
var selector = <?php echo json_encode($target); ?>;
var $container = $(selector);
This will allow you to pass not only simple strings but more complex variable structures as well (and with encoding safety).
Turns out the page I was loading wasn't having the variable $target passed to it. On the initial page, $target was initialized with a value, and thus the source output looked as specified in the question. However, the ajax call I was making to reload the page with new data was not passing the variable.
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.