1

i'm having this problem with my codes. what im supposed to achieve is to enable a disabled button with a certain condition. my code is working when the condition is invalid, but if its correct it will not work right away. i have to re click it to be enabled. $sentEmailClients (will be a mysql data if i got it working).

Here's my code

<?php
$sentEmailClients = 'test@test.com';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $email = $_POST["validEmail"];

  if ($email === $sentEmailClients) {
    echo '
      <script>
      function enableButton2() {
        document.getElementById("addToClient").disabled = false;
      } 
      < /script>
    ';
  } else {
    echo 'Please enter a valid email';
  }
}

and here's the form BTW i've used Bootstrap for this

<form class="form-horizontal" method='post'  data-toggle="validator">
  <fieldset>
    <div >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Validate My Email!</h4>
          </div>
          <div class="modal-body">
            <label for="validEmail">Email:</label>
            <input type="text" class="form-control input-sm " id="validEmail" name = "validEmail"  required> 
          </div>
          <div class="modal-footer">
            <button class="btn btn-warning btn-lg " id="submit" type="submit text-center" onclick="enableButton2()" >Validate!</button>
            <button class="btn btn-success btn-lg " id="addToClient" type="submit text-center" onclick="addClientInformation();" disabled >Submit Information</button>
          </div>
        </div>
      </div>
    </div>
  </fieldset>
</form>
PaulH
  • 2,918
  • 2
  • 15
  • 31
jmv
  • 415
  • 2
  • 9
  • 24
  • You are just adding function based on condition rather then call the function. Add function by default, then you just need to call function based on condition – Bhavin Solanki Jul 05 '16 at 08:38
  • Please understand the difference between PHP and JavaScript. One is server-side, the other is client-side. – bzeaman Jul 05 '16 at 08:38
  • 1
    Read this very nice tutorial: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – PaulH Jul 05 '16 at 08:47

1 Answers1

0

The function is rendered only when the condition is true, on 1st click you will make its disabled = false and it will get enabled.Instead of triggering the function 'onclick' make it document.ready.

$(document).ready(function(){
    $(#id).show()
}
dimlucas
  • 5,040
  • 7
  • 37
  • 54
line-segment
  • 369
  • 5
  • 14