1 (edited by akhenathon 2014-03-28 12:18:58 PM)

Topic: cannot pass string variable from one xajax function to another

Hi everybody!!!

I'm glad to say that I discovered xajax few days ago an it is indeed a huge discovery!

The thing is I have a problem passing a string variable from one xajax function (seleccionarTipoConsumible to another xajax function (insertarCatalizador, meaning that it works fine with int variables but it does not with strings.

function insertarCatalizador($asto)
{
    $mon.="<p>nombreCatalizador->'$asto'</p>";
         
    $objResponse = new xajaxResponse();
    $objResponse->assign('div2', 'innerHTML', $mon);

    return $objResponse;
}
   
function seleccionarTipoConsumible() // I call to this function from the main script
{
       
    $lol="5"; // this is the variable I wanna pass, it doesn't work with string type variables though.
           // Eventually it would pass the contents of nombreCatalizador text input using either   xajax.getFormValues(insertarNuevoCatalizador)
           // or document.getElementById(nombreCatalizador)
           
    $mon.="<form id='insertarNuevoCatalizador' name='insertarNuevoCatalizador' method='post' action='insertar.php'>";
    $mon.="<table border='1'>";
    $mon.="<tr><td><b>Nombre Catalizador</b></td></tr>";
    $mon.="<tr><td><input type='text' name='nombreCatalizador' id='nombreCatalizador'/></td></tr>";
        $mon.="<tr><td>";
       
            $mon.="<input type='button' name='insertarNuevoCatalizador' value='Insertar' onclick='xajax_insertarCatalizador($lol)'/>";
                   
        $mon.="</td></tr>";
    $mon.="</table>";
    $mon.="</form>";
           
    $objResponse = new xajaxResponse();
    $objResponse->assign('div1', 'innerHTML', $mon);
    return $objResponse;
}

Gotta say the function seleccionarTipoConsumible is already a xajax function called from the main script.

I think the problem migth be related with the quotes in my code but I tried, I think, every way I could, putting double quotes, single quotes, not putting them at all...

Is there a way to do what I am trying to achieve???

Thanks in advance.

ps: sorry my english

Re: cannot pass string variable from one xajax function to another

Hi,

Welcome to the forum.

seleccionarTipoConsumible() is an xajax function and to call it from another xajax function you need to use appendResponse() in xajax 0.5 or addCommands() in 0.6.

$objResponse ->appendResponse(another_function_returning_a_ response(); //xajax v0.5

$objResponse ->addCommands(another_function_returning_a_ response();  //xajax v0.6

The response object normally is returned to the processRequest() function for processing back to the browser. The appendResponse and addCommands functions allow one xajax function to call another xajax function and have it's output added to the caller's response object.

Here's the basics of how xajax works:
processRequest() intercepts the incoming xmlhttp  object from the browser, calls the indicated registered function expecting an xajaxResponse object to be returned. It then process the response object and returns the info the the browser where the client side javascript part of xajax receives and processes the info.

To see what is happening, turn on the xajax debugger - $xajax->configure('debug',true);
Then you can see the entire transaction in the debug window.

I noticed this line: $mon.="<p>nombreCatalizador->'$asto'</p>";

I believe it should be $mon .= "<p>".nombreCatalizador->asto."</p>"; assuming that asto is a public variable.

Hope this helps. If not post back and we can work on it some more.

Ed

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

3 (edited by akhenathon 2014-03-29 12:35:55 PM)

Re: cannot pass string variable from one xajax function to another

thanks a lot Ed!

I have to say that "seleccionarTipoConsumible" is not called from a xajax function but from the main script. Anyways, "insertarCatalizador" is a xajax function called from another xajax function so your explanation is  great.  But here I have a question.

Although using appendResponse for calling a xajax function from another xajax function, how can I pass a variable from "seleccionarTipoConsumible" to "insertarCatalizador"?

could this be the code?

$objResponse ->appendResponse(seleccionarCatalizador(document.getElementById('nombreCatalizador'))); //xajax v0.5

And what I'd like is to execute that line from a button event, I mean:

<input type='button' name="xxx" id="xxx" value="bot" onclick="xajax_seleccionarCatalizador(document.getElementById('nombreCatalizador'))" />

Re: cannot pass string variable from one xajax function to another

Well, you can't use document.getElementById() on the server as it is a Javascript function.

In the php code using appendResponse() you CAN pass a value to seleccionarCatalizador() as a simple parameter.

$objResponse ->appendResponse(seleccionarCatalizador(5) or

$objResponse ->appendResponse(seleccionarCatalizador("5") depending on what it is expectng - string or number.

Using appendResponse(), the called function works just as when called by processRequest().

Ed

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

Re: cannot pass string variable from one xajax function to another

Thanks a lot Ed!

I made my experiments with your advices and they work perfectly!!! But, I'm very sorry and I don't mean to waste any of your time but I think I haven't explained my problem properly. So, first of all, I'm going to copy my code in here.

<?php
require ('../../../../../xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->configure('javascript URI', '../../../../../xajax/');

function showCarColour($colour)
{
    $mon.="<p>This is the colour of the car --> $colour</p>";
   
    $objResponse = new xajaxResponse();
    $objResponse->assign('div2', 'innerHTML', $mon);

    return $objResponse;
}

function showBreed($breed)
{
    $mon.="<p>This is the breed --> $breed</p>";
   
    $objResponse = new xajaxResponse();
    $objResponse->assign('div2', 'innerHTML', $mon);

    return $objResponse;
}

function showItem($selection)
{
    if($selection==1)
    {
        $mon.="<input type='text' name='insertColour' id='insertColour'/>";
        $mon.="<input type='button' value='Insert Car Colour' onClick='xajax_showCarColour(3)'/>";
//with integer it works, but instead of the number 3, I would like to pass the value of "insertColour" textbox, if possible "document.getElementById('insertColour').value"
    }
    else
    {
        $mon.="<input type='text' name='insertBreed' id='insertColour'/>";
        $mon.="<input type='button' value='Insert Breed' onClick='xajax_showBreed(4)'/>";
//with integer it works, but instead of the number 4, I would like to pass the value of "insertBreed" textbox, if possible "document.getElementById('insertBreed').value"
    }
   
    $objResponse = new xajaxResponse();
    $objResponse->assign('div1', 'innerHTML', $mon);

    return $objResponse;
}

$xajax->registerFunction('showItem');
$xajax->registerFunction('showCarColour');
$xajax->registerFunction('showBreed');
$xajax->processRequest();

?>

<html>
    <head>
        <title>Hello World!!!</title>
        <?php
            // output the xajax javascript. This must be called between the head tags
            $xajax->printJavascript();
        ?>
    </head>
    <body>
        <select name="selection" id="selection" onchange="xajax_showItem(document.getElementById('selection').value)">
            <option value="----">----</option>
            <option value="1">car</option>
            <option value="2">dog</option>
        </select>
       
        <div id="div1">
           
        </div>
       
        <div id="div2">
           
        </div>
    </body>
</html>

So, "insertColour" and "insertBreed" are not called from a xajax function but from the main script and I can't pass to those functions any variable (except integer), let alone the text I would like to get from the textboxes.
And again sorry, I don't want to be a pain in the ass...

Re: cannot pass string variable from one xajax function to another

No pain! I am retired and have lots of time on my hands...

I messed with your code and came up with this:

<?php
require ('xajax/xajax_core/xajax.inc.php'); //Change to fit your setup
$xajax = new xajax();
$xajax->configure('javascript URI', 'xajax/'); //change to fit your setup
//$xajax->configure('debug',true); //Uncomment and refresh page to see xajax debug info

function showCarColour($colour)
{
    $mon = " <p>This is the colour of the car --> $colour</p>";
    
    $objResponse = new xajaxResponse();
    $objResponse->assign('div2', 'innerHTML', $mon);
    return $objResponse;
}

function showBreed($breed)
{
    $mon = "<p>This is the breed --> $breed</p>";
    
    $objResponse = new xajaxResponse();
    $objResponse->assign('div2', 'innerHTML', $mon);
    return $objResponse;
}

function showItem($selection)
{
    if($selection==1)
    {
        $mon  = '<input type="text" name="colour" id="colour"/>';
        $mon .= '<input type="button" value="Car Colour" onclick="xajax_showCarColour(document.getElementById(\'colour\').value)"/>';
    }
    else
    {
        $mon  = '<input type="text" name="breed" id="breed"/>';
        $mon .= '<input type="button" value="Dog Breed" onclick="xajax_showBreed(document.getElementById(\'breed\').value)"/>';
    }
    
    $objResponse = new xajaxResponse();
    $objResponse->assign('div1', 'innerHTML', $mon);
    return $objResponse;
}
$xajax->register(XAJAX_FUNCTION,'showItem');
$xajax->register(XAJAX_FUNCTION,'showCarColour');
$xajax->register(XAJAX_FUNCTION,'showBreed');
$xajax->processRequest();
?>
<html>
    <head>
        <title>Hello World!!!</title>
        <?php $xajax->printJavascript();?>
    </head>
    <body>
        <select name="selection" id="selection" onchange="xajax_showItem(document.getElementById('selection').value)">
            <option value="----">----</option>
            <option value="1">car</option>
            <option value="2">dog</option>
        </select>
        
        <div id="div1">
            
        </div>
        
        <div id="div2">
            
        </div>
    </body>
</html>

I think it is what you are trying to do.

Ed

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

7 (edited by akhenathon 2014-04-01 7:11:11 AM)

Re: cannot pass string variable from one xajax function to another

That was exactly what I was trying to do! Thanks A LOT!!!
My guess is the lack of slashes was the problem...

Anything you need don't hesitate to ask and I'm going to do my best to help you. I've seen you live in Boise, and I know there is a great community of basque people there. If you wanna surprise any basque friend tell me and we will figure it out.

Thanks again!

Re: cannot pass string variable from one xajax function to another

To answer the original problem, the code was
$mon.="<input type='button' name='insertarNuevoCatalizador' value='Insertar' onclick='xajax_insertarCatalizador($lol)'/>";

and it needed to be
$mon.="<input type='button' name='insertarNuevoCatalizador' value='Insertar' onclick='xajax_insertarCatalizador('$lol')'/>";
(extra quotes around the $lol variable)
When it is rendered to the page, the variable is calculated, and a  string variable without the quotes would have resulted in something like this...
<input type='button' name='insertarNuevoCatalizador' value='Insertar' onclick='xajax_insertarCatalizador(this is text)'/>

Re: cannot pass string variable from one xajax function to another

thanks pfleming!!!

Re: cannot pass string variable from one xajax function to another

pfleming wrote:

To answer the original problem, the code was
$mon.="<input type='button' name='insertarNuevoCatalizador' value='Insertar' onclick='xajax_insertarCatalizador($lol)'/>";

and it needed to be
$mon.="<input type='button' name='insertarNuevoCatalizador' value='Insertar' onclick='xajax_insertarCatalizador('$lol')'/>";
(extra quotes around the $lol variable)
When it is rendered to the page, the variable is calculated, and a  string variable without the quotes would have resulted in something like this...
<input type='button' name='insertarNuevoCatalizador' value='Insertar' onclick='xajax_insertarCatalizador(this is text)'/>

good afternoon everybody!

I tried the given solution and I'm sorry to say that the it doesn't work... if I put those quotes I get the next result in the browser.

the code:
           

$var="auritz";
           $mon.="<input type='button' value='ejecutarXajax' onclick='xajax_ponerDosFunciones('$var')' />";

the result (this is what I get):
           

<input type="button" value="ejecutarXajax" onclick="xajax_ponerDosFunciones(" auritz')'>

what am  I doing wrong? or how can I pass a string variable from a xajax function to another xajax function properly?


Thanks in advance!!!