PHP/AJAX Form and Loop not working properly

Hi,

I’d like to start off by saying that I’m new here and would like to thank you for your help in advance. I’m new to PHP and AJAX, but has a small background and understanding of programming.

What I am trying to accomplish is to build a calculator. The user inputs a few parameters, hits a button and the result is outputted. The issue I’m having is that when I hit the button nothing happens and I get a POST 500 error (Internal Server Error).

I’ve tried everything and have no idea why this won’t work.

Here is my HTML code for the form

  	<div class="twelve columns">
  		<h2>Moment Resistance Using Strain-Stress Relationship</h2>
  		<form method="post" action="return.php" class="js-ajax-php">
  		<fieldset>
	  		<legend>Step 1</legend>
				<div class="row">
		    		<div class="four columns">
		    			<label for="fc">Concrete Compressive Strength - f'<sub>c</sub> [MPa]</label>
		    			<input class="u-full-width" type="text" placeholder="e.g. 30" id="fc" name="fc">
		    		</div>
		    		
		    		<div class="four columns">
		    			<label for="As">Reinforcing Bars Size - Tension</label>
		    			<select class="u-full-width" id="As" name="As">
		    				<option value="" disabled="" selected="">Select rebar size</option>
		    				<option value="100">10M - 100 mm^2</option>
		    				<option value="200">15M - 200 mm^2</option>
		    				<option value="300">20M - 300 mm^2</option>
		    				<option value="500">25M - 500 mm^2</option>
		    				<option value="700">30M - 700 mm^2</option>
		    				<option value="1000">35M - 1000 mm^2</option>
		    				<option value="1500">45M - 1500 mm^2</option>
		    				<option value="2500">55M - 2500 mm^2</option>	    				
		    			</select>
		    		</div>
		
					<div class="four columns">
		    			<label for="bar-num">Number of Reinforcing Bars Per Row</label>
		    			<input class="u-full-width" type="text" placeholder="e.g. 6" id="bar-num" name="bar-num">
		    		</div>
				</div>
				
				<div class="row">
					<div class="four columns">
						<label for="fy">Steel Yield Strength - f<sub>y</sub> [MPa]</label>
						<input class="u-full-width" type="text" name="fy" placeholder="e.g. 400" id="fy">
					</div>
					
					<div class="four columns">
						<label for="b">Beam Width - b/b<sub>w</sub> [mm]</label>
						<input class="u-full-width" type="text" name="b" placeholder="e.g. 300" id="b">
					</div>
					
					<div class="four columns">
						<label for="h">Beam Height - h [mm]</label>
						<input class="u-full-width" type="text" placeholder="e.g. 450" id="h" name="h">
					</div>
				</div>
				
				<div class="row">
					<div class="four columns">
						<label for="bf">Flange Width - b<sub>f</sub> [mm]</label>
						<input class="u-full-width" type="text" placeholder="e.g. 450" id="d" name="bf">
					</div>
	
					<div class="four columns">
						<label for="hf">Flange Depth - h<sub>f</sub> [mm]</label>
						<input class="u-full-width" type="text" placeholder="e.g. 450" id="hf" name="hf">
					</div>

<!--
					<div class="four columns">
						<label for="c">C Value [mm]</label>
						<input class="u-full-width" type="text" placeholder="e.g. 450" id="c" name="c">
					</div>
-->

				</div>
		</fieldset>
		
		<fieldset>
			<legend>Step 2</legend>
				<div class="row">
					<div class="four columns">
						<label for="ds">Effective Depth for Each Row - d<sub>s</sub> [mm]</label>
						<input class="u-full-width" type="text" placeholder="e.g. 450" id="ds1" name="ds[]">
						<input class="u-full-width" type="text" placeholder="e.g. 450" id="ds2" name="ds[]">
					</div>
					
				</div>
		</fieldset>	

			<div class="row">
				<div class="two columns">
					<input class="button-primary" type="submit" value="Calculate">
				</div>
				
				<div class="two columns">
					<input class="button-primary" type="reset" value="Reset">
				</div>
				
				<div class="four columns u-max-full-width">
					<div class="the-return2">
						<p>Moment Resistance:</p>
						<p>0.00 kN.m <br />
						The reinforcing steel …!</p>
					</div>
				</div>
			</div>
    	</form>
   	</div> 

Here is the code for the PHP script:

[php]

<?php if (is_ajax()) { if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists $action = $_POST["action"]; switch($action) { //Switch case for value of action case "test": test_function(); break; case "test2": new_function(); break; } } } //Function to check if the request is an AJAX request function is_ajax() { return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; } //Moment Resistance using Strain-Stress Relationship function new_function(){ $return=$_POST; $fc = $_POST["fc"]; $fy = $_POST["fy"]; $b = $_POST["b"]; $h = $_POST["h"]; $bf = $_POST["bf"]; $hf = $_POST["hf"]; $ds = $_POST["ds"]; /* $c = $_POST["c"]; */ $c = 0; $alpha1 = 0.85 - 0.0015 * $fc; $beta1 = 0.97 - 0.0025 * $fc; $phis = 0.85; $phic = 0.65; $epsilonu = 0.0035; while ($c < 82) { for ($i = 0; $i < count($ds); $i++) { //Calculate strain in each layer of steel $epsilon[$i] = -$epsilonu * ($ds[$i] - $c) / $c; //Calculate stress in each layer of steel [MPa] if ($epsilon[$i] >= -0.002) { $fs[$i] = 200000 * $epsilon[$i]; } else { $fs[$i] = -380 + 10000 * $epsilon[$i]; } //Adjust Stress in Steel to yielding values if this occurs if ($fs[$i] > $fy) { $fs[$i] = $fy; } elseif ($fs[$i] < -$fy) { $fs[$i] = -$fy; } else { ; //Do nothing } //Calculate area of steel in each layer [mm2] $bar[$i] = $_POST["As"] * $_POST["bar-num"]; //Calculate the tension force in each layer of steel [kN] $Ts[$i] = $fs[$i] * $phis * $bar[$i] * pow(10,-3); } $Ts_Total = array_sum($Ts); //Calculate the compression force of the stress block [kN] $a = $beta1 * $c; $Cc[] = array(0,0,0); if ($a <= $hf) { $Cc[0] = $phic * $alpha1 * $fc * $a * $bf * pow(10,-3); } else { $Cc[1] = $phic * $alpha1 * $fc * $hf * ($bf - $b) * pow(10,-3); $Cc[2] = $phic * $alpha1 * $fc * $a * $b * pow(10,-3); } $Cc_Total = array_sum($Cc); $c = $c + 1; } //Calculate Mr [kN.m] $Mr[0] = ($Cc[0] * ($h/2 - $a/2) + $Cc[1] * ($h/2 - $hf/2) + $Cc[2] * ($h/2 - $a/2)) * pow(10,-3); //Mr for compressive stress for ($i = 0; $i < count($ds); $i++) { //Mr for tension stress $Mr[$i+1] = $Ts[$i] * ($h/2 - $ds[$i]) * pow(10,-3); } $Mr_Total = array_sum($Mr); $return["yield"] = "Yields"; $return["Mr"] = $Mr_Total; $return["Cc"] = $Cc_Total; $return["Ts"] = $Ts_Total; $return["c"] = $c-1; $return["a"] = $a; $return["json"] = json_encode($return); echo json_encode($return); } ?>

[/php]

Here is the code for the AJAX:

$(".js-ajax-php").submit(function(){
    var data = {
      "action": "test2"
    };
    data = $(this).serialize() + "&" + $.param(data);
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "script/response.php", //Relative or absolute path to response.php file
      data: data,
      success: function(data) {
        $(".the-return2").html(
			"<p>Moment Resistance:</p> "
			+ "<p>" + data["Mr"].toFixed(2) + " kN.m"
			+ "<br /> The reinforcing steel " + data["yield"] + "!</p>"

			+ "<br />JSON: " + data["json"] 
        );
      }
    });
    return false;
  });

I just don’t understand why the code breaks as soon as I start using a while loop.

You can see a online version here: http://dev.gelias.ca

Any help will be greatly appreciated. Thank you.

My suggestion to you would be to get the php portion working properly and once you get that working, adding the AJAX and JavaScript is just a matter of a few modifications to the php and tweaks to the HTML. Basically all Ajax is doing is creating it where the user doesn’t see the refreshing of the page. In my opinion it would be easier.

I don’t understand why you’re looping in the first place? I would think that user enters a few things and press the submit (Enter) button it just would figure out the math (php portion would) and spit out the answer. If you want to have a memory function like a true calculator I would look into using sessions (php.net).

Thank you Strider64 for your help.

So I managed to run the PHP code first and made sure that this worked the way I wanted to. This can be seen here: http://dev.gelias.ca/test2.php

[php]

<?php print_r("Script has started!
"); $fc = 30; $fy = 400; $b = 500; $h = 600; $bf = 1800; $hf = 140; $ds = array(450,510); $c = 0; $error = 0; $alpha1 = 0.85 - (0.0015 * $fc); $beta1 = 0.97 - (0.0025 * $fc); $phis = 0.85; $phic = 0.65; $epsilonu = 0.0035; while ($error <= 0.0001) { for ($i = 0; $i < count($ds); $i++) { //Calculate strain in each layer of steel $epsilon[$i] = -$epsilonu * ($ds[$i] - $c) / $c; //Calculate stress in each layer of steel [MPa] if ($epsilon[$i] >= -0.002) { $fs[$i] = 200000 * $epsilon[$i]; } else { $fs[$i] = -380 + 10000 * $epsilon[$i]; } //Adjust Stress in Steel to yielding values if this occurs if ($fs[$i] > $fy) { $fs[$i] = $fy; } elseif ($fs[$i] < -$fy) { $fs[$i] = -$fy; } else { ; //Do nothing } //Calculate area of steel in each layer [mm2] $bar[$i] = 3000; //Calculate the tension force in each layer of steel [kN] $Ts[$i] = $fs[$i] * $phis * $bar[$i] * pow(10,-3); } $Ts_Total = array_sum($Ts); //Calculate the compression force of the stress block [kN] $a = $beta1 * $c; $Cc = array(0,0,0); if ($a <= $hf) { $Cc[0] = $phic * $alpha1 * $fc * $a * $bf * pow(10,-3); } else { $Cc[1] = $phic * $alpha1 * $fc * $hf * ($bf - $b) * pow(10,-3); $Cc[2] = $phic * $alpha1 * $fc * $a * $b * pow(10,-3); } $Cc_Total = array_sum($Cc); $c = $c + 1; $error = ($Ts_Total + $Cc_Total) / $Cc_Total; } //Calculate Mr [kN.m] $Mr[0] = ($Cc[0] * ($h/2 - $a/2) + $Cc[1] * ($h/2 - $hf/2) + $Cc[2] * ($h/2 - $a/2)) * pow(10,-3); //Mr for compressive stress for ($i = 0; $i < count($ds); $i++) { //Mr for tension stress $Mr[$i+1] = $Ts[$i] * ($h/2 - $ds[$i]) * pow(10,-3); } $Mr_Total = array_sum($Mr); print_r(array_values($Cc)); echo("
"); print_r(array_values($Ts)); echo("

Mr Value: "); print_r($Mr_Total); echo("
"); print_r("Script Finished!"); ?>

[/php]

Now I want to take that but instead of predefining the variables at the top, I would like the user to input them in the form. This still doesn’t seem to work. http://dev.gelias.ca second form.

Resolved. Thank you again Strider64 for your help.

Sponsor our Newsletter | Privacy Policy | Terms of Service