1 (edited by neovea 2013-01-13 10:01:16 PM)

Topic: PHP function execution via xajax and event

Hi everybody, I try to execute a php function with an event, but I still have an error

Code php :

require_once('class/class.mysql_use.php');
require_once('xajax/xajax_core/xajax.inc.php');

//instanciation de la classe
$my_cnx = new mysql_use();
$xajax = new xajax();

$xajax->configure('javascript URI', '/xajax');
$xajax->configure("debug",true);
$xajax->register(XAJAX_FUNCTION,'getResults');

$xajax->processRequest();

function getResults()
{
    global $my_cnx, $champs, $table, $conditions, $orderby, $limit, $page;
    $resultats='';

    $recup = $my_cnx->exe_select_query($champs, $table ,$conditions ,$orderby ,$limit);

    while($row=mysql_fetch_row($recup))
    {
        $resultats.=    '<div class="row well resultats">
                            <div class="span2">'.strtoupper($row[1]).'</div>
                            <div class="span2">'.ucwords($row[2]).'</div>
                            <div class="span4"><a href="mailto:'.$row[4].'">'.$row[4].'</a></div>
                            <div class="span1"><a href="#"><img src="img/flags/'.getflag($row[12]).'.png" alt="#" /></a></div>    
                            <div class="span1"><a href="info_contact.php?uid='.$row[0].'&key='.$key.'&page='.$page.'" id="consulter"><i class="icon-eye-open size16"></i></a></div>
                            <div class="span1"><a href="modif_contact.php?uid='.$row[0].'&key='.$key.'&page='.$page.'" id="modifier"><i class="icon-pencil size16"></i></a></div>
                            <div class="span1"><a href="delete_contact.php?uid='.$row[0].'&key='.$key.'&page='.$page.'" id="effacer"><i class="icon-remove size16 red"></i></a></div>
                        </div>';
    }
    $objResponse = new xajaxResponse();
    $objResponse->assign('resultats','innerHTML',$resultats);
    return $objResponse;
}

code html :

<head>
<?php
// appel des fonction d'ajout de javascript dans la page

$xajax->printJavascript();
?>
</head>

<body>
<button type="button" class="btn" onClick="xajax_getResults();">afficher</button>

<article>
    <div id="resultats"></div>
</article>

</body>


And the error

ERROR: ResponseReceived: SyntaxError: Unexpected token <

Thanks a lot for helping a newbie smile

Re: PHP function execution via xajax and event

Hi,

Well I looked at it 'till my eyes hurt and could not see the problem. I suspect a wayward character or punctuation in the loop that builds your output...

I would suggest breaking up the html in the function into more readable strings.

Also turn on the debugger so you can see what is coming back from the server.

$xajax->configure('debug', true);

The error indicates that the error is coming from the client side xajax code. It doesn't like what it "sees."

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: PHP function execution via xajax and event

Here is my code withe the barely necessary, the probleme still remains

<?php
require_once('class/class.mysql_use.php');
require_once('xajax/xajax_core/xajax.inc.php');

//instanciation de la classe
$my_cnx = new mysql_use();
$xajax = new xajax();
// appel des fonction initiales pour l'utilisation de xajax
$xajax->configure('javascript URI', '/xajax');
$xajax->configure("debug",true);
$xajax->register(XAJAX_FUNCTION, 'getResults');

// appel des fonction d'ajout de javascript dans la page
$xajax->processRequest();

    $conditions="WHERE ";
    $orderby=" ORDER BY `nom` ASC";
    $champs='`uid`,`nom`,`prenom`,`email`,`pays`';
    $table='contact';

    function getResults()
    {
        global $my_cnx, $champs, $table, $page, $key;
        $objResponse=new xajaxResponse();
        $resultats='';
        $recup = $my_cnx->exe_select_query($champs, $table);
        while($row=mysql_fetch_row($recup))
        {
            $resultats.=    '<div class="row well resultats">
                                <div class="span2">'.strtoupper($row[1]).'</div>
                                <div class="span2">'.ucwords($row[2]).'</div>
                                <div class="span4"><a href="mailto:'.$row[3].'">'.$row[3].'</a></div>
                                <div class="span1"><a href="#"><img src="img/flags/'.$row[4].'.png" alt="#" /></a></div>    
                                <div class="span1"><a href="info_contact.php" id="consulter"><i class="icon-eye-open size16"></i></a></div>
                                <div class="span1"><a href="modif_contact.php" id="modifier"><i class="icon-pencil size16"></i></a></div>
                                <div class="span1"><a href="delete_contact.php" id="effacer"><i class="icon-remove size16 red"></i></a></div>
                            </div>';
        }
        $objResponse->assign('resultats', 'innerHTML', $resultats);
        return $objResponse;
    }
?>
<!DOCTYPE html>
<html>
<head>
    <script>
        function refresh()
        {
            xajax_getResults();
            setTimeout(refresh, 5000);
        }
    </script>
    <?php 
        $xajax->printJavascript(); /* Fonction qui va afficher le Javascript de la page */ 
    ?>
</head>
<body>
    <article>
    <?php echo '
    <div class="row well entete">
        <div class="span2"><a href="index.php">Nom</a></div>
        <div class="span2"><a href="index.php">Prénom</a></div>
        <div class="span4"><a href="index.php">Email</a></div>
        <div class="span1"><a href="index.php">Pays</a></div>
        <div class="span1">Consulter</div>
        <div class="span1">Modifier</div>
        <div class="span1">Supprimer</div>
    </div>';?>
    <div id="resultats"></div>
</article>
</body>
    <script>
        refresh();
    </script>
</html>

and my sql request

<?php
    require_once('data.php');
    //on initialise la classe
    class mysql_use
    {
        private $cnx;            
        //on initialise la fonction de construction de classe et on renseigne les variables de connexion avec les données de connexion de la base de données
        function __construct()
        {            
            global $host, $user, $pwd, $bdd, $cnx; //on déclare les variables de connexion
            //on initialise la variable $cnx avec la fonction de connexion, et on donne les variables de connexion en parametres
            $this->cnx = mysql_connect($host, $user, $pwd);
            
            //on definit le charset de connexion, utf8
            mysql_set_charset('utf8', $this->cnx);
            
            //on selectionne la base de donnée à laquelle on va se connecter (renseignée dans la variable $bdd)
            mysql_select_db($bdd);
        }

        //on initialise la fonction de dé-construction de la classe
        function __destruct()
        {            
            //on initialise la fonction de fermeture de connexion a la bdd $cnx
            mysql_close($this->cnx);
        }

        
        //on déclare une nouvelle fonction publique qui va aller sélectionner les éléments souhaités selon des parametres précis
        public function exe_select_query($champs, $table, $conditions='', $orderby='', $limit='')
        {
            $select_sql = "SELECT ".$champs." FROM `".$table."` ".$conditions.$orderby.$limit;//on initialise la variable qui va contenir la requete sql en langage sql
            $resultat= mysql_query($select_sql);//on stock la requete sql dans une variable
            $num_rows = mysql_num_rows($resultat);//recupération du nombre de lignes renvoyées
            return $resultat;
        }
    }
?>

Re: PHP function execution via xajax and event

What does the debugger show?

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.