0

I have a database with a table of CV´s where the client writes up all his info like (name, surname, professional experience, etc...) and on the html page I have 3 divs.

The 1st Div will show the whole CV form I'm planning on using echo $row=name separated using <br> below each line to separate it and display it as a real CV or Perhaps a Job Application form.

The 2nd Div will only contain a list of the names of the registered CV´s and because I know little about jQuery or js I used buttons below each name, so that when clicked it compares the $userform variable to the row which has an equal name on the table and displays whole row on the 1st div separating each row by <br> or <p>.

First it will refresh the list on the 2nd div:

<div class="content2">
  <h3>LISTA DE CANDIDATOS:</h3>
  <form class="btn" method="post">
    <input type="submit" name="btn_search" value="Actualizar">
  </form>
  <br>
<?php if (isset($_POST["btn_search"])) {
  $con = mysqli_connect("localhost","root","","chevron");
  $q = "SELECT * FROM candidatos ORDER BY id DESC";
  $result = mysqli_query($con,$q);
  while ($row = mysqli_fetch_assoc($result)){
      $userform = $row["nome"] ;
      $_SESSION["userform"] = $userform;
      echo " ".$row['sobrenome'].",  ".$_SESSION['userform']." <br><br>"; ?>
      <form class="btn" action="" method="post">
          <input type="submit" name="post_form" value="Vizualizar CV">
      </form>
      <br>
<?php }
}
?>
</div>

After the mysqli_fetch_assoc I declare a variable $userform is equal to the row name(which means name in my language) and the global variable $_SESSION['userform'] is equal to that $userform variable which contains the row name.

Now the 1st div:

<div class="content">
  <br>

  <div class="form_cand">
    <br>
    <?php if (isset($_POST["post_form"])) {


      $con = mysqli_connect("localhost","root","","chevron");
      $q = "SELECT * FROM candidatos WHERE nome = ".$_SESSION["userform"]." ";

      $result = mysqli_query($con, $q);
      if (!$result) {
        echo "ERROR";
      }
      $row = mysqli_fetch_array($result);
      echo "NOME: ".$row["nome"]."<br>";
      echo "SOBRENOME: ".$row["sobrenome"]."<br>";
      echo "MORADA: ".$row["morada"]."<br>";
      echo "BI: ".$row["bi"]."<br>";
      echo "EMAIL: ".$row["email"]."<br>";
      echo "COMPETENCIAS: ".$row["competencias"]."<br>";
      echo "IDIOMAS: ".$row["idiomas"]."<br>";
      echo "OBJECTIVO: ".$row["objectivo"]."<br>";
    }
  ?>
  </div>
</div>

Based on the $userform variable which is in the Session Variable it should find the name which is equal to the one in the session variable and show the whole row but I don't know why it is not showing?

UPDATE:

I got the 1st part working thank you for the help! now I have added 2 buttons and a checkbox on the while condition for when the data is posted the buttons and the checkbox appear as well, one is button is DENY and the other ACCEPT.

when I click on Deny I want it to delete the current posted info from that table and send it over to another table which for eg. could be "DeniedCV´s" and the accept button to do practically the same thing but sending it over to the "ACCEPTEDCV´s" table.

but when the ACCEPT button is clicked and the checkbox checked, I want it to the same thing, but this time sending the data to 2 other tables ass well.

here is what I've done so far:

<div class="content">
  <br>

  <div class="form_cand">

    <br>
    <?php if (isset($_POST["post_form"])) {
      $selectedUser = intval($_POST['users']); //pra selecionar o nome clicado na option list

        if($selectedUser > 0){
          $con = mysqli_connect("localhost","root","","chevron");
            $q = "SELECT * FROM candidatos WHERE id = ".$selectedUser." ";
                $result = mysqli_query($con, $q);
                      if (!$result) {
                      echo "ERROR";
                          }

                    $row = mysqli_fetch_array($result);

                      if(is_array($row) && count($row) > 0){
                         echo "<center><strong>CANDIDATO Nº.:</strong>".$row["id"]." <br><br><br></center>";
                          echo "<strong>NOME:</strong><br>".$row["nome"]."<br><br><br>";
                        echo "<strong>SOBRENOME:</strong><br> ".$row["sobrenome"]."<br><br><br>";
                                          echo "<strong>MORADA:</strong><br> ".$row["morada"]."<br><br><br>";
                                          echo "<strong>BI:</strong><br> ".$row["bi"]."<br><br><br>";
                                          echo "<strong>EMAIL:</strong><br> ".$row["email"]."<br><br><br>";
                                          echo "<strong>COMPETENCIAS:</strong><br> ".$row["competencias"]."<br><br><br>";
                                          echo "<strong>IDIOMAS: </strong><br>".$row["idiomas"]."<br><br><br>";
                                          echo "<strong>OBJECTIVO:</strong><br> ".$row["objectivo"]."<br>"; ?>

                                          <center>
                                          <form class="decisions" action="" method="post">

                                          <p>
                                            Se achar esta candidatura excepcional e quiser mostra-la ao ADMINISTRADOR marque a caixinha pequena que esta apos o botão qualificado!
                                          </p>
                                          <input type="submit" name="btn_deny" value="NÃO QUALIFICADO">

                                          <!-- botao negar -->
                                          <?php if (isset($_POST['btn_negar'])) {

                                                  $selectedUser = intval($_POST['users']);

                                                  if($selectedUser > 0){
                                                      $con = mysqli_connect("localhost","root","","chevron");
                                                      $q = "SELECT * FROM candidatos WHERE id = ".$selectedUser." ";

                                                      $result = mysqli_query($con, $q);

                                                      if (!$result) {
                                                          echo "ERROR";
                                                      }

                                                      $row = mysqli_fetch_array($result);

                                                      if(is_array($row) && count($row) > 0){
                                                             //I WANTED TO PUT ANOTHER $q =  wich inserts Into another table
                                                             //and then add another $q = wich deletes it from the current table
                                                          



                                          } ?>
                                           <!-- botao negar -->
                                          <input type="submit" name="btn_aceitar" value="QUALIFICADO">
                                          <!-- botao aceitar -->
                                          <?php if (isset($_POST['btn_accept'])) {

                                          } ?>
                                           <!-- botao aceitar -->
                                          <input type="checkbox" name="checkbox_recomend" value="Recomendar ao Administrador">
                                          </form>
                                          </center>
                                            <?php  }
                                               }

                                             }
                                             ?>

  </div>


</div>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

1

In your while cycle you are creating multiple forms with the same button name and no inputs whatsoever, just print out information.

The correct way to do it is to use some kind of a list(select drop-down for example) and based on the chosen user, show the correct information: The html would look like:

<form class="btn" action="" method="post">
    <select name="users">
        <?php while ($row = mysqli_fetch_assoc($result)){ ?>

            <option value="<?php echo $row['id'] ?>">
                <?php echo ' '.htmlspecialchars($row['sobrenome']).', '.htmlspecialchars($row['nome']) ?>
            </option>

        <?php } ?>
    </select>    

    <input type="submit" name="post_form" value="Vizualizar CV" />

And then after submission:

<?php 

if (isset($_POST["post_form"])) {

    $selectedUser = intval($_POST['users']); // This is how we extract the chosen item from the drop-down


    if($selectedUser > 0){
        $con = mysqli_connect("localhost","root","","chevron");
        $q = "SELECT * FROM candidatos WHERE id = ".$selectedUser." ";

        $result = mysqli_query($con, $q);

        if (!$result) {
            echo "ERROR";
        }

        $row = mysqli_fetch_array($result);

        if(is_array($row) && count($row) > 0){
            echo "NOME: ".$row["nome"]."<br>";
            echo "SOBRENOME: ".$row["sobrenome"]."<br>";
            echo "MORADA: ".$row["morada"]."<br>";
            echo "BI: ".$row["bi"]."<br>";
            echo "EMAIL: ".$row["email"]."<br>";
            echo "COMPETENCIAS: ".$row["competencias"]."<br>";
            echo "IDIOMAS: ".$row["idiomas"]."<br>";
            echo "OBJECTIVO: ".$row["objectivo"]."<br>";
        }
     }
}

?>

You don't need to store the information in a $_SESSION superglobal. When transmitting data through forms, the best practice is to use IDs rather than strings. Instead of using the name in the option value, we used the id.

If you want to achieve the same thing live, without refresh, then you ought to use AJAX

Edit: Since you requested a sample AJAX code, please first look at this answer.

You will have to make some changes according to your needs. First add the following javascript code:

 <script>
        $('input[name="post_form"]').on('click',function(){
           var chosenID = $('select[name="users"]').val();

           if(chosenID > 0){
                 $.ajax({
                    url: 'yourfile.php?ID = ' + chosenID,
                    type: 'GET',
                    dataType: 'html',
                    success: function (data) {
                        $('#userData').html(data);
                    }
                });
           } 
        });
  </script>

You should also add the div element:

<div id="userData"></div>

All you have to do now is to get the ID in the yourfile.php file, which is under the $_GET superglobal and repeat the PHP code I've previously posted.

Community
  • 1
  • 1
Suat Hyusein
  • 495
  • 3
  • 14
  • Thank You very much it worked!!! but when using AJAX for it to be in real-time is it to complicated? i mean the code? becuz i learn fast u can show me the basics and ill figure out how its done, plz show me te steps – Renas DIVilhena Alberto Manuel Nov 14 '16 at 16:52
  • I've edited the answer and added an AJAX sample as well. – Suat Hyusein Nov 14 '16 at 17:21
  • Thank you, but i dont understand the yourfile.php part, what php file should i add there and what is it for? – Renas DIVilhena Alberto Manuel Nov 14 '16 at 17:55
  • i´ve added 2 buttons and a checkbox, for when the cv is posted, the buttons appear with it as well, but no im trying to alter your code and adapt it in a way that when i click on DENY it deletes that user data from the current table and sends it to another table(for eg. called deletedCV´s), and the accept button sends it to the AcceptedCV´s table but all on the same script, i edited the question for you to see what i have done. – Renas DIVilhena Alberto Manuel Nov 14 '16 at 17:59