I want to generate a random number between 1 and 10, use it in Javascript to return a random item in that array, and use that SAME item in PHP. Thus, I create a random number:
var iRandom = Math.floor((Math.random() * 10) + 1);
I use that number in my Javascript function, and then I have my PHP like this:
$json_a['items'][$iRandomPHP]['title']
Simply said, the $iRandomPHP must have the same random number as in my javascript variable iRandom. I tried jquery .post like this:
$.post('custom_search.php', {iRandomPHP: iRandomPHP});
Trying to convert it in PHP like this:
$iRandomPHP = $_POST['iRandomPHP'];
It doesn't work. Any ideas?
Thanks!
EDIT
For some more complete code:
<script>
var iRandom = Math.floor((Math.random() * 10) + 1);
$.post('custom_search.php', {iRandomPHP: iRandom});
function hndlr(response) {
var item = response.items[iRandom];
document.getElementById("content").innerHTML += item.title + "<br>" + "<img src=" + item.link + ">";
}
</script>
PHP code:
$iRandomPHP = $_POST['iRandomPHP']."haha";
print_r($iRandomPHP);
$url_nocb = "https://www.googleapis.com/customsearch/v1?key=".$key."&cx=013263683795763287108:ne-fxf3oy-a&searchType=image&q=office";
$json_string = file_get_contents($url_nocb);
$json_a = json_decode($json_string, true);
print $json_a['items'][$iRandomPHP]['title']."<br>";
That's all
This line:
$.post('custom_search.php', {iRandomPHP: iRandomPHP});
should read
$.post('custom_search.php', {iRandomPHP: iRandom});
since you are passing the js var iRandom
Related
I have this string in my JS code right now:
newWords = '$(p span.word[style="--' + paraIndex + 'word-index:undefined"], p span.whitespace[style="--' + paraIndex + 'word-index:undefined"])';
I want to convert this string into a jQuery object that I can use do identify those specific elements.
I also saw the eval() function. That looks like it does what I want it to, but is super unsafe/unsecure.
Does anyone know a safe way to do this?
The simplest solution is to remove $( and ) and pass the remaining string as an argument to $():
var paraIndex = 0;
var newWords = '$(p span.word[style="--' + paraIndex
+ 'word-index:undefined"], p span.whitespace[style="--'
+ paraIndex + 'word-index:undefined"])';
var jQ = $(newWords.slice(2, -1));
console.log(jQ);
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I have an anchor which I inject in HTML in jqGrid formatter as below:
var number = rowObject.number;
var plateNumber = rowObject.plateNmber;
var markup = "<a href=%Href%;>%Text%</a>"
var replacements = {
"%Text%": plateNumber ,
"%Href%": "javascript:Search.openViewByPlateNumber(" + number + "," + plateNumber + ")"
};
markup = markup.replace(/%\w+%/g, function(all) {
return replacements[all];
});
Here is my OpenViewByPlateNumber function:
var OpenViewByPlateNumber = function(number, plateNumber) {
// Do something
};
In the UI there will be a number in the grid. When I click on the number the openViewByPlateNumber function will be called. Everything is working fine for me. The problem is the plate number is a string type. It is a number but it can be 1, 2, 3/4, 340/2 etc.
It's working fine when number is simple like 1, 5 or 9 but if number is 340/2, then the method receives a value of 170.5. It divides the number. So how I can pass it as string?
To pass the values to the function as a string wrap them in quotes:
"%Href%": 'javascript:Search.openViewByPlateNumber("' + number + '","' + plateNumber + '")'
I have the following code which is really bloated
$(".field-name-field-parts-status .field-item:contains('Submitted'), .field-name-field-parts-status .field-item:contains('Saved'), .field-name-field-parts-status .field-item:contains('HMNZ Approved')").addClass('btn-primary');
I tried to neaten it up by adding a var
var fieldItemStatus = $(".field-name-field-parts-status .field-item");
So it looked like this
$(fieldItemStatus + ":contains('Submitted'), " + fieldItemStatus + ":contains('Saved'), " + fieldItemStatus + ":contains('HMNZ Approved')").addClass('btn-primary');
But it stopped working, can anyone tell me what I did wrong? Thanks
Because you are trying to add a jQuery object and a string together. It does not work like that.
var fieldItemStatus = $(".field-name-field-parts-status .field-item");
should be a string
var fieldItemStatus = ".field-name-field-parts-status .field-item";
other option is to use filter.
You need to use .filter()
fieldItemStatus.filter(":contains('Submitted'), :contains('Saved'), :contains('HMNZ Approved')").addClass('btn-primary');
fieldItemStatus is an object so
fieldItemStatus + ":contains('Submitted'), " + fieldItemStatus + ":contains('Saved'), " + fieldItemStatus + ":contains('HMNZ Approved') will create a string like [Object object]:contains('Submitted'), [Object object]:contains('Saved'), [Object object]:contains('HMNZ Approved')
remove $ in front for fieldItemStatus
var fieldItemStatus = ".field-name-field-parts-status .field-item";
Because you want to use a jQuery Object to concat string. The right way to do this is using string all the time.
var fieldItemStatus = ".field-name-field-parts-status .field-item";
$(fieldItemStatus + ":contains('Submitted'), " + fieldItemStatus + ":contains('Saved'), " + fieldItemStatus + ":contains('HMNZ Approved')").addClass('btn-primary');
You could use the filter method:
fieldItemStatus.filter(":contains('Submitted'), :contains('Saved'), :contains('HMNZ Approved')").addClass('btn-primary');
Another option is using the filter callback function:
var items = ['Submitted', 'Saved', 'HMNZ Approved'];
fieldItemStatus.filter(function(_, el) {
return items.some(function(item) {
return el.textContent.indexOf(item) > -1;
});
});
.
A more procedural approach. This way if you want to easily change the selectors, just change the contains array. You could turn this into a function to easily retrieve your selector on demand elsewhere in the script.
var contains = ['Submitted','Saved','HMNZ Approved'];
var selector = '';
for(var i = 0; i < contains.length; i++) {
selector += '.field-name-field-parts-status .field-item:contains("' + contains[i] + ')';
if(i < contains.length - 1) selector += ', ';
}
$(selector).addClass('btn-primary');
I have a Problem with my push function in JavaScript.
<script type="text/javascript">
var myArr = []
var len = myArr.length
$.getJSON('daten.json', function(data) {
$.each(data,function(key,value) {
for(var i = 0; i <= len; i++){
myArr.push("<p>" + value.Name+i ," ", value.Nachname+i + "</p>")
}
})
$('.content').html(myArr.join(''))
})
</script>
I need to convert value.Name+i like this = value.Name0, value.Name1 and so on. I got a JSON File and the Keys are Dynamic, so the first entry got Name0 the second Name1 and so on. Now I must print the JSON file on my html page, but how I can write this line:
myArr.push("<p>" + value.Name+i ," ", value.Nachname+i + "</p>")
with my var i which increment in the loop, to call the Keys in my JSON file?
Like value.Name0. Doing value.Name+i and value.Name.i does not work.
It seems to me what you're looking for is something like this:
myArr.push("<p>" + value['Name'+i] ," ", value['Nachname'+i] + "</p>")
This portion of javascript is covered pretty nicely here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Take the object property in a variable, use condition to check if it has value or not then concat it
var nameval = value.name;
then use in your javascript variable
nameval+i
You need to convert your i (integer value) to string prior to adding it.
use:
value.Name + i.toString()
here's the jfiddle link: http://jsfiddle.net/kpqmp49o/
I am trying to get the following working. It seemed to work initially, but somehow it stopped working
var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + eval("setCommonAttr")).value;
what is wrong with above?
The above code is little different from what I am trying to accomplish. I gave the above example just not to make things complicated here. Below is what I am trying to accomplish:
First I am getting an existing element as follows. The element is a
<tr id="row_1_4_2009_abc" class="rowclick">
<td></td>
</tr>
I am using jquery to get the id on click of a row:
$(".rowclick").click(function() {
var row_id = $(this).attr("id");
var getAttributes = row_id.split("_");
var setCommonAttr = getAttributes[1] + "_" + getAttributes[2] + "_" + getAttributes[3] + "_" + getAttributes[4];
var new_row_id = document.getElementById("new_row_" + setCommonAttr).value;
});
You shouldn't need eval() to do this. The value you want is already a variable in JavaScript. Try:
var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + setCommonAttr).value;
Will everything be in that form? row_xxx_xxx_xxx_xxx
if so, why not var new_row_id = document.getElementById("new_" + row_id).value;
You don't need to call eval().
You can just concatenate the string with the variable:
var setCommonAttr = "1_row1_common"; var val = document.getElementById("abc_" + setCommonAttr).value;