Call to undefined function get_field_id() in wordpress - javascript

Fatal error: Call to undefined function get_field_id() in E:\wamp\www\customfield\wp-content\plugins\custom-widget2\update.php on line 6.
i am using ajax to create second dropdown. my ajax page name is update.php but in response give me this error Call to undefined function get_field_id().
i making a plugin for widgit
echo $ret='<select id="'.get_field_id('test2').';" name="">';
$categories= get_categories();
$categoryid="";
$categoryid=5;
$taxonomy_names = get_object_taxonomies($status);
$taxonomy_name =$taxonomy_names[0];
$tax_terms = get_terms($taxonomy_name);
foreach ($tax_terms as $tax_term):
option = '<option value="' .$tax_term->name . '" ';
if ($cat->cat_ID == $categoryid) :
$option .= ' selected = "selected" ';
endif;
$option .= '">';
$option .= $tax_term->name;
$option .= '</option>';
$ret.=$option;
echo $option;
endforeach;
$ret.="</select>'";
update.php
please help me......

That function is out of scope.
To fix add
require('/the/path/to/your/wp-blog-header.php');
at the top of your file

Related

How to insert a php array with innerHTML in javascript

I would like to be able to insert html code from javascript, which inserts php code that contains an array. What I do is get in the variable $ teams an array of teams. And then I go through the array with a foreach to set the values ​​in the select options
I have done the following but it does not work.
document.getElementById('selectTeam').innerHTML = '<select class="form-control"><?php $teams = ControllerTeam::ctrTeam(); foreach ($teams as $key => $value) { echo '<option value="'.$value["id"].'">'.$value["name"].'</option>';}?></select>';
First problem is that, probably, the content is loaded after the JS so the selector returns null hence the error in the OP code.
Cannot set property 'innerHTML' of null
That means, you should only execute the JS code on window.load or any other similar event that ensures the fact that the HTML is loaded before trying to execute.
Now, for the other issue in the comments, if you rewrite the PHP code like this :
<?php
$teams = ControllerTeam::ctrTeam();
$options_html = '';
foreach ($teams as $key => $value) {
$options_html .= '<option value="' . $value["id"] . '">' . $value["name"] . '</option>';
}?>
And in the JS - make sure it loads on window.load or similar -
document.getElementById('selectTeam').innerHTML =
'<select class="form-control"><?php echo $options_html ?></select>';
This makes the code more readable and helps you debug easier.
There is a typo in your code. Please change it to:
document.getElementById('selectTeam').innerHTML = '<select class="form-control"><?php
$teams = ControllerTeam::ctrTeam();
foreach ($teams as $key => $value) {
echo '<option value="' . $value["id"] . '">' . $value["name"] . '</option>';
}
?></select>';

form select option causing javascript to fail

I've got the following javascript function that reloads the form once a select option is chosen. I'm using this to pull data from database into each additional select dropdown based on which option they chose in the first, second, etc.
function reloadForm(form)
{
var cat=form.cat.options[form.cat.options.selectedIndex].value;
if (cat.length > 0) { var cat1 = '&cat=' + cat; }
var subcat1=form.subcat.options[form.subcat.options.selectedIndex].value;
if (subcat1.length > 0) { var subcat2 = '&subcat=' + subcat1; }
self.location = 'index.php<?php echo $webvars; ?>' + cat1;
}
But here is my problem, once I add the subcat variable in, the script stops working. I don't understand why?
Below is my html/php code so far.... just in case someone needs it.
$product_sku = $db1q->query("SELECT sku FROM Inventory_SKU WHERE id = $id");
if ($product_sku->num_rows > 0) {
while ($row = $product_sku->fetch_assoc()) { echo '<h2>CONNECTING '. $row['sku'] .' TO PRODUCT</h2>'; }
}
echo '<div class="margin20">';
echo '<form name="connectsku" action="index.php'. $webvars .'" method="POST">';
echo '<input type="hidden" name="connect_sku" value="1">';
echo '<select name="cat" onchange="reloadForm(this.form)"><option value="">SELECT CATEGORY</option>';
foreach ($db1q->query("SELECT c.id, c.name,s.name as section FROM Product_Categories as c JOIN Product_Sections as s ON c.section = s.id") as $catdd) {
echo '<option value="'. $catdd['id'] .'">'. $catdd['name'] .'</option>';
}
echo '</select>';
if ((isset($cat)) && ($cat != "")) {
echo '<select name="subcat" onchange="reloadForm(this.form)">
<option value="">Select SUBCAT</option>';
foreach ($db1q->query("SELECT id, name FROM Product_Sub_Category ") as $subcatdd) {
echo '<option value="'. $subcatdd['id'] .'">'. $subcatdd['name'] .'</option>';
}
echo "</select>";
}
if ((isset($subcat)) && ($subcat != "")) {
echo "<select name='subcat3' ><option value=''>Select one</option>";
foreach ($dbo->query($quer3) as $noticia) {
echo "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";
}
echo "</select>";
}
echo '<input type="submit" value="MOVE TO NEXT STEP">';
echo '</form>';
echo '</div>';
CONSOLE LOG
Uncaught TypeError: Cannot set property 'oninput' of null
at HTMLDocument.<anonymous> (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:65)
at c (jquery-1.10.2.min.js:4)
at Object.fireWith [as resolveWith] (jquery-1.10.2.min.js:4)
at Function.ready (jquery-1.10.2.min.js:4)
at HTMLDocument.q (jquery-1.10.2.min.js:4)
index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:238 Uncaught TypeError: Cannot read property 'options' of undefined
at reloadForm (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:238)
at HTMLSelectElement.onchange (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:248)
Subcat Select field is hidden until $cat is definied by url update. Could it be because there is nothing to pull from on the page thats causing the error? Is there a line of code I can add to check to see if subcat has a valid value or exists because running the subcat portion?
Found out that it was because the select field for subcat was not being generated because I had a condition that it only generated if cat was set. I ended up always showing it and just "hide" it using html if category isnt set. That way javascript could at least detect it was there. This ended up fixing the entire problem.

Get day names in Russian

The day names on my site are in English.
How can I get the day names in Russian?
In JavaScript I tried to replace Tuesday->Вторник in code but it is not working.
Code:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$url = 'http://api.sypexgeo.net/xml/'. $ip .'';
$xml = simplexml_load_string(file_get_contents($url));
$loc_array = array($xml->ip->city->lat,$xml->ip->city->lon);
$loc_safe = array();
foreach($loc_array as $loc){
$loc_safe[] = urlencode($loc);
}
$loc_string=implode(',', $loc_safe);
$json = file_get_contents('http://api.wunderground.com/api/232323***/satellite/webcams/forecast/q/' . $loc_string . '.json');
$obj = json_decode($json, true);
?>
<?
$html .= "</h2><table cellpadding=4 cellspacing=3><tr>";
foreach ($obj['forecast']['simpleforecast']['forecastday'] as $arr) {
$html .= "<td align='center'>" . $arr['date']['weekday'] . "<br />";
$html .= "<img src='http://icons-pe.wxug.com/i/c/k/" . $arr['icon'] . ".gif' border=0 /><br />";
$html .= "<font color='red'>" . $arr['high']['celsius'] . '°C' . " </font>";
$html .= "<font color='blue'>" . $arr['low']['celsius'] . '°C' . "</font>";
$html .= "</td>";
}
$html .= "</tr></table>";
echo $html;
?>
<script type="text/javascript">
window.onload=function(){
//I try, but not replace
$("td:contains('Tuesday')").html("Вторник");
};
</script>
As an option, you can change the call to wunderground API. Include language settings /lang:xy. Something like this:
https://api-ak-aws.wunderground.com/api/8dee7a0******/satellite/webcams/forecast/lang:RU/units:metric/v:2.0/q/CWEW.json
You can try defining a JSON object like:
days = { "name of the day": "name of the day in Russian" }
Then instead of using $arr['date']['weekday'] use days[$arr['date']['weekday']].
Note: I don't know the php syntax actually, but something like that should work.

Passing javascript array via POST to PHP does not working

I need to pass a javascript array, via POST, to a PHP file.
I've tried to semplify my original business trying to explain my troubles ...
This is my first PHP file, in which I declare a javascript array and I set each element to 1 value (not a great business I know, but it doesn't matter, it's only to explain ....)
<?php
echo '<form action="testPhp-2.php" method="POST" target="testPhp-2">';
echo '<script type="text/javascript">';
echo 'var arr_selections = [];';
echo 'for(i=0; i < 10; i++)';
echo ' {';
echo ' arr_selections[i] = 1';
echo ' }';
echo 'arr_selections_json = JSON.stringify(arr_selections);';
echo 'alert (arr_selections[2]);';
echo 'document.write("<br/>");';
echo ' ';
// echo 'document.write("<input type="hidden" />");';
echo 'document.write("<input type=\"hidden\" name=\"arr_selections_json\" value=\"arr_selections_json\" />");';
echo ' ';
echo '</script>';
echo ' <input type="submit" value="Controlla">';
echo ' </form>';
?>
.... and here you are the code of testPhp-2 file ...
<?php
if(isset($_POST['arr_selections_json']))
{
echo "OK, array exist !! ";
echo '</br>';
echo '</br>';
$arr_selections = json_decode($_POST['arr_selections_json'], true);
echo $arr_selections[0];
}
else {
echo "NO; array does not exist !! ";
echo '</br>';
echo '</br>';
}
?>
Now, if you try to execute the code you'll see the OK, array exist !! message but no array value is printed about the echo $arr_selections[0]; line of code in testPhp-2.php file.
Any suggestion will be appreciated! Thank you in advance!
Cesare
Problem is that you're setting the value of the input to the litteral string "arr_selections_json" instead of to the contents of that variable.
Change
echo 'document.write("... value=\"arr_selections_json\" />");';
To
echo 'document.write("... value=\""+arr_selections_json+"\" />");';

PHP Database Query: How to return the results from a while loop to a Javascript Variable?

I have a php query like so:
<?php
$query = "SELECT * FROM " . $usertable . " ORDER BY fname;";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo '<option value="' . $row['pkid'] . '">' . $row['fname'] . ' ' . $row['lname'] . '</option>';
}
?>
Within this same .php file I have some javascript. All I would like to do is return $row['fname'] as a a javascript variable.
Is this possible?
If you want to output the PHP variable in a JavaScript variable you could do something like this -
echo '<script>var name+' . $row['pkid'] . ' = ' .$row['fname'] . ';</script>';
This would give you a uniquely named variable for each row.

Categories

Resources