0

A) I would like to have 2 different functions for 1 button. For first click function 1 should start, for second click function 2, for third click function 1, fourth click function 2 ...

For now I need 2 buttons, one disables and the other one enables the possibility to input data in a form field. The best would be to have both functions for 1 button (as explained above).

Does anyone has an idea how to do that?

B) All data get saved and can be reopened in the datamanagementsystem. I would like that disabled fields stay disabled (after reopening the form again) for input. Is there a possibility to do so?

<script>
var nav = false;
function disable18() {

    document.getElementById("field1").style.color = "red";
    document.getElementById("field1").value = "X";;
    document.getElementById("field1").disabled = true;
nav = true;
}
function enable18() {
document.getElementById("field1").disabled = false;
document.getElementById("field1").value = "";
document.getElementById("field1").style.color = "black";
nav = false;
}
function toggleNav() {
  if(nav==false){
    disable18();
  } else {
    enable18();
  }
}
</script>

How I get the data from database:

<?php
session_start();
require_once 'sc/functions.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
 {
$user_home->redirect('index.php');
  }

 $stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE    userID=:uid");
 $stmt->execute(array(":uid"=>$_SESSION['userSession']));
 $row = $stmt->fetch(PDO::FETCH_ASSOC);

 ?>
<?php

// php search data in mysql database using PDO
// set data in input text


  $user = "xxx";
  $pass = "xxxx";

  if(isset($_POST['Find']))
    {
        // connect to mysql
     try {
       $pdoConnect = new    PDO('mysql:host=localhost;dbname=xxx;charset=utf8', $user, $pass);    //mysql:host=localhost;dbname=test_db","root","")
      $pdoConnect->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $exc) {
       echo $exc->getMessage();
      exit();
  }



// id to search
$ID = $_POST['ID'];

 // mysql search query
$pdoQuery = "SELECT * FROM dabase WHERE ID = :ID";

$pdoResult = $pdoConnect->prepare($pdoQuery);

//set your ID to the query ID
$pdoExec = $pdoResult->execute(array(":ID"=>$ID));


if($pdoExec)
{
        // if ID exist
        // show data in inputs
    if($pdoResult->rowCount()>0)
    {
        foreach($pdoResult as $row)
        {
            $ID = $row['ID'];
            $field1 = $row['field1'];
   }
    }
        // if the id not exist
        // show a message and clear inputs
    else{
         header( "Location: nodatasearch.php" ); die;

        }
      }else{
    echo 'ERROR Data Not Inserted';
    }
  } ?>

Submitting/Saving data:

<?php

 error_reporting(E_ALL);
 ini_set('display_errors', 1);


    require("php/tc.php");


            $ID = $_POST['ID'];
            $field1 = $_POST['field1'];

            $sql = "UPDATE pdbase SET
            field1 = :field1
            WHERE ID = :ID";


            $stmt = $dbh->prepare($sql);

            $stmt->bindValue(':ID', $ID);
            $stmt->bindValue(':field1', $field1);

    $stmt->execute();

     // var_dump($_POST['user']);



  ?>
Jessy642
  • 67
  • 8
  • Is there some kind of business logic behind the disable toggle? Do you want to store the state on the server, or on the user machine in localStorage? – Halcyon Sep 05 '22 at 20:59
  • Does this answer your question? [JavaScript Toggle between 2 Functions](https://stackoverflow.com/questions/37164993/javascript-toggle-between-2-functions) – Heretic Monkey Sep 05 '22 at 21:38
  • ... also, one question per question, please. If you want to save the state of fields between reloads, there are a plethora of questions about that too, including [Saving form state after a page refresh](https://stackoverflow.com/q/3347269/215552) – Heretic Monkey Sep 05 '22 at 21:41

1 Answers1

1

My asnwer will solve problem A however problem B is a little more complicated. You will need a flag for this, for example this is your html

<input type="text" id="field1">
<button id="toggler" class="btn btn-success" onclick="toggleNav()">Toggler</button>

in your js start off with creating your flag, let's set it to false

var nav = false;

When your 1st function is called change your flag to true

function disable18() {
  document.getElementById("field1").disabled = true;
  nav = true;
}

Now for the second function we will set it back to false

function enable18() {
  document.getElementById("field1").disabled = false;
  nav = false;
}

Now we create the function that toogles between the 2 of them

function toggleNav() {
  if(nav==false){
    disable18();
  } else {
    enable18();
  }
}

After that, all that's left is make sure your toggleNav() function is in the onclick() in your button. Now for problem B I have more questions than answers. Need more details about how do you want to do achieve that

Chris G
  • 1,598
  • 1
  • 6
  • 18
  • Thank you very much! That worked perfect. I honestly appreciate your help. About question B) I am using 1 buttons to enable / disable the possibility to enter data into input fields. (what we already solved now). That works great. All data are stored finally in a database after submitting. Users have the possibility to load all those data again by entering the ID of the submitted form. Now I would like to know if there is a possibility to leave disabled data fields disabled also at the next new loading of the dataset? – Jessy642 Sep 06 '22 at 07:51
  • For now I made a workaround: When user clicks the button and disable the possibility to enter data into a specific input fields - submit - close the page - reload the page -> I have a checkbox which shows which button was pressed before. Then he needs to press the button again. The best option would be if the user doesn't have to click the button again for disabling again those specific data inout fields. I hope its more clear now. – Jessy642 Sep 06 '22 at 08:00
  • Maybe i got an idea: So if the user loads the data from database again: If checkbox A is checked then disable/block input field A... Or maybe there is an easier way without the checkbox (but that would be a way to save indirectly that sth. was disabled).. just sharing my thought – Jessy642 Sep 06 '22 at 10:08
  • 1
    maybe you can use another flag for this case, once you save in your database but this flag will be in your table. If inputs were disabled before you saved then save 1 else save 0 or something like that. Once you load all the data you will read value in your flag and disabled inputs if the stored value was 1 else enable them. – Chris G Sep 06 '22 at 11:52
  • Thank you for your advice! Would you be so nice and help me with the code, just about the JS - I never did that before. – Jessy642 Sep 06 '22 at 22:04
  • @Jessy642 can you update your question then so I can see the rest of the code? like how you save your data and when do you read your saved data again? – Chris G Sep 07 '22 at 12:15
  • Sure, I updated my post (for getting/saving data). – Jessy642 Sep 07 '22 at 18:30
  • dang never used `PDO` before but I will help how I can. First thing first, you already created your flag in your table and saved 1 if `field1` was disabled right? – Chris G Sep 07 '22 at 19:45
  • True PDO was also new to me, just worked with it because it seemed to give a higher security level. If field 1 is disabled it saves X and also leads to that the checkbox get checked. If field 1 is enabled users can enter 0 or 1. – Jessy642 Sep 08 '22 at 20:15
  • @Jessy642 your flag needs to be automatic, users don't need to enter any value, just read if your input is disabled then tell your code to save 1 in your flag, if not then save 0. That's it for the `INSERT` or `UPDATE` of your data. Now when you reload page, you have to read what value was store in your flag – Chris G Sep 08 '22 at 22:06
  • Thank you for your answer! When user open the data again they are normally able to insert new data. I know how to save 1 or 0 -> Button disable is clicked -> input fields are disabled and checkbox get automatically clicked (1). Now user reloads data -> when 1 is saved automatically a function get activated which blocks input fields right? For that last part I don't know how to do. I mean I know about PDO and things. Just I am asking for the part of JS or JQuery which automatically disables if 1 is saved. Do you know how it works? – Jessy642 Sep 12 '22 at 08:09