简体   繁体   中英

Jquery Check if Something Has Posted $_POST in PHP

I haven't been able to find an answer to this that works. All I want to do is check with Jquery to see if something has been posted in PHP.

PHP:

if ($_POST['clockin'] == "Clock In") {  
    //some code
};

And for the Jquery:

if (<?php $_POST['clockin'] == "Clock In" ?>) {
        $("#clockin").click(function(){
            $(this).attr('disabled', 'disabled');
            $("#clockout").removeAttr('disabled');
        });
    };

Try this,

<?php if ( isset($_POST['clockin'])) { 
    if ( $_POST['clockin'] == "Clock In") { ?>
        $("#clockin").click(function(){
            $(this).attr('disabled', 'disabled');
            $("#clockout").removeAttr('disabled');
        });
<?php } } ?>

Make sure you enclose script with <script></script> tags.

You shouldn't mix Server and Client Code(JS) unless you have no option but you can embed php in html thats advantage of scripting If you want to check it on Page load if previous page was form submission

Use Flag to check whether clock in is set php

<?php
  $flag=false;
   if ($_POST['clockin'] == "Clock In") {  
        $flag=true;
   }
?> 

html

<html>
   <body>
       <form onsubmit="return changeAttr(<?php echo $flag ?>);"> 
           <input type="submit" id="clockin" />
           <input id="clockout" />
      </form>
   </body>
</html>

JS

function changeAttr(flag) {
  //alert(flag);
  var clckin = document.getElementById('clockin');
  var clckout = document.getElementById('clockout');
  if (flag) {
    clckin.disabled = true;
    clckout.disabled = false;
    return false;
  }
  return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM