0

I have problem. I have a code that writes name, surname, birth, church, phone, mail into the database. However, when the user is under 18, I need to get in touch with my parents using the phonep, mailp variables.

Here is the counter in JS:

    if (birth_date2 <= new Date()) {
            if (birth_date <= new Date()) {
            $(document).ready(function(){
                $("#par").slideUp();
            });
            <?php $more = 0;?>;
        } else {
            $(document).ready(function(){
                $("#par").slideDown();
            });
            <?php $more = 1;?>;
        }
    } else {
        alert("Minimum age is 12!");
        document.getElementById("birth").value = "";
    }

This code determines whether the user is an adult or not:

<?php $more = 0;?>;

Then I have PHP code that I tried to modify using the orientation of the variables:

   if($more === 1) {
       if(!($_POST["mailp"])) {
           $err++;
           $echo_er .= "Není vyplněno pole s Emailem rodičů! ";
       } else {
           $mailp = htmlspecialchars($_POST["mailp"]);
       }
       if(!($_POST["phonep"])) {
           $err++;
           $echo_er .= "Není vyplněno pole s telefonním číslem rodičů! ";
       } else {
           $phonep = htmlspecialchars($_POST["phonep"]);
       }
   }
   $sql = "SELECT * FROM registrace";
   $conn->query($sql);
   if($more === 1) {
       if($err === 0) {
           $sql = "INSERT INTO registrace (name, surname, birth, church, phone, mail, phonep, mailp)
           VALUES ('$name', '$surname', '$birth', '$church', '$phone', '$mail', '$phone', '$mailp')";
           $conn->query($sql);
           echo "<p class='pyes'> Registration comlete! </p>";
       } else {
           echo "<p class='pno'>" . $echo_er . "</p>";
       }
   } else {
       if($err === 0) {
           $sql = "INSERT INTO registrace (name, surname, birth, church, phone, mail, phonep, mailp)
           VALUES ('$name', '$surname', '$birth', '$church', '$phone', '$mail', '', '')";
           $conn->query($sql);
           echo "<p class='pyes'> Registration comlete! </p>";
       } else {
           echo "<p class='pno'>" . $echo_er . "</p>";
       }
   }

For other variables (name, phone etc ...) I have code above it - no need to put it here. My goal is for PHP to send all the data in the first case and also check all the data. In the second example, I want to send and check everything except mailp and phonep. What I tried using the print variable $more, so its value is still 1 and does not change. Maybe it's because the function from JS is from here: <input type="date" name="birth" id="birth" onchange="adult()">

In both cases, it sends and checks all data (including mailp and phonep).

Does anyone know how to fix this, please?

MartanKing
  • 31
  • 6
  • 2
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) - Remember that JS is client side and PHP is server side when you read that. – M. Eriksson Oct 06 '21 at 23:42
  • PHP executes on the Server before anything is sent to the Client, then it's done. In your case you are executing a page that creates HTML, which happens to also include JavaScript *(not recommended - use external JavaScript instead)*. Once again you are creating an HTML page with PHP, then it's done. JavaScript has no access to the PHP variables on that page that created the page ever. You need to learn about JavaScript's `XMLHttpRequest` and/or `fetch` and the PHP `$_SESSION` superglobal. – StackSlave Oct 06 '21 at 23:53
  • you can use ajax or vue js to get variables realtime –  Oct 07 '21 at 03:41
  • @StackSlave please, how? – MartanKing Oct 07 '21 at 19:21
  • @ArvinJasonCabrera please, how? – MartanKing Oct 07 '21 at 19:21
  • @ArvinJasonCabrera - _"you can use ajax or vue js"_... There's not really an "or" there. Vue uses Ajax as well. It's just hidden behind an extra layer of abstraction so you don't use the native methods directly. If you just want to make some simple Ajax requests though, it's probably way easier to go native and just use `fetch()` directly. – M. Eriksson Oct 07 '21 at 20:15
  • @ArvinJasonCabrera how I can do with `fetch()`? Or do you now good website to learn it, please? – MartanKing Oct 07 '21 at 20:55
  • @MartanKing try this. https://www.sourcecodester.com/tutorials/php/12352/php-simple-crud-ajaxmysqli.html –  Oct 08 '21 at 00:48

0 Answers0