Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1410
  • Last Modified:

JQuery - submit php via form onchange event.

Hi, the code below works fine but I want to check for error message when values in form change instead of when form is submited, can this be achieved with jQuery ajax ?

Thanks in advance for your feedback...

<div id="postage_div">
<?php
if($_POST) {
// GET the variables from the POST

$from_postcode = 2671; // wylong postcode
$to_postcode = $_POST['to_postcode'];
$length = $_POST['length']; // (cm)
$height = $_POST['height']; // (cm)
$width = $_POST['width']; // (cm)
$weight = $_POST['weight']; // kgs
}
else {
$to_postcode = '2000';
$length = '13'; // (cm)
$height = '5'; // (cm)
$width = '45'; // (cm)
$weight = '6'; // kgs
}
?>

<form method="post">

<table width="396">
<tr>
<td>Height (cm)</td>
<td><input type="text" name="height" value="<?php echo $height;?>"></td>
</tr>
<tr>
<td>Width (cm)</td>
<td><input type="text" name="width" value="<?php echo $width;?>"></td>
</tr>
<tr>
<td>Length (cm)</td>
<td><input type="text" name="length" value="<?php echo $length;?>"></td>
</tr>
<tr>
<td>Weight (kgs)</td>
<td><input type="text" name="weight" value="<?php echo $weight;?>"></td>
</tr>
</table>
<input type="submit" value="check dimensions" />
</form>
<hr />


<?php
if ($_POST) {

 include_once 'auspost_api.php';
 
$post_str = "https://auspost.com.au/api/postage/parcel/domestic/service.json?from_postcode=2000&to_postcode=3000&length=$length&height=$height&width=$width&weight=$weight";

$auspost_json = get_auspost_api($post_str);

//convert json to array
$auspost_array = json_decode($auspost_json,true);

//print_r($auspost_array);

$error_msg = $auspost_array['error']['errorMessage'];

echo $error_msg;

}

?>

Open in new window

0
sabecs
Asked:
sabecs
  • 3
  • 2
1 Solution
 
GaryCommented:
JQuery already has plugins for form validation, search Google for whatever suits you.
This would be one of the most popular ones
http://docs.jquery.com/Plugins/validation
0
 
Julian HansenCommented:
A plugin is useful but be aware that sometimes the plugin comes with a lot more functionality than is required. Depending on your requirements validation can be achieved with a few lines of jscript.

For instance if you just want to check for a field not being empty then you can do this
<input type="text" name="somename" class="required" value="" />
// Now the jscript
// Include JQuery library here or in head
<script type="text/javascript">
$(function() {
  $('#yourformid').submit(function () {
      $('.required').each (function() {
          if ($(this).val() == '') {
              alert('Field ' + $(this).attr('name') + ' is a required field');
              return false;
          }
      });
     return true;
  });
});
</script>

Open in new window

You can modify the above  depedning on the message type you want to deliver, whether you want to put the message in the form in a box in front of the form in a message box per error or all messages in one message box.

You can read more here
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/HTML/Q_27831313.html

If you need more complex validation (across different controls) or your validation needs to be more than just a blank or filled then a validation plugin may be more suited - comes down to need and proficiency in jscript.
0
 
sabecsAuthor Commented:
Thanks for your response, sorry for the confusion, I do not need form validation. I just need to submit the form and check the results ($error_msg) with an onchange event of any of the form fields.

Basically I need to run the code below onchange instead of form submitt button click.

<?php
if ($_POST) {

 include_once 'auspost_api.php';
 
$post_str = "https://auspost.com.au/api/postage/parcel/domestic/service.json?from_postcode=2000&to_postcode=3000&length=$length&height=$height&width=$width&weight=$weight";

$auspost_json = get_auspost_api($post_str);

//convert json to array
$auspost_array = json_decode($auspost_json,true);

//print_r($auspost_array);

$error_msg = $auspost_array['error']['errorMessage'];

echo $error_msg;

}

?> 

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Julian HansenCommented:
As an ajax call or as a post back

AJAX
$(function() {
   $('id_of_input_or_class_of_inputs').change(function() {
      $.post('postbackpage.php',{build list of vars you want sent back here}, function(data) {
         do_something_with_the_data(data);
      });
   });
});

Open in new window


Submit
$(function() {
   $('id_of_input_or_class_of_inputs').change(function() {
     $('id_of_form').submit();
   });
});

Open in new window

0
 
sabecsAuthor Commented:
Thanks Julian, much appreciated..
0
 
Julian HansenCommented:
you are welcome
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now