I am trying to print date values before form submission but it did not print anything and submitted the form, below is my code
function webforms_save_submission_alter(&$fields, $context)
{
foreach ($fields as $key => $value) {
if (in_array($key, ["Date_Prototype__c", "Date_Production__c", "NBIOT_Date_of_Field_Trial__c", "NBIOT_Date_Lab_Trial__c"])){
$fields[$key] = trim($value);
print_r($fields); // printing here
if ($fields[$key] == 'Date_Production__c') {
$fields[$key] = format_date(strtotime($fields['Date_Production__c']), 'medium', 'm-d-Y');
print_r($fields); // printing here
die;
}
I want to see the date reactions before submitting the form but why it did not print anythign? can someone please help me on this?
Your help will be highly appreciated..
There are a number of problems with that code, which I suspect you've only written to debug a problem? It's much too complicated for that. Also, die is a function, and needs to be called with parentheses; Syntax errors are also a likely reason for why your code did nothing. Check your watchdog logs.
Try this code instead:
function webforms_save_submission_alter(&$fields, $context)
{
var_dump($fields);
die();
}
Related
I am trying to modify data in a json file with php. The code I am using is below. It is able to successfully ready the file contents and in the foreach loop it will echo out in the if statement.
This is great, the if statement is hardcoded for now to test. What I want to do is modify various properties and write it back to the file. This does not seem to be working. When I load the page, then refresh to see if the new value was set it just keeps echoing the same values. I download the .json locally and nothing has changed.
Any thoughts on what I am doing wrong?
//Get file, decode
$filename = '../json/hartford.json';
$jsonString = file_get_contents($filename);
$data = json_decode($jsonString, true);
foreach ($data['features'] as $key => $segment) {
if ($segment['properties']['UID'] == '25301') {
//echo out properties for testing
echo("KEY: ".$key."<br/>");
echo("UID: ".$segment['properties']['UID']."<br/>");
echo("Full Name: ".$segment['properties']['FULLNAME']."<br/>");
echo("FCC: ".$segment['properties']['FCC']."<br/>");
echo("Render CL: ".$segment['properties']['RENDER_CL']."<br/>");
echo("<hr/>");
//set property to new value.... NOT WORKING?
$segment['properties']['RENDER_CL'] = 111;
}
}
//Test if file is writable to be sure
$writable = ( is_writable($filename) ) ? TRUE : chmod($filename, 0755);
if ( $writable ) {
$newJsonString = json_encode($data);
if (file_put_contents($filename, $newJsonString)) {
echo('Put File Content success');
} else {
echo('NOT put');
}
} else {
echo 'not writeable';
}
In the end it will echo out 'Put File Content success' which seems to indicate it was successful but it isn't... Thanks for any advice.
You need to understand how foreach cycle works. The thing is, that the value you're getting ($segment) is a copy of the real value in the source array. So when you assign to it ($segment['properties']['RENDER_CL'] = 111;), you don't really change the source array. You only change some local variable that goes out of scope when the cycel-loop ends.
There are several ways how to solve this issue. One of them is to add & before the value-variable:
foreach ($data['features'] as $key => &$segment)
This tells it to use the reference of the array-item, not to copy its value.
You should use $segment variable in foreach as a reference:
foreach ($data['features'] as $key => &$segment) {
...
$segment['properties']['RENDER_CL'] = 111;
}
I'm wondering if anyone could help me out in trying to use a feature of my form plugin (FormCraft for Wordpress) that allows the form data to be sent to a URL. There's no documentation on this feature and I can't figure it out. Here's a screenshot of the options:
I've created an empty form_data.php file and uploaded it to my site, then put that URL in the field seen in the screenshot. Nothing happens in the php file when I submit a form. The support team for the plugin hasn't been very helpful, they just said, "You need to make sure the script on that URL will catch and parse the data." Is there anything that I would need to add to the php file to get this to work?
Send the data to this file below.
http://localhost/bellow_file.php
<?php
$json = json_encode($_REQUEST, JSON_UNESCAPED_UNICODE);
$json = json_decode($json,true);
$data = "";
foreach ($json as $key => $val) {
if(is_array($val)) {
$data .= "$key:\n";
} else {
$data .= "$key => $val\n";
}
}
file_put_contents('test.txt', $data);
?>
I'm trying to get this function to work but for some reason it errors out on the foreach line saying there is an invalid argument.
$scores= TESTAPI::GUL($user->ID);
if (empty($scores)) {
echo "<p>No Scores</p>";
} else {
foreach ($scores as $score) {
echo "<p>".$score."</p>";
}
}
The error I get is: PHP Warning: Invalid argument supplied for foreach()
Note: I need to keep the if... else... in the order it currently is.
Some variations for the if statement, prior to the for
if(!is_array($scores) && !count($scores) || empty($scores)){
if(!is_array($scores) && !count($scores)){
I can't use use var_dump($scores); because the error is on a page on a private website I run (can't provide links) and I am unable to find a way to recreate the error. Maybe someone knows a way I can capture data that causes the foreach to go wrong and save it to a log file?
You can use error_log in case it is no array:
$scores= TESTAPI::GUL($user->ID);
if (empty($scores)) {
echo "<p>No Scores</p>";
} else {
if(!is_array($scores)) {
$scores_info = print_r($scores, true);
error_log('$scores is no array but: ' . $scores_info);
echo "<p>No Scores</p>";
} else {
foreach ($scores as $score) {
echo "<p>".$score."</p>";
}
}
}
It will write to the error log. You can see more at the error_log doc how to configure it. You should clearly dry it up when you know what the error is, but for temporary catching it this works ...
I have some code to get some public available data that i am fetching from a website
//Array of params
foreach($params as $par){
$html = file_get_html('WEBSITE.COM/$par');
$name = $html->find('div[class=name]');
$link = $html->find('div[class=secondName]');
foreach($link as $i => $result2)
{
$var = $name[$i]->plaintext;
echo $result2->href,"<br>";
//Insert to database
}
}
So it goes to the given website with a different parameter in the URL each time on the loop, i keep getting errors that breaks the script when a 404 comes up or a server temporarily unavailable. I have tried code to check the headers and check if the $html is an object first but i still get the errors, is there a way i can just skip the errors and leave them out and carry on with the script?
Code i have tried to checked headers
function url_exists($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
$headers = #get_headers($url);
//print_r($headers);
if (is_array($headers)){
//Check for http error here....should add checks for other errors too...
if(strpos($headers[0], '404 Not Found'))
return false;
else
return true;
}
else
return false;
}
Code i have tried to check if object
if (method_exists($html,"find")) {
// then check if the html element exists to avoid trying to parse non-html
if ($html->find('html')) {
// and only then start searching (and manipulating) the dom
You need to be more specific, what kind of errors are you getting? Which line errors out?
Edit: Since you did specify the errors you're getting, here's what to do:
I've noticed you're using SINGLE quotes with a string that contains variables. This won't work, use double quotes instead, i.e.:
$html = file_get_html("WEBSITE.COM/$par");
Perhaps this is the issue?
Also, you could use file_get_contents()
if (file_get_contents("WEBSITE.COM/$par") !== false) {
...
}
I would like to set up on my server a service that would determine if a proxy server I scraped off the net is anonymous or not. What I need is just a uri, from which the server would return the request exactly as it was received, and then to check if my public IP is in the response string(in HTTP_X_FORWARDED_FOR for example).
Has anyone has ever done this before?
Any help would be appreciated!
Why not write a simple PHP script and check this for yourself?
<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
?>
Save it as headers.php and call it in your browser via the proxy server. All the request headers seen by the server will be echo'd on screen.
OK, thanks to Gaurav I got it done with this simple php script (getallheaders() need PECL):
<?php
$headers = array();
foreach($_SERVER as $key => $value) {
if(strpos($key, 'HTTP_') === 0) {
$headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
echo $value;
}
}
?>
If anyone ever needs this..