Integrate 2 functions for 1 button and keep input fields disabled after reload

Viewed 55

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']);



  ?>
1 Answers

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

Related