Topic: Improvement on xajax.getFormValues()

Is it possible to revise the xajax.getFormValues() as the following?

...
this.getFormValues(frm,submit) {
...
if(formElements[i].type == 'submit' && formElements[i].name!=submit)
        continue;
...
}

Because sometimes there might be multiple submit buttons with different names in a form, and the xajax.getFormValues() function should only include one 'submit' button.

2 (edited by edrobinson 2007-11-05 6:51:44 PM)

Re: Improvement on xajax.getFormValues()

How about if you setup your XAJAX function to take 2 parameters - the getFormValues array AND the button pressed?

<input type="submit" id="sub1", name="sub1" onclick="xajax_doSomething(getformValues('form1'), "sub1"); return false;">
<input type="submit" id="sub2", name="sub2" onclick="xajax_doSomething(getformValues('form1'), "sub2"); return false;">


function soSomething($data, $subbutton)
{
$resp = new xajaxResponse();   
if ($subbutton == 'sub1)
      DoFirstThing($resp);
  else if ($subbutton == 'sub2')
      DoSecondThing($resp);

...
return $resp;
}

Hope this helps. smile

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

Re: Improvement on xajax.getFormValues()

edrobinson wrote:

How about if you setup your XAJAX function to take 2 parameters - the getFormValues array AND the button pressed?

<input type="submit" id="sub1", name="sub1" onclick="xajax_doSomething(getformValues('form1'), "sub1"); return false;">
<input type="submit" id="sub2", name="sub2" onclick="xajax_doSomething(getformValues('form1'), "sub2"); return false;">


function soSomething($data, $subbutton)
{
$resp = new xajaxResponse();   
if ($subbutton == 'sub1)
      DoFirstThing($resp);
  else if ($subbutton == 'sub2')
      DoSecondThing($resp);

...
return $resp;
}

Hope this helps. smile

Thanks for your advice, that makes sense.
However, what if we just want to do a server-side forward in the xajax php script?
For example, the xajax php script may work as a 'proxy' which invokes other remote scripts and transform the result back to the client. (In this way, we may simulate a cross-site ajax call)
But since we can't change the behavior of the remote script, multiple submit buttons may confuse it.

Re: Improvement on xajax.getFormValues()

Hmm. Never had any experience with using a proxy.

How does the data get to the remote script? Wouldn't it need to be aware of ANY change in the data coming into it whether it is an additional parameter you add or the getFormValues() function adds?

You could also add the submit button identity to the form values array before forwarding it.

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

Re: Improvement on xajax.getFormValues()

Hi Edrobinson,

Thanks for your feedbacks. Let me explain the problem to you.

1. Sometimes, we need invoke some functions on a third party server. Since we can't change the source code of the third party script. We have to pass the same post parameters to it. For example, a third party script may work as the following:

if ( has post variable 'publish' ){
     //'publish' is the name of a submit button
     // do some thing
}

if ( has post variable 'cancel'){
    //'cancel' is the name of a submit button
    //do some thing
}

Since the xajax.getFormValues() returns an array including both submit buttons, if we post the array to server via socket connection, the server side script might be confused.

Just as you suggested, we may develop the xajax php script as the following:

do_post($callbackId, $data, $uri, $submit_name){
            //unset all submit buttons from $data except the $submit_name
            //post $data to $uri via socket
            //parse the http response
            //update the $callbackId element
}

But the problem is: $data does not record the type of each form element. So there is no way for us to know which one is submit button and which one is not.

So I believe the best way to do this is: revise the xajax.getFormValues(frm) to xajax.getFormValues(frm, submit)

Please feel free to get back to me with your comments.

Kind regards,

Michael

Re: Improvement on xajax.getFormValues()

Michael,

Now I see your problem.

The following page may help...

<?PHP
  /*
    Messing with getFormValues()
  */
  ob_start();
  require_once ('xajax/xajax_core/xajax.inc.php');
  $xajax = new xajax();
  
  function ProcessForm($dta, $button)
  {
    $resp = new xajaxResponse();
    $dta[$button] = $button;            //Add the buttton to the data array.
    $resp->alert(print_r($dta, true));  
    return $resp;
  }    

//  $xajax->setFlag('debug',true);
  $xajax->Register(XAJAX_FUNCTION,"ProcessForm");
  $xajax->ProcessRequest();  
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
  <head> 
     <title>"getFormValues Test"</title>
     <? $xajax->PrintJavaScript('xajax/'); ?>
  </head>
<body>
<center>
getFormValues Test<br><br>
<form id="form1" onsubmit="return false;">
<input id="v1" name="v1" value="V1"> <br>
<input id="v2" name="v2" value="V2"> <br>
<!-- Note that neither submit button has a name or id. Thus they do not get sent to the server -->
<input type="submit" value="Cancel" onclick="xajax_ProcessForm(xajax.getFormValues('form1'), 'cancel'); return false;"> 
<input type="submit" value="Update" onclick="xajax_ProcessForm(xajax.getFormValues('form1'), 'update'); return false;"> 
</center>
</form>
</body>
</html>

It prevents the submit buttons from being sent to the server an adds the button the the data array for sending to the remote.

Ed

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

Re: Improvement on xajax.getFormValues()

Hi edrobinson,

Thanks for your workaround. That's really smart smile  I will revise my code base according to your suggestions.

Kind regards,

Michael

Re: Improvement on xajax.getFormValues()

Hi Michael,

Glad to be of help.

Ed

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