Home>

There is a php file (calllog.php), which contains the elements #userfio, #phonenumber, #selectoperatorid

<
div class= "centeredblock" >
Adding users <
/div >
    <
div class= "wrapperusers" >
        <
div class= "leftmenuusers" >
            <
div >
Username <
/div >
            <
div >
<
input type= "text" placeholder= "full name" id= "userfio" >
<
/input >
<
/div >
            <
div >
Phone number <
/div >
            <
div >
<
input type= "text" placeholder= "Phone number" id= "phonenumber" >
<
/input >
<
/div >
            <
div >
Operator <
/div >
            <
div >
<
select name= "selectoperator" id= "selectoperatorid" >
<
? php
        $ hostname= "localhost";
        $ username= "root";
        $ password= "";
        $ dbname= "dbcall";
        $ selectoperatorsconn= mysqli_connect ($ hostname, $ username, $ password, $ dbname);
        if ($ selectoperatorsconn=== false) {
            die ("Error:". mysqli_connect_error ());
        }
        else {
            $ selectoperatorsresult= mysqli_query ($ selectoperatorsconn, "SELECT * FROM` operators` ");
            if ($ selectoperatorsresult) {
                    while ($ dbrowselectoperator= mysqli_fetch_assoc ($ selectoperatorsresult)) {
                        echo '<
option value= selectid '. $ dbrowselectoperator [' id '].' >
'. $ dbrowselectoperator [' name '].' <
/option >
';
                    }
        }
    }
? >
        <
/select >
<
/div >
            <
button type= "button" id= "addnewuser" >
Add <
/button >
        <
/div >
        <
div class= "tableblockusers" >
            <
table id= "alllistusers" >
                <
tr >
                    <
td >
                        Name
                    <
/td >
                    <
td >
                        Phone number
                    <
/td >
                    <
td >
                        Operator
                    <
/td >
        <
/tr >
<
? php
        $ hostname= "localhost";
        $ username= "root";
        $ password= "";
        $ dbname= "dbcall";
        $ userslistconn= mysqli_connect ($ hostname, $ username, $ password, $ dbname);
        if ($ userslistconn=== false) {
            die ("Error:". mysqli_connect_error ());
        }
        else {
            $ userslistresult= mysqli_query ($ userslistconn, "SELECT * FROM` users` ");
                if ($ userslistresult) {
                    $ tableuserscontent= '';
                    while ($ dbrowuser= mysqli_fetch_assoc ($ userslistresult)) {
                        $ sqlExtractNameOperator= 'SELECT name FROM operators WHERE operators.id='. $ dbrowuser ['operator'];
                        $ nameOperatorsListResult= mysqli_query ($ userslistconn, $ sqlExtractNameOperator);
                        if ($ nameOperatorsListResult) {
                            while ($ dbrowoperatorinstance= mysqli_fetch_assoc ($ nameOperatorsListResult)) {
                                //echo '<
option value= selectid '. $ dbrowselectoperator [' id '].' >
'. $ dbrowselectoperator [' name '].' <
/option >
';
                                $ operatornameforuser= $ dbrowoperatorinstance ['name'];
                            }
                        }
                        $ tableuserscontent.= '<
tr class= userlist '. $ dbrowuser [' id '].' >
<
td >
'. $ dbrowuser [' name '].' <
/td >
<
td >
'. $ dbrowuser [' phonenumber '].' <
/td >
<
td >
'. $ operatornameforuser.' <
/td >
<
/tr >
';
                    }
                    echo $ tableuserscontent;
            }
        mysqli_free_result ($ userslistresult);
        }
        mysqli_close ($ userslistconn);
? >
            <
/table >
        <
/div >
    </div > 

Data is transferred using ajax (from the addinfouser.js js file to the addinfouser.php file)

$ (document) .ready (function () {
        console.log ('Inside addinfouser.js');
$ ('# addnewuser'). Click (function () {
    let data= {
            userfio: $ ("# userfio"). val (),
          //userfio: $ ("# userfio: selected"). text () -if you need text, not a value
            phonenumber: $ ("# phonenumber"). val (),
            selectoperatorid: $ ("# selectoperatorid"). val ()
          };
    if (data.userfio== '' || data.phonenumber== '') {
        return;
    }
    console.log (data);
    $ .post ("addinfouser.php", data, function (data) {
      console.log (data.phpuserid);
      console.log (data.phpuserfio);
      console.log (data.phpphonenumber);
      console.log (data.phpselectoperatorname);
      let tr= '<
tr class= userlist '+ data.phpuserid +' >
'+' <
td >
'+ data.phpuserfio +' <
/td >
<
td >
'+ data.phpphonenumber +' <
/td >
<
td >
'+ data.phpselectoperatorname +' <
/td >
<
/tr >
';
      console.log (tr);
      $ (tr) .appendTo ("# alllistusers");
    });
});
    }
    ) 

This data is received in the addinfouser.php file

$ userfio= $ _POST ['userfio'];
        $ phonenumber= $ _POST ['phonenumber'];
        $ selectoperatorid= $ _POST ['selectoperatorid'];

then the data is validated

$ selectoperatorid= mb_substr ($ selectoperatorid, 8, mb_strlen ($ selectoperatorid) -8);
        //Establish values ​​that will be returned via ajax
        $ return= array ();
        $ return ['dataTable']= '';
        $ return ['error']= false;
        $ return ['countrecords']= 0;
        $ i= 0;
        require_once ('config.php');
        $ pdo= new PDO ($ dsn, $ user, $ password);
        //Begin form validation functionality
        if (! isset ($ userfio) || empty ($ userfio) ||! isset ($ phonenumber) || empty ($ phonenumber) ||! isset ($ selectoperatorid) || empty ($ selectoperatorid)) {
            $ return ['error']= true;
            $ return ['dataTable'].= '<
li >
Error: User is undefined. <
/li >
';
        }

Then there are sql requests to insert the received data into the users table

$ sqlCountUsersInsertOperator= 'SELECT COUNT (*) as numberusers from users';
        $ queryCountUsersOperator= $ pdo->
prepare ($ sqlCountUsersInsertOperator);
        $ queryCountUsersOperator->
execute ();
        $ extractcountusersrow= $ queryCountUsersOperator->
fetch (PDO :: FETCH_ASSOC);
            $ countalluserid= $ extractcountusersrow ['numberusers'];
            //echo $ countalloperatorid. '<
br >
';
        echo $ countalluserid. '<
br >
';
        echo $ userfio. '<
br >
';
        echo $ phonenumber. '<
br >
';
        echo $ selectoperatorid. '<
br >
';
        $ phpuserid= $ countalluserid + 1;
        $ sqlInsertOperator= "INSERT INTO users (id, name, phonenumber, operator) VALUES (: phpuserid,: userfio,: phonenumber,: selectoperatorid)";
        echo $ sqlInsertOperator;
        $ queryInsertOperator= $ pdo->
prepare ($ sqlInsertOperator);
        $ queryInsertOperator->
execute (array (': phpuserid'= >
 $ phpuserid, ': userfio'= >
 $ userfio, ': phonenumber'= >
 $ phonenumber, ': selectoperatorid'= >
 $ selectoperatorid));

then the user gets the name of the user's cellular operator

$ sqlExtractOperator= "SELECT name as operator_name FROM operators WHERE operators.id=". $ selectoperatorid;
        $ queryExtractOperator= $ pdo->
prepare ($ sqlExtractOperator);
        $ queryExtractOperator->
execute ();
        $ extractoperatorname= $ queryExtractOperator->
fetch (PDO :: FETCH_ASSOC);
        $ selectoperatorname= $ extractoperatorname ['operator_name'];

and as a result, the data is returned to the js script

return json_encode ([
                            'phpuserid'= >
 $ phpuserid,
                            'phpuserfio'= >
 $ userfio,
                            'phpphonenumber'= >
 $ phonenumber,
                            'phpselectoperatorname'= >
 $ selectoperatorname
                           ]
        );

But as a result, by clicking on the Add button, a new row is not added despite the command (it is added to the database, but not to the #alllistusers table)

$ .post ("addinfouser.php", data, function (data) {
  console.log (data.phpuserid);
  console.log (data.phpuserfio);
  console.log (data.phpphonenumber);
  console.log (data.phpselectoperatorname);
  let tr= '<
tr class= userlist '+ data.phpuserid +' >
'+' <
td >
'+ data.phpuserfio +' <
/td >
<
td >
'+ data.phpphonenumber +' <
/td >
<
td >
'+ data.phpselectoperatorname +' <
/td >
<
/tr >
';
  console.log (tr);
  $ (tr) .appendTo ("# alllistusers");
},
 "JSON");

a is added after page reload

<
? php
        $ hostname= "localhost";
        $ username= "root";
        $ password= "";
        $ dbname= "dbcall";
        $ userslistconn= mysqli_connect ($ hostname, $ username, $ password, $ dbname);
        if ($ userslistconn=== false) {
            die ("Error:". mysqli_connect_error ());
        }
        else {
            $ userslistresult= mysqli_query ($ userslistconn, "SELECT * FROM` users` ");
                if ($ userslistresult) {
                    $ tableuserscontent= '';
                    while ($ dbrowuser= mysqli_fetch_assoc ($ userslistresult)) {
                        $ sqlExtractNameOperator= 'SELECT name FROM operators WHERE operators.id='. $ dbrowuser ['operator'];
                        $ nameOperatorsListResult= mysqli_query ($ userslistconn, $ sqlExtractNameOperator);
                        if ($ nameOperatorsListResult) {
                            while ($ dbrowoperatorinstance= mysqli_fetch_assoc ($ nameOperatorsListResult)) {
                                //echo '<
option value= selectid '. $ dbrowselectoperator [' id '].' >
'. $ dbrowselectoperator [' name '].' <
/option >
';
                                $ operatornameforuser= $ dbrowoperatorinstance ['name'];
                            }
                        }
                        $ tableuserscontent.= '<
tr class= userlist '. $ dbrowuser [' id '].' >
<
td >
'. $ dbrowuser [' name '].' <
/td >
<
td >
'. $ dbrowuser [' phonenumber '].' <
/td >
<
td >
'. $ operatornameforuser.' <
/td >
<
/tr >
';
                    }
                    echo $ tableuserscontent;
            }
        mysqli_free_result ($ userslistresult);
        }
        mysqli_close ($ userslistconn);
? >
      

Tell me how to add #alllistusers to the table. Learning resources don't have this.

too big a question, break it down into smaller ones. This is porridge, not a question.

Jean-Claude2021-10-09 22:48:10

@ Jean-Claude help please.

beginner2021-10-09 22:48:10

what can you help here, you know how many plugs there can be, from the database, and from php, and from js. the literature above was suggested.

Jean-Claude2021-10-09 22:48:10

@ Jean-Claude I updated the question.

beginner2021-10-09 06:05:05

The best answer here is to use the debugging tools and debug the code yourself, go through it step by step and figure out what's going on.

Daniel Protopopov2021-10-09 08:12:28