1

I am trying to make a single php form to make some calculations, however when pressing the submit button nothing happens.

I am trying to use ajaxForm to do this

Code:

<?php

  if(isset($_POST['submit'], $_POST['v'], $_POST['e'], $_POST['ei'])){
$v = $_POST['v']; //Dyrets vægt
$v = pow($v, 0.75);
$s = (int)$_POST['s']; //Stofskifte
$e = (int)$_POST['e']; //Energibehov
$ei = (int)$_POST['ei']; //Energiindhold i foder

$vaegt = ($v);
$stofskift = round($vaegt * $s, 2);
$de = $stofskift * $e;
$eif = round($de / $ei * 100, 2);
$result = round($eif * 4.2, 2);

$arr = array( 'testDiv' => $result );
  echo json_encode( $arr );

 exit;
};

//$string = '(((('.$v.')*'.$s.')*'.$e.')/'.$ei.' * 100) * 4,2';

?>

<html>
<head>
<title>Hej</title>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>
<script type="text/javascript" src="jsFile.js"></script>
</head>
<body>
<form method="POST" action="" class="ajaxForm" >
<label for="vaegt">Weight</label><br>
<input type="text" name="v" id="v" placeholder="Weight"><br>
<label for="type">Art</label><br>
<select size="7" name="s" id="s"> 
  <option value="70" >Fugl</option>
</select><br><br>
<label for="str">Størrelse</label><br>
<select size="7" name="e" id="e"> 
  <option value="1.25" >Absolut</option>
</select><br><br>

<label for="foder">Foder</label><br>
<select size="4" name="ei" id="ei"> 
  <option value="60" >Plante</option>
</select><br><br>

<input type="submit" value="Udregn" name="submit" class="ajaxSubmit"/>

</form>

<div id="testDiv">result here</div>
</body>
</html>

jsFile.js:

jQuery(document).ready(function(){

    jQuery('.ajaxform').submit( function() {

        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        for(var id in data) {
                            jQuery('#' + id).html( data[id] );
                        }
                      }
        });

        return false;
    });

});
Rikesh
  • 26,156
  • 14
  • 79
  • 87
Emil Elkjær
  • 685
  • 1
  • 9
  • 31

1 Answers1

2

Problem #1:

In your if statement that handles a POST request, you output a bunch of JSON. But you forgot to exit output when you're done. So, your output looks like:

{"a":"b","c":"d"/* more json here*/}
<html>
<head>
/* more html here */

Which, obviously is not valid JSON. jQuery, if the response is not real JSON, calls the error method instead of the success method.

To fix the problem, add this line before the end of your PHP if statement:

exit;

That will quit the PHP script before all of the HTML is outputted.

Problem #2:

$(this).serialize() only grabs inputs that have values. This excludes buttons. So, unlike a normal form submit, the submit button is not included in the POST request. This is causing your first if statement to evaluate to false.

So, just remove the first of the nestled if statements (the if(isset($_POST['submit'])){ one) and it will work correctly.

Robbie Wxyz
  • 7,671
  • 2
  • 32
  • 47
  • Hope OP takes away soemthing from the advice on debugging in the browser if [nothing else](http://4.bp.blogspot.com/_D_Z-D2tzi14/S8TRIo4br3I/AAAAAAAACv4/Zh7_GcMlRKo/s400/ALOT.png). – ficuscr Mar 07 '14 at 20:45