0

In the index.php file, I have done some code in chart.js, which is drawn triangles. Everything is fine until I try to use it dynamically, it is either takes the default values via javascript or just zeroes via PHP. I load values with form such as that on the picture the main view and the code

HTML
<form method="get" action="index.php"  >


<div class="form-group">
  <label for="Tpoint1"  class="form-label">Перша точка основи трикутника</label><br>
  <div class="row" id="Tpoint1">
    <div class="col-4"><input type="number" value=6 name="Tpoint1x" class="form-control"></div>
    <div class="col-2">
      <p>X</p>
    </div>  <div class="col-4"><input type="number" value=20  name="Tpoint1y" class="form-control">
    </div><div class="col-2">
      <p>Y</p>
    </div>
  </div>
  <label for="Tpoint2"  class="form-label">Друга точка вершини трикутника</label><br>
  <div class="row" id="Tpoint2">
    <div class="col-4"><input  type="number" value=50   name="Tpoint2x" class="form-control"></div>
    <div class="col-2">
      <p>X</p>
    </div>  <div class="col-4"> <input type="number" value=300 name="Tpoint2y" class="form-control">
    </div><div class="col-2">
      <p>Y</p>
    </div>
  </div>
  <label for="Theight"  class="form-label">Висота трикутника</label><br>
  <input type="number" id="Theight"  name="Theight" value=60 class="form-control">
  <button id = "TriangleSubmition"type="submit" name="drawTriangle" class="btn btn-primary">Намалювати трикутник</button>
  </div>
</form >

with javascript, it brings me always the same values enter image description here

JavaScript
 var number1x =+ document.forms[0][0].value;
var number1y =+ document.forms[0][1].value;
var number2x =+document.forms[0][2].value;
var number2y =+ document.forms[0][3].value;
var theight =+ document.forms[0][4].value;

using PHP, either post or get method it is always 0 Here below is the PHP code getting values from the form

    <script  >
   <?php

       $x1 = ($_GET["Tpoint1x"]);
        $y1 = ($_GET["Tpoint1y"]);
       $x2 = ($_GET["Tpoint2x"]);
        $y2 = ($_GET["Tpoint2y"]);
        $h = ($_GET["Theight"]); 
     ?>

var number1x = +"<?php $x1;?>";
var number1y = +"<?php $y2;?>";
var number2x =+"<?php $x2;?>";
var number2y = +"<?php $y2;?>";
var theight = +"<?php $h;?>";
var Xpoint3 = 0;
var Ypoint3 = 0;

The picture as follows on the error.php file No matter have I "Get" values from the form or not, it is always the same with PHP code

error: Uncaught SyntaxError: Unexpected token '<'

console output in my index.php file:

values which take value from the form

As you have seen, I just try to assign these values to variables which I will use in the chart.js code

As follows

The code below I have added for some specific issues on your side. You can just skip this

  <script  >
   <?php

       $x1 = ($_GET["Tpoint1x"]);
        $y1 = ($_GET["Tpoint1y"]);
       $x2 = ($_GET["Tpoint2x"]);
        $y2 = ($_GET["Tpoint2y"]);
        $h = ($_GET["Theight"]); 
     ?>

var number1x = +"<?php $x1;?>";
var number1y = +"<?php $y2;?>";
var number2x =+"<?php $x2;?>";
var number2y = +"<?php $y2;?>";
var theight = +"<?php $h;?>";
var Xpoint3 = 0;
var Ypoint3 = 0;
  if(number1x == number2x ){
    var Ypoint3x = ((number1x+number2x)/2)
    Ypoint3 = ((number1y+number2y)/2)
     Xpoint3 = Ypoint3x+theight;
  }
  if(number1y == number2y ){
  var Ypoint3x = ((number1x+number2x)/2)
  Ypoint3 = ((number1y+number2y)/2)
   Xpoint3 = Ypoint3x+theight;
  }
  else{
  Xpoint3 = number1x
  Ypoint3 = number2y
  }
var  xyValues = [
   {x:Xpoint3, y:Ypoint3},
   {x:number1x, y:number1y},
   {x:number2x, y:number2y},
   {x:Xpoint3, y:Ypoint3}
 ];



  let myChart = document.getElementById('canvas').getContext('2d');
  let MassPopChart = new Chart(myChart, {

      type: 'scatter',
      data: {
        datasets: [{
          type: 'line',
             data:   xyValues,
           fill:{
                       },
            borderColor: 'rgb(75, 192, 192)'

     }]},
 options: {
   responsive: true,
   plugins: {
     title: {
       display: true,
       text: 'Triagles :) :( :| '
     }
   },

   scales: {
     x: {
       min: -500,
       max: 500,

       ticks: {
          // forces step size to be 50 units
          stepSize: 50
        }
     },
     y: {

       min: -1000,
       max: 1000,
       ticks: {
          // forces step size to be 50 units
          stepSize: 25
        }
     }
   }
 }
  });

  </script>
  • 3
    you probably want to echo the php variables... i.e. `var number1x = +"";` – berend Jun 03 '21 at 19:50
  • You need to check whether the form has been submitted before trying to use the `$_GET` variables. You'll get those warnings about undefined index when you load the page the first time. – Barmar Jun 03 '21 at 19:52
  • Don't use the archaic `document.forms[0][0].value;` way of access form fields. Give the inputs `id` attributes and then use `getElementById()`. – Barmar Jun 03 '21 at 19:54
  • @berend I am not very strong in forms to send data, and any solutions to the problem will be fine. – Svitlana Kuievda Jun 03 '21 at 19:54
  • See @berend's comment above: `var number1x = +"";` should be `var number1x = +"";` or `var number1x = +"= $x1;?>";` (if short echo is enabled in your PHP installation). – kmoser Jun 03 '21 at 19:55
  • @Barmar I always get those warnings, no matter. – Svitlana Kuievda Jun 03 '21 at 19:58
  • @kmoser Yes, and I think the short echo doesn't enable. But I have also a problem with the form values. After the form is submitted it is loads the whole page!!! – Svitlana Kuievda Jun 03 '21 at 20:01
  • The problem is that the PHP warnings are being written to the output, so they end up in the JavaScript, causing syntax errors there. – Barmar Jun 03 '21 at 20:02
  • @Barmar ok :) I don't understand you wholly. But anyway if I can solve this with plain JavaScript or another way, that will be great!!! There is the photo with console output. PHP doesn't write values to variables – Svitlana Kuievda Jun 03 '21 at 20:06
  • There is the photo with console output. PHP doesn't write values to variables. And I don't exactly understand where the problem is, either in short echo or in PHP code. – Svitlana Kuievda Jun 03 '21 at 20:12
  • @kmoser how can I enable short echo? – Svitlana Kuievda Jun 03 '21 at 20:13
  • Can It be done asynchronously???? Or is it possible? – Svitlana Kuievda Jun 03 '21 at 20:15
  • 1
    Does this answer your question? [Check whether $_POST-value is empty](https://stackoverflow.com/questions/9154719/check-whether-post-value-is-empty) –  Jun 03 '21 at 20:15
  • 1
    @OrHasade You don't need short echo. Just use ``. – kmoser Jun 03 '21 at 20:34
  • @jabaa yes, exactly. But not at all! – Svitlana Kuievda Jun 04 '21 at 04:58
  • 1
    Why not? You're trying to access indexes in `$_GET` that don't exist. That causes the warnings in the generated HTML code: `Notice: Undefined Index: ...` –  Jun 04 '21 at 07:05
  • @Jabaa you mean `$_POST ` is checking if desirable value exits or not, only? – Svitlana Kuievda Jun 04 '21 at 10:05
  • No, I mean in the other question you can see how to check if a value exists. The other question is about `$_POST` and your question about `$_GET` but it's the same logic. –  Jun 04 '21 at 10:15
  • @berend I haven't understood you from the very beginning. So, you just mean thats the variable assignment from PHP to JavaScript must be through `echo`? It does not just work through "simple assignment", does it? – Svitlana Kuievda Jun 04 '21 at 15:19
  • @jabaa thank you a lot! `if(isset($_POST['Tpoint1x'])){ $x1 = ($_POST["Tpoint1x"]);$y1 = ($_POST["Tpoint1y"]);$x2 = ($_POST["Tpoint2x"]);$y2 = ($_POST["Tpoint2y"]);$h = ($_POST["Theight"]); }` without this check chart.js is not visible – Svitlana Kuievda Jun 04 '21 at 16:09
  • @berend I got this, thank you for the tip. With `echo` it works fine. But my variables after submitting in the form reset to default. It is not very emergency, but the way things are supposed to be! – Svitlana Kuievda Jun 04 '21 at 16:20
  • Anyway, maybe there are some solutions with just only plain JavaScript I believe! – Svitlana Kuievda Jun 04 '21 at 16:21

0 Answers0