1

I have a page where I use jQuery load() method to display a table of results based on a post request of some fields. But I need this load() to display the table and also inform javascript if a condition is met in the PHP script, so probably I need a json response. I don't know if it's possible to use the complete() callback to achieve that. I only need a single variable to pass from my PHP script to javascript.

I'm using load() because I believe other ajax methods need to do the HTML template part from javascript, am I right?

Hope I made myself clear, thanks

UPDATE1:

Here is my js code:

$("#listaNegraView").load('procesos/funcionesAjax.php', 
            {accion: 'listaNegra', 
            nombres: $("#nombres").val(), 
            apellidoP: $("#apellidoP").val(),
            apellidoM: $("#apellidoM").val(),
            nacimiento: $("#nacimiento").val()},
            function(data){console.log(data);}
            );

And here is PHP script:

case 'listaNegra':
            $_POST['nombres'] = mb_convert_encoding($_POST['nombres'], 'Windows-1252', 'UTF-8');
            $_POST['apellidoP'] = mb_convert_encoding($_POST['apellidoP'], 'Windows-1252', 'UTF-8');
            $_POST['apellidoM'] = mb_convert_encoding($_POST['apellidoM'], 'Windows-1252', 'UTF-8');
            $listaNegra = $personaDB->existsPersonaListaNegra($_POST);
$pct100 = false;
            if(!empty($listaNegra) && is_array($listaNegra)){
                foreach($listaNegra as &$persona){
                    $persona['match'] = '';
                    $porcentaje = 80;
                    if(strtolower($persona['nombres']) == strtolower($_POST['nombres'])){
                        $persona['match'] .= 'name';
                        $porcentaje += 10;
                    }
                    if($_POST['nacimiento'] == $persona['fecha_nacimiento']){
                        $persona['match'] .= 'date';
                        $porcentaje += 10;
                    }
                    $persona['porcentaje'] = $porcentaje;
if($porcentaje == 100)
                        $pct100 = true;
                }
                unset($persona);
            }
    include(ROOT.RUTA_TPL.'ventas/listanegra.tpl.php');
    break;

UPDATE 2:

Specifically the condition I want to pass to jasvascript is variable $pct100

7
  • 2
    What code have you already tried? Can you post that as well? Commented Jan 16, 2014 at 16:42
  • Do not rely on others to solve your hole issues. Show your code and people will try to help you! Commented Jan 16, 2014 at 16:47
  • Please check my question again, I didn't want to post my code because I have some other things not related with the question Commented Jan 16, 2014 at 16:49
  • Ok, and now, what is the PHP condition that you should make javascript code know? Commented Jan 16, 2014 at 16:52
  • The variable I want to pass to javascript is $pct100, added to my last edit Commented Jan 16, 2014 at 16:55

1 Answer 1

2

You are "directly" outputting HTML code so I think, as a quick workaround, you should write the $pct100 in a hidden field/dom element and then access it with the complete callback in your javascript code.

This is an example of what I am suggesting

$("#listaNegraView").load(
    'procesos/funcionesAjax.php', 
    {accion: 'listaNegra', 
     nombres: $("#nombres").val(), 
     apellidoP: $("#apellidoP").val(),
     apellidoM: $("#apellidoM").val(),
     nacimiento: $("#nacimiento").val()
    },
    function(data){
        $('#where-to-put-html-code').html(data);
        var pct100 = $('#where-to-put-html-code #hidden-field-id').val() == '1' ? true : false;
    }
);

Answer added by the suggestion of the asker.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this definitely will be the easiest solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.