1 (edited by johnjohn 2011-10-16 11:07:26 AM)

Topic: Validate and then submit form to external url

Hi,

I have a problem with doing a validation locally and then submitting the form to an external url.

I manage to validate the form successfully using the xajax example (formSubmissionTest.php).
However, how do i submit the form after it's validated?

I'm using
<input type="submit" name="submit" id="submit" value="Open Account"
onclick = "xajax_processForm(xajax.getFormValues('leadform')); return false;"/>

as well as doing a check in the validation function that if there are no errors to submit the form

    if ($bError == false) {
        $objResponse->script('document.getElementByID(\'leadform\').submit()');
    }

However, it's not working.

So i guess i need another javascript to do the job if everything's ok.

Basically what i want is, after the validation to let the form continue / submit as if there was no "return false" statement to stop it.

How do i do that?

Thanks,

John

Re: Validate and then submit form to external url

Hi John,

I would handle the validation and processing in one call.

function validateForm($data){
  $s = '';
  if (condition1)
   $s .= "Error message 1\n";
  if (condition_n)
   $s .= "Error message n\n";

  return $s;
}

function processForm($data){
 $resp = new xajaxResponse();
 $s = validateForm($data);
 if ($s != ''){
   $resp->alert($s)
   return $resp;
}

Continue with the form processing

 return $resp; //Finally return the response obj.
}

Hope this helps...

Ed

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

Re: Validate and then submit form to external url

Thanks Ed,

Validation i'm doing ok and is similar to your code.

It's how to let the form continue posting to another domain script that i can't do.

Basically, i want to somehow "remove" the "return false" after calling the validation script in onSubmit so that the form continues posting to the external url.

John

Re: Validate and then submit form to external url

Yeah. If you remove the return false the page reloads...

I'm not sure why your script submitting the form doesn't work.

I try to avoid the submit button and use a simply input type="button" onclick=... then I don't need to worry about the return false or anything.

When you call a registered function passing the form values using getFormvalues() you are not really submitting the form. The xajax JS reads the form data into an array and passes it in to the function.

$objResponse->script("document.getElementByID('leadform').submit()"); should work. I have had problems with the escaped single quotes at times...

I'll try a test script and get back.

Ed

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

Re: Validate and then submit form to external url

This seems to work:

<?php
/*
  10/16/11 Sample page for johnjohn
  johnjohn.php
*/

//Setup the xajax framework.
include_once("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax();
$xajax->configure('javascript URI', 'xajax/');

function validate($data){
  $s = '';
  if ($data['name'] == '')
    $s .= "Missing Name\n";
  if ($data['address'] == '')
    $s .= "Missing Address\n";
  
  if ($s != '')
    $s = "Please fix the following:\n" .$s;
  return $s;
}

function processform($data){
  $resp = new xajaxResponse();
  $s = validate($data);
  if ($s != ''){
    $resp->alert($s);
  }
  else{
    $resp->script("xajax.$('form1').submit();");
  }
  return $resp;
}

$xajax->register(XAJAX_FUNCTION,'processform');
$xajax->processRequest();
?>
<!DOCTYPE html>
<html>
<head>
<?php $xajax->printJavascript() ?>
</head>
<body>
<form id="form1" action="t2.php" method="get">
<label>Name</label><input id="name" name="name"/><br/>
<label>Address</label><input id="address" name="address"/><br/>
<input type="button" value="Submit" onclick="xajax_processform(xajax.getFormValues('form1'))"/>
</form>
</body>
</html>

Take it for a test drive...

Ed

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

Re: Validate and then submit form to external url

This is what i thought and tested initially but for some reason it didn't work.
I'll test your code.

Thanks!

Re: Validate and then submit form to external url

Validation works ok.
However it won't proceed with submission.

$resp->script("xajax.$('form1').submit();");

this doesn't seem to work

Re: Validate and then submit form to external url

Found it!

Apparently naming a button or submit element as "submit" is not good idea

I had this from before and after i removed the name / id it worked

<input type="button" name="submit" id="submit">

Re: Validate and then submit form to external url

Ed, i've noticed in your posts that you're a fan of separating the validation into two functions, validate and process.
This works smoothly when you are generating an text message alert in validation that you alert in processform function.

However, how do i display / assign error messages for each field's innerHTML?
Do i need another function for that?

Re: Validate and then submit form to external url

My code works as written. No need to assign a name or id but it shouldn't make any difference.

As to using a seperate validation, I suppose you could do it the processing function instead of having a seperate validation function. Then you could do the assigns inline and return the response if errors were found.

Ed

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