XML find each javascript request , another each in the main each - javascript

i've been working on android application with the help of phonegap framework .I've made 1 php -xml file to parse the data from my site to application. The problem i have is that when i click button , i want to get all the <vic></vic> separated of the clicked cat_id
Heres the link with xml file
http://smeshnoto.eu/rss/mobile_vicove_cats.php
At the moment i have this code:
function showVicoveKategorii(){
var xml = '';
$.ajax({
url: "http://smeshnoto.eu/rss/mobile_vicove_cats.php",
dataType: 'xml',
success: kategorii,
error: function () {
alert('Грешка в сървъра');
}
});
}
function kategorii(xml){
var obj = document.getElementById("vicove_kategorii_id");
$(xml).find('category').each(function(){
var catname = $(this).find('cat_name').text();
var catid = $(this).find('cat_id').text();
var titlevic = $(this).find('title_vic').text();
var descvic = $(this).find('desc_vic').text();
addContent(obj, ''+catname+'');
});
}
I want when i click the button the alert to be all <vic></vic> with <br> for example after every element , of course those <vic></vic> to be from the clicked category (cat_id)

The solution is to use http://smeshnoto.eu/rss/mobile_vicove_cats.php?cat_id=ChosenID
so you can get the items from the chosen id :)

Related

How to set download url in a table row

I've this javascript function that takes the ajax call's result and build a html table with it. This table rapresents list of reports.
Each row of a table is composed by report's filename and date of creation, and when I click over it a new browser page is opened showing me the document. But I want to download it. How can I get it?
Code:
function report_call(){
$.ajax({
type: 'GET',
url: "/rest/v2/reports/",
dataType: 'json',
success: function (data) {
$("#modal-body").html('')
const YOURREPORTS_ROW = "yourReportRow";
const YOURREPORTS_TABLE = "yourReports-table-content";
let table = $(`#${YOURREPORTS_TABLE}`);
table.html('')
for(const key in data) {
if(data[key].length == 0){
continue
}
data[key].forEach(report_type => {
let url = report_type[2] // take file url
let fname = report_type[0];
let fname_td = $(`<td>${fname}</td>`);
let date = report_type[1];
let date_td = $(`<td>${date}</td>`);
let onclick = "";
if(url !== ""){
onclick = `onclick="window.open('${url}', '_blank');"`;
}
let row = $(`<tr class="${YOURREPORTS_ROW}" [data-file="${fname}"] data-date="${date}" ${onclick}>`);
row.append(fname_td);
row.append(date_td);
table.append(row);
})
}
}
})
}
On the web I read that I can use "download" keyword in tag , but here the situation is different. I try in this way, it does not work.
Sorry for the ignorance.
Use an anchor tag with download attribute like below. You can also add another td for the download link. Remove the onClick event in row.
let date_td = $(`<td>${date} <a href=${url} download>Download</a></td>`);

Sending form data with extra parameters through Ajax?

I am trying to send a post request with the form data as well as some other parameters to my Codeigniter controller. This is the code I am using.
function onSignIn(googleUser) {
console.log('onto the function');
var profile = googleUser.getBasicProfile();
var google_name = profile.getName();
var google_image = profile.getImageUrl();
var google_email = profile.getEmail();
console.log('got the details');
console.log('submitting');
var title = ('#title').val();
var message = ('#message').val();
$.ajax({
type: "POST",
url: 'http://localhost/review/submit',
data: {
title,
message,
'google_name': google_name,
'google_email': google_email,
'google_image': google_image,
},
success: function () {
alert('fuck');
}
});
}
I keep getting $ is not defined and I have no idea why that is, cause when I remove $ it gives me #title.val() is not a function.
Make sure you are giving jquery reference with correct path to your code
Include bellow CDN link in your head section of code
https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js

Javascript works fine with a hard-coded string but not with variable

I have a problem I have bee struggling over all morning so I felt it was time to get some help! I have a javascript function which gets the value entered by a user into an autocomplete box, uses AJAX to send that value to a php script which queries the database and then populates the following box with the possible options. The problem is this all works fine when I hard-code in the selected option as so:
var selected="Ed Clancy";
but not when it pulls it from the box, as so:
var selected = this.getValue();
I have tried debugging this using an alert box and both boxes come up with the same string in them so I am completely puzzled! Any ideas? Full code below:
$(riderSelected).on('selectionchange', function(event){
var selected = this.getValue();
//var selected="Ed Clancy";
alert(selected);
$('#nap4').removeAttr('disabled');
$('#nap4').empty();
$('#nap4').append($("<option>-select-</option>"));
$.ajax({
type: "GET",
url: 'getbiketype.php',
data: { name: selected },
success: function(data) {
console.log(data);
$('#nap4').append(data);
}
});
});
Based on magicsuggest documentation - http://nicolasbize.com/magicsuggest/doc.html , you probably could do this
var selected = this.getValue()[0];
IF you do not allow multiple selection
Change your code as I have written below for you .
Code
$(riderSelected).on('change', function (event) {
var selected = this.value;
alert(selected);
$('#nap4').removeAttr('disabled');
$('#nap4').empty();
$('#nap4').append($("<option>-select-</option>"));
$.ajax({
type: "GET",
url: 'getbiketype.php',
data: {name: selected},
success: function (data) {
console.log(data);
$('#nap4').append(data);
}
});
});

Passing JSON through AJAX Plus Button Dilema

so I am attempting to pass some information in a JSON object and have a php page insert the data into a database. However, I am running into some trouble. The "update" button exists in a popup window. The user then clicks "update" and the inputted data should be processed accordingly. However, I fear that I am not even reaching my .click function. None of my alerts seems to be triggered. Below I will point out where issues are occurring. Thank you!
<script>
function updateTable()
{
document.getElementById("testLand").innerHTML = "Post Json";
//echo new table values for ID = x
}
$('#update').click( function() {
alert("help!");
var popupObj = {};
popupObj["Verified_By"] = $('#popupVBy').val();
popupObj["Date_Verified"] = $('#popupDV').val();
popupObj["Comments"] = $('#popupC').val();
popupObj["Notes"] = $('#popupN').val();
var popupString = JSON.stringify(popupObj);
alert(popupString);
#.ajax({
type: "POST",
dataType: "json",
url: "popupAjax.php",
//data: 'popUpString = '+ popupString,
data: popupObj,
cache: false,
success: function(data) {
updateTable();
alert("testing tests");
}
});
});
</script>
<html>
<button onClick="openPopup(<?php echo $row['ID'];?>);"><?php echo $row['ID'];?></button> <!--opens a popup with input options-->
<button id="update">Update</button> <!-- this button is supposed to cause the javascript above to run when clicked, however none of my alerts seem to be reached.-->
</html>
Thank you for looking!
1)I can only guess that you're trying to use JQUERY?
Where do you include the library?
2 )#.ajax isnt valid Jquery function
try $.ajax instead

How can I capture the attribute id from a dynamically generated html?

I am pretty sure this is not so complicated but I have been for hours trying to figure out how to catch the id of this dynamically generated anchor tags.
What I do in my code is that everytime a text input changes, theres an ajax request that goes to a php file and returns me a json array with the prices then I render this results of search in buttons that will be clickable to do other types of request but so far here's where I'm stuck.
heres's the code that loops through the array and renders this buttons (NOTE:The Id of the buttons are variables rendered by the function too.
$.ajax({
type: "POST",
url: "php/get_products.php",
data: {query:prod_qry},
success: function(data){
$('#loader_s').hide();
var jsarray = JSON.parse(data);
var length = jsarray.length;
for(i=0;i<jsarray.length;i++){
var index1 = i;
var index2 = Number(i++) + 1;
var index3 = Number(i++) + 2;
$('#modal-bod').append('<a onclick="renderProds();" class="btn btn-default-item prod_sel" style="margin-top:10px;" id="'+index3+'" data-dismiss="modal">'+jsarray[index1]+' <span class="pull-right" st>lps. '+jsarray[index2]+'</span></a>');
}
}
Then here's the function renderProds()
function renderProds(){
var id = $(this).attr('id');
alert(id);
}
the alert is just to try and catch the values for testing purposes, but what really goes there is another Ajax request.
The only thing I get here is that the var Id is undefined...
You can pass object like
function renderProds(obj) {
var id = obj.id;
alert(id);
}
Pass invoker object like
onclick="renderProds(this);"
I would do :
onclick="renderProds(this);"
function renderProds(that){
var id = that.id;
alert(id);
}
You use jQuery.. so USE jQuery !
Ajax can do the JSON.parse for you with just dataType: "json".
The inline onclick is bad practice.
Move the success function to make your code more readable.
$.ajax({
type: "POST",
url: "php/get_products.php",
data: {query:prod_qry},
dataType : 'json',
success: productsUpdate
});
function renderProds(event){
var id = $(event.target).attr("id");
alert("Id is:"+id);
}
function productUpdate(data){
$('#loader_s').hide();
for(i=0;i<data.length;i++){
var link = $('<a>....</a>');
link.click(renderProds);
$('#modal-bod').append(link);
}
}
Now, this is readable.
Complete the link creation with your code, without the onclick, remove the inline css and use a real css selector and finally, check this ugly Number(i++)+ .... it looks so bad.

Categories