Community
cancel
Showing results for 
Search instead for 
Did you mean: 
madson_g_
Beginner
77 Views

Problem with parseJSON

Jump to solution

How do I pass data via ajax to php using a graphic button? It has its id and when I click on it, I want to select some database data according to its span (Admin).

<a class="uib-graphic-button default-graphic-sizing default-image-sizing hover-graphic-button 
active-graphic-button default-graphic-button default-graphic-text widget uib_w_10 d-margins 
media-button-text-bottom" data-uib="media/graphic_button" data-ver="0" id="admin">
    <img src="images/administrativa.png">
    <span class="uib-caption">Admin</span>
</a>

javascript:

$(document).on("click", "#admin", function(evt)
{
    /* your code goes here */

    var dataString = "admin=";
    var $server = "http://localhost/project";

    function SelectAdm(){
        $.ajax({
            type: "post",
            url: $server+"/select.php",
            data: dataString,
            success: function(result, jqXHR){

                var vaga = $.parseJSON(result);

                $.each(vaga, function(index, item){
                    var list_item = '<li class="widget uib_w_17" data-uib="jquery_mobile/listitem" data-ver="0" data-icon="carat-r" id="listitem"><a href="#"><span>'+item.CARGO+'</span></a>';

                    $("#list").append(list_item);
                    activate_page("#listpage");

                });

            },
            error: function(jqXHR, status){
                alert("not working");
            }
        });
    }
    new SelectAdm();
});

php:

<?php 

include_once("conn.php");

if(isset($_POST['admin'])){

$adm = "administrativa";

$sql = $mysqli->query("SELECT * FROM vagas WHERE area = '".$adm."' ");

if($sql->num_rows > 0){
    while($row = $sql->fetch_array(MYSQL_BOTH)){
        $registro = array(
            "AREA" => $row['area'],
            "CARGO" => $row['cargo'],
            "ATIVIDADES" => $row['atividades'],
            "SALARIO_INI" => $row['salario_ini'],
            "POS_EXPERIENCIA" => $row['pos_experiencia'],
            "BENEFICIOS" => $row['beneficios'],
            "HORARIO" => $row['horario'],
            "ESCOLARIDADE" => $row['escolaridade'],
            "LOCAL" => $row['local'],
            "INFOS_ADICIONAIS" => $row['infos_adicionais']                      
        );

        $retorno[] = $registro;

    }       
}

$mysqli->close();
$retorno = json_encode($retorno);
echo $retorno;

}

?>

I used:

console.log(JSON.stringify(result));

below success: function(result, jqXHR){

and below: var vaga = $.parseJSON(result);

Console returned to me "Unexpected end of input" or Undefined variable retorno on $retorno = json_encode($retorno);

Tags (2)
0 Kudos
1 Solution
Diego_Calp
Valued Contributor I
77 Views

Hi,

Seems to me that the url sent by ajax is

http://localhost/project/select.php?admin=

And this way, this line in php will be false, because admin GET variable is empty

if(isset($_POST['admin'])

So the returned JSON will be an empty string.

Regards,

Diego

 

View solution in original post

10 Replies
Swati_S_Intel1
Employee
77 Views

The issue is with your php script. Please correct that. We can only guide you with the client side issues related to XDK.

Diego_Calp
Valued Contributor I
78 Views

Hi,

Seems to me that the url sent by ajax is

http://localhost/project/select.php?admin=

And this way, this line in php will be false, because admin GET variable is empty

if(isset($_POST['admin'])

So the returned JSON will be an empty string.

Regards,

Diego

 

View solution in original post

madson_g_
Beginner
77 Views

Diego Calp wrote:

Hi,

Seems to me that the url sent by ajax is

http://localhost/project/select.php?admin=

And this way, this line in php will be false, because admin GET variable is empty

if(isset($_POST['admin'])

So the returned JSON will be an empty string.

Regards,

Diego

 

 

How should I proceed? How to click on Admin graphic button and get only results corresponding to it? Create a String var with admin value and pass it?

Diego_Calp
Valued Contributor I
77 Views

Hi,

Yes, could be as you saying.

For example, if you click the Admin button the code in XDK could be something like:

 var dataString = "admin=1";

The php script is looking only if the POST variable has a value, but not what value. I don't know exactly how you want this to work, if you agree with the suggestion above, the line in php could be:

if($_POST['admin']==1){

This is the way to pass POST values, you can read it on your php and process it.

Hope this helps.

Regards,

Diego

madson_g_
Beginner
77 Views

I was doing right. My php was fine too. The problem was the text accents inside database table. After that I list the data on a listview. When I press the back button on header to repeat the process, my list item is losing its css. It shows just a common link. Why is this happening? Thanks in advance.

Diego_Calp
Valued Contributor I
77 Views

Hi,

Which is the code of the back button? Or is just a link to the former page?

Regards

Diego

madson_g_
Beginner
77 Views

I use activate_subpage(). If I use activate_page() it shows me a blank page. It´s weird because some time ago when I pointed to the page main id it was really fine.

$(document).on("click", "#listpage_back_btn", function(evt)
    {
         /*global activate_subpage */
         activate_subpage("#page_74_57");
         
    });

I tried to .empty() the list before loading it but I get a blank page.

Diego_Calp
Valued Contributor I
77 Views

Two options to try:

parent.history.back();

Instead the activate_subpage()

or

Convert your admin button click code in a function and call it before the activate_subpage().

Regards,

Diego

madson_g_
Beginner
77 Views

Diego Calp wrote:

Two options to try:

parent.history.back();

Instead the activate_subpage()

or

Convert your admin button click code in a function and call it before the activate_subpage().

Regards,

Diego

Both options are not working. I noticed that if I use

$('#list').append(list_item);

the css is working fine, but it keeps duplicating the list item everytime I repeat the process and the duplicated items have no css. I could not prevent append() from duplicate the item even if I use it outside the $.each(...)

                  

$.each(vaga, function(index, item){
                        
    list_item = '<li class="widget uib_w_17" data-uib="jquery_mobile/listitem" data-ver="0" data-icon="carat-r" id="listitem"><a href="javascript:ViewItem('+item.ID+');"><span>'+item.CARGO+'</span></a></li>';
                                                
});
                        
$('#list').append(list_item);
activate_subpage("#page_91_35");

How could I check if #list is empty? If so, then the code would append list_item. This could be a way of doing that?

madson_g_
Beginner
77 Views

madson g. wrote:

I was doing right. My php was fine too. The problem was the text accents inside database table. After that I list the data on a listview. When I press the back button on header to repeat the process, my list item is losing its css. It shows just a common link. Why is this happening? Thanks in advance.

It seems to be a framework problem. I changed from jquery to bootstrap and it worked as it should.

Reply