3

I have used Javascript to enable or disable a button, depending on the input value. I'm new to PHP, so I don't know why this isn't working. Here's the PHP code:

<?php 
session_start();
include_once('header.php');
include_once('functions.php');

$_SESSION['userid'] = 1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Microblogging Application</title>
</head>
vbody>

<?php
if (isset($_SESSION['message'])){
    echo "<b>". $_SESSION['message']."</b>";
    unset($_SESSION['message']);
}
?>
<form method='post' action='add.php'>
<p>Your status:</p>
<textarea name='body' rows='5' cols='40' wrap=VIRTUAL></textarea>

<input type="text" name="age" />
<input type="text" name="poscode" />
<input type="submit" name="submit" value="Post" disabled="disabled"/>




</form>

<script type="text/javascript">
$('input[name="age"], input[name="poscode"]').change(function(){
  if ($(this).val())
  {
    $("input[name='submit']").removeAttr('disabled');
  }
});

</script>
<?php
$posts = show_posts($_SESSION['userid']);

if (count($posts)){
?>
<table border='1' cellspacing='0' cellpadding='5' width='500'>
<?php
foreach ($posts as $key => $list){
    echo "<tr valign='top'>\n";

    echo "<td>".$list['body'] ."<br/>\n";
    echo "<small>".$list['stamp'] ."</small></td>\n";
    echo "</tr>\n";
}
?>
</table>
<?php
}else{
?>
<p><b>You haven't posted anything yet!</b></p>
<?php
}
?>
</body>
</html>

How do I change it so when the value of the input = "foo", the button is now clickable.
Thanks in advance

-Ben

2
  • here: <script type="text/javascript"> $('input[name="age"], input[name="poscode"]').change(function(){ if ($(this).val()) { $("input[name='submit']").removeAttr('disabled'); } }); </script> Commented Jan 7, 2012 at 0:07
  • To let you know, PHP has not a slightest relation to JavaScript. So, JS cannot be "working" or "not working" in PHP Commented Jan 7, 2012 at 0:15

2 Answers 2

3

I can't make sense of your question, but if you're asking what I think you're asking (which is "how do I enable the button if the form is populated on page load?"), change your script to the following:

$.ready(function(){
    $('input[name="age"], input[name="poscode"]').change(function(){
      if ($(this).val())
      {
        $("input[name='submit']").removeAttr('disabled');
      }
    }).change();
});

I added the change call to trigger the change event handler, and wrapped it in jQuery's ready handler.

Sign up to request clarification or add additional context in comments.

5 Comments

Please define "not working", and define what it is you really want to accomplish.
Sorry, I just want so when someone enters age in the input, the button is not disabled. I don't know if its in my php right or what...
Do you want to do that server-side or client side?
Then it probably has to do with the fact that jQuery may not be initialized by the time you define your event handler. Answer updated.
1

Try using the attr function like below

$('#element').attr('disabled', true);

Or

$('#element').attr('disabled', false);

Because different browsers treat the disbaled attribute differently, using true and false lets jquery handle the cross browser issues

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.