Jump to content

Recommended Posts

So, the app I try to code it's for a school project, but the teacher failed to explain anything and wanted a working app in 2 days from now. I chose to make an electromagnet calculator but I don't understand why it doesn't work, from my point of view I wrote the code correctly but when I press the calculate button nothing happens. Most of the line codes are normal HTML and equations written in PHP, the two functions I made are for checking the closest value to the calculated dcu from a database that contains 2 columns: dcu and dcuiz, after that store them inside the dcu and dcuiz variables. At the end i want the program to display the d1,d2,d3,h,g and N variables, but the text is always displayed as "echo $ d1 ....", not as i wanted to show the calculated values. Please help and thank you in advance for helping me

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Calculator electromagnet</title>
  <div class="ele-calculator">
    <a href="https://imgbb.com/"><img src="https://i.ibb.co/hZLHzMG/download.png" alt="download" border="0" /></a>
    <h2>Calculator electromagnet</h2>
      <section>
        <fieldset>
          <div class="row">
            <label for="ele-raport">Raportul h/(d2-d1)</label>
            <select id="ele-raport" class="ele-raport" name="ele-raport">
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
            </select>
          </div>
          <div class="row ele-inductie-row">
            <label for="Inductia">Inductia (T)</label>
            <input type="text" id="ele-inductie" class="ele-inductie" name="ele-inductie" size="2" maxlength="10" value="" />
          </div>
          <div class="row ele-forta-row">
            <label for="ele-forta">Forta (N)</label>
            <input type="text" id="ele-forta" class="ele-forta" name="ele-forta" size="2" maxlength="10" value="" />
          </div>
          <div class="row">
              <label for="ele-intrefier">Intrefierul (mm)</label>
              <input type="text" id="ele-intrefier" class="ele-intrefier" name="ele-intrefier" size="2" maxlength="10" value="" />
            </div>
          <div class="row last">
            <input type="submit" id="ele-calc" class="ele-calc" name="ele-calc" value="Calculeaza" />
          </div>
        </fieldset>
      </section>
    </div>
</head>
<body>
  <?php

      // Function to get the closest value of dcu to the calculated one
    function closestValueTo($calculated_dcu, $db_file) {
      $db = new SQLite3($db_file);
      $query = "SELECT dcu FROM table ORDER BY ABS(dcu - $calculated_dcu) LIMIT 1";
      $results = $db->query($query);
      if($results){
          $row = $results->fetchArray();
          return $row['dcu'];
      } else {
          return null;
      }
    }

    // Function to get a specific value from the database
    function getValueFromDB($dcu, $db_file, $column) {
      $db = new SQLite3($db_file);
      $query = "SELECT $column FROM table WHERE dcu = '$dcu'";
      $results = $db->query($query);
      if($results){
          $row = $results->fetchArray();
          return $row[$column];
      } else {
          return null;
      }
    }

  if(isset($_GET['ele-calc'])){
    $Forta = $_GET['ele-forta']; // value for Forta
    $Inductia = $_GET['ele-inductie']; // value for Inductia
    $Intrefierul = $_GET['ele-intrefier']; // value for Intrefierul

    $mSelect = $_GET['ele-raport'];
    if ($mSelect === '3') {
      $raport = 3;
    }
    if ($mSelect === '4') {
      $raport = 4;
    }
    if ($mSelect === '5') {
      $raport = 5;
    }

    // Equation A1
    $A1 = (2* 1.257*pow(10,-6)*$Forta)/pow($Inductia, 2);

    // Equation d1
    $d1 = sqrt((4*$A1)/3.14);

    // Equation A1 (repeated)
    $A1 = (pow($d1, 2)* 3.14)/4;

    // Equation F1
    $F1 = ($A1*pow($Inductia, 2))/ (2* 1.257*pow(10,-6));

    // Equation Sol
    $Sol = ($Inductia * $Intrefierul) / ( 0.7 * 1.257*pow(10,-6));

    // Equation h
    $h = pow (((5* 2.115 * pow (10, -8) * pow ($Sol, 2) * 0.5)/(2*4*40/100* 40), 1/3));

    // Equation d2
    $d2 = $h/$raport  $d1;

    // Equation d3
    $d3 = sqrt((pow($d1, 2)/0.8 + pow($d2,2)));

    // Equation g
    $g = $d2  $d1;

    // Equation dcu
    $dcu = sqrt((4* 2.115 * pow (10, -8) * ($d1 + $d2) * $Sol)/ 24);

    // Compare DCU with values from a .db file
    $dcuiz = closestValueTo($dcu, 'sarma.db');

    // Get dcuiz from database
    $dcuiz = getValueFromDB($dcuiz, 'sarma.db', 'dcuiz');

    // Equation Nst
    $Nst = $h/$dcuiz;

    // Equation N
    $N = (4*40/100 * $g* $h)/ 3.14 * pow($dcu);

    echo "d1: " . $d1 . "<br>";
    echo "d2: " . $d2 . "<br>";
    echo "d3: " . $d3 . "<br>";
    echo "g: " . $g . "<br>";
    echo "h: " . $h . "<br>";
    echo "N: " . $N . "<br>";
  }
  ?>
  <form>
    <!-- Form fields and submit button go here -->
    <div id="result" style="display:none;"></div>
  </form>
  <script>
        document.getElementById("ele-calc").addEventListener("click", function(){
            if(isset($_GET['ele-calc'])){
                // Code for calculating variables, such as $d1, $d2, $d3, $g, $h, and $N
                var result = "d1: " + <?php echo $d1; ?> + "<br> d2: " + <?php echo $d2; ?> + "<br> d3: " + <?php echo $d3; ?> + "<br> g: " + <?php echo $g; ?> + "<br> h: " + <?php echo $h; ?> + "<br> N: " + <?php echo $N; ?>;
                document.getElementById("result").innerHTML = result;
                document.getElementById("result").style.display = "block";
            }
        });
    </script>
  </body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/315810-begginer-php-web-app-doesnt-work/
Share on other sites

26 minutes ago, bogdanserban3105 said:

So, the app I try to code it's for a school project, but the teacher failed to explain anything and wanted a working app in 2 days from now. I chose to make an electromagnet calculator but I don't understand why it doesn't work, from my point of view I wrote the code correctly but when I press the calculate button nothing happens. Most of the line codes are normal HTML and equations written in PHP, the two functions I made are for checking the closest value to the calculated dcu from a database that contains 2 columns: dcu and dcuiz, after that store them inside the dcu and dcuiz variables. At the end i want the program to display the d1,d2,d3,h,g and N variables, but the text is always displayed as "echo $ d1 ....", not as i wanted to show the calculated values. Please help and thank you in advance for helping me

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Calculator electromagnet</title>
  <div class="ele-calculator">
    <a href="https://imgbb.com/"><img src="https://i.ibb.co/hZLHzMG/download.png" alt="download" border="0" /></a>
    <h2>Calculator electromagnet</h2>
      <section>
        <fieldset>
          <div class="row">
            <label for="ele-raport">Raportul h/(d2-d1)</label>
            <select id="ele-raport" class="ele-raport" name="ele-raport">
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
            </select>
          </div>
          <div class="row ele-inductie-row">
            <label for="Inductia">Inductia (T)</label>
            <input type="text" id="ele-inductie" class="ele-inductie" name="ele-inductie" size="2" maxlength="10" value="" />
          </div>
          <div class="row ele-forta-row">
            <label for="ele-forta">Forta (N)</label>
            <input type="text" id="ele-forta" class="ele-forta" name="ele-forta" size="2" maxlength="10" value="" />
          </div>
          <div class="row">
              <label for="ele-intrefier">Intrefierul (mm)</label>
              <input type="text" id="ele-intrefier" class="ele-intrefier" name="ele-intrefier" size="2" maxlength="10" value="" />
            </div>
          <div class="row last">
            <input type="submit" id="ele-calc" class="ele-calc" name="ele-calc" value="Calculeaza" />
          </div>
        </fieldset>
      </section>
    </div>
</head>
<body>
  <?php

      // Function to get the closest value of dcu to the calculated one
    function closestValueTo($calculated_dcu, $db_file) {
      $db = new SQLite3($db_file);
      $query = "SELECT dcu FROM table ORDER BY ABS(dcu - $calculated_dcu) LIMIT 1";
      $results = $db->query($query);
      if($results){
          $row = $results->fetchArray();
          return $row['dcu'];
      } else {
          return null;
      }
    }

    // Function to get a specific value from the database
    function getValueFromDB($dcu, $db_file, $column) {
      $db = new SQLite3($db_file);
      $query = "SELECT $column FROM table WHERE dcu = '$dcu'";
      $results = $db->query($query);
      if($results){
          $row = $results->fetchArray();
          return $row[$column];
      } else {
          return null;
      }
    }

  if(isset($_GET['ele-calc'])){
    $Forta = $_GET['ele-forta']; // value for Forta
    $Inductia = $_GET['ele-inductie']; // value for Inductia
    $Intrefierul = $_GET['ele-intrefier']; // value for Intrefierul

    $mSelect = $_GET['ele-raport'];
    if ($mSelect === '3') {
      $raport = 3;
    }
    if ($mSelect === '4') {
      $raport = 4;
    }
    if ($mSelect === '5') {
      $raport = 5;
    }

    // Equation A1
    $A1 = (2* 1.257*pow(10,-6)*$Forta)/pow($Inductia, 2);

    // Equation d1
    $d1 = sqrt((4*$A1)/3.14);

    // Equation A1 (repeated)
    $A1 = (pow($d1, 2)* 3.14)/4;

    // Equation F1
    $F1 = ($A1*pow($Inductia, 2))/ (2* 1.257*pow(10,-6));

    // Equation Sol
    $Sol = ($Inductia * $Intrefierul) / ( 0.7 * 1.257*pow(10,-6));

    // Equation h
    $h = pow (((5* 2.115 * pow (10, -8) * pow ($Sol, 2) * 0.5)/(2*4*40/100* 40), 1/3));

    // Equation d2
    $d2 = $h/$raport  $d1;

    // Equation d3
    $d3 = sqrt((pow($d1, 2)/0.8 + pow($d2,2)));

    // Equation g
    $g = $d2  $d1;

    // Equation dcu
    $dcu = sqrt((4* 2.115 * pow (10, -8) * ($d1 + $d2) * $Sol)/ 24);

    // Compare DCU with values from a .db file
    $dcuiz = closestValueTo($dcu, 'sarma.db');

    // Get dcuiz from database
    $dcuiz = getValueFromDB($dcuiz, 'sarma.db', 'dcuiz');

    // Equation Nst
    $Nst = $h/$dcuiz;

    // Equation N
    $N = (4*40/100 * $g* $h)/ 3.14 * pow($dcu);

    echo "d1: " . $d1 . "<br>";
    echo "d2: " . $d2 . "<br>";
    echo "d3: " . $d3 . "<br>";
    echo "g: " . $g . "<br>";
    echo "h: " . $h . "<br>";
    echo "N: " . $N . "<br>";
  }
  ?>
  <form>
    <!-- Form fields and submit button go here -->
    <div id="result" style="display:none;"></div>
  </form>
  <script>
        document.getElementById("ele-calc").addEventListener("click", function(){
            if(isset($_GET['ele-calc'])){
                // Code for calculating variables, such as $d1, $d2, $d3, $g, $h, and $N
                var result = "d1: " + <?php echo $d1; ?> + "<br> d2: " + <?php echo $d2; ?> + "<br> d3: " + <?php echo $d3; ?> + "<br> g: " + <?php echo $g; ?> + "<br> h: " + <?php echo $h; ?> + "<br> N: " + <?php echo $N; ?>;
                document.getElementById("result").innerHTML = result;
                document.getElementById("result").style.display = "block";
            }
        });
    </script>
  </body>
</html>

 

image.png.44d1ba4d7eb6085baf99f81cbbf157be.png

the red square is where i wanted to see the calculated results but this is all it displays all the time

17 hours ago, bogdanserban3105 said:
<script>
        document.getElementById("ele-calc").addEventListener("click", function(){
            if(isset($_GET['ele-calc'])){
                // Code for calculating variables, such as $d1, $d2, $d3, $g, $h, and $N
                var result = "d1: " + <?php echo $d1; ?> + "<br> d2: " + <?php echo $d2; ?> + "<br> d3: " + <?php echo $d3; ?> + "<br> g: " + <?php echo $g; ?> + "<br> h: " + <?php echo $h; ?> + "<br> N: " + <?php echo $N; ?>;
                document.getElementById("result").innerHTML = result;
                document.getElementById("result").style.display = "block";
            }
        });
    </script>

 

  • I would do what @ginerjm suggested and make this script a self posting form.  Wrap the html form elements in a standard form tag of type post.  Get your variables from $_POST and not $_GET.  Otherwise it should work in similar fashion.
  • Your markup as presented is very messed up.  You have an entire body inside the head tag, and then you try and create another html document for your output.  You can't do that.  You only should have one html/head/body tag.
  • Another problem with your current code, is that php runs "serverside" and not "clientside".  You can not intermix php and javascript this way (as you did in the snippet I quoted).  The javascript code is delivered statically to the client when the page is requested.  If you use the chrome debug tools for example, you should see that your javascript function is invalid.  
  • When people want dynamic data, they use ajax, which is a level of complexity higher, so to keep your project simple, just use the standard html form, which will post back to the php script on submit, do the calculation (serverside in your php script) and re-render your output.  For simplicity, I've just shown a simple div/pre as you already are trying to echo out variables for debugging purposes.
  • Notice that I moved your blocks around, with your functions at the start of the script.  Functions are typically included prior to your logic, and it makes the flow of control clearer when you are intermixing html and php in this way.  

Something similar to this should work (I editted your code, but don't guarantee that there are no errors).

<?php
      // Function to get the closest value of dcu to the calculated one
    function closestValueTo($calculated_dcu, $db_file) {
      $db = new SQLite3($db_file);
      $query = "SELECT dcu FROM table ORDER BY ABS(dcu - $calculated_dcu) LIMIT 1";
      $results = $db->query($query);
      if($results){
          $row = $results->fetchArray();
          return $row['dcu'];
      } else {
          return null;
      }
    }

    // Function to get a specific value from the database
    function getValueFromDB($dcu, $db_file, $column) {
      $db = new SQLite3($db_file);
      $query = "SELECT $column FROM table WHERE dcu = '$dcu'";
      $results = $db->query($query);
      if($results){
          $row = $results->fetchArray();
          return $row[$column];
      } else {
          return null;
      }
    }
?>

<!DOCTYPE html>
<html lang="en">
	<head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Calculator electromagnet</title>
  </head>
  <body>
  <form method="post">
  <div class="ele-calculator">
    <a href="https://imgbb.com/"><img src="https://i.ibb.co/hZLHzMG/download.png" alt="download" border="0" /></a>
    <h2>Calculator electromagnet</h2>
      <section>
        <fieldset>
          <div class="row">
            <label for="ele-raport">Raportul h/(d2-d1)</label>
            <select id="ele-raport" class="ele-raport" name="ele-raport">
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
            </select>
          </div>
          <div class="row ele-inductie-row">
            <label for="Inductia">Inductia (T)</label>
            <input type="text" id="ele-inductie" class="ele-inductie" name="ele-inductie" size="2" maxlength="10" value="" />
          </div>
          <div class="row ele-forta-row">
            <label for="ele-forta">Forta (N)</label>
            <input type="text" id="ele-forta" class="ele-forta" name="ele-forta" size="2" maxlength="10" value="" />
          </div>
          <div class="row">
              <label for="ele-intrefier">Intrefierul (mm)</label>
              <input type="text" id="ele-intrefier" class="ele-intrefier" name="ele-intrefier" size="2" maxlength="10" value="" />
            </div>
          <div class="row last">
            <input type="submit" id="ele-calc" class="ele-calc" name="ele-calc" value="Calculeaza" />
          </div>
        </fieldset>
      </section>
    </div>
</form>
<?php
  if(isset($_POST['ele-calc'])){
    $Forta = $_POST['ele-forta']; // value for Forta
    $Inductia = $_POST['ele-inductie']; // value for Inductia
    $Intrefierul = $_POST['ele-intrefier']; // value for Intrefierul

    $mSelect = $_POST['ele-raport'];
    if ($mSelect === '3') {
      $raport = 3;
    }
    if ($mSelect === '4') {
      $raport = 4;
    }
    if ($mSelect === '5') {
      $raport = 5;
    }

    // Equation A1
    $A1 = (2* 1.257*pow(10,-6)*$Forta)/pow($Inductia, 2);

    // Equation d1
    $d1 = sqrt((4*$A1)/3.14);

    // Equation A1 (repeated)
    $A1 = (pow($d1, 2)* 3.14)/4;

    // Equation F1
    $F1 = ($A1*pow($Inductia, 2))/ (2* 1.257*pow(10,-6));

    // Equation Sol
    $Sol = ($Inductia * $Intrefierul) / ( 0.7 * 1.257*pow(10,-6));

    // Equation h
    $h = pow (((5* 2.115 * pow (10, -8) * pow ($Sol, 2) * 0.5)/(2*4*40/100* 40), 1/3));

    // Equation d2
    $d2 = $h/$raport  $d1;

    // Equation d3
    $d3 = sqrt((pow($d1, 2)/0.8 + pow($d2,2)));

    // Equation g
    $g = $d2  $d1;

    // Equation dcu
    $dcu = sqrt((4* 2.115 * pow (10, -8) * ($d1 + $d2) * $Sol)/ 24);

    // Compare DCU with values from a .db file
    $dcuiz = closestValueTo($dcu, 'sarma.db');

    // Get dcuiz from database
    $dcuiz = getValueFromDB($dcuiz, 'sarma.db', 'dcuiz');

    // Equation Nst
    $Nst = $h/$dcuiz;

    // Equation N
    $N = (4*40/100 * $g* $h)/ 3.14 * pow($dcu);
    
    echo "<div><pre>";
    echo "d1: " . $d1 . "<br>";
    echo "d2: " . $d2 . "<br>";
    echo "d3: " . $d3 . "<br>";
    echo "g: " . $g . "<br>";
    echo "h: " . $h . "<br>";
    echo "N: " . $N . "<br>";
    echo "</pre></div>";
  }
?>
  </body>
</html>
  

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.