Jump to content

Disabling field input with PHP?


vozzek

Recommended Posts

Hi everyone,

 

Maybe I'm going about this the wrong way (or the more complicated way...) but I'm trying to use PHP to conditionally disable a field within a form.  It's not working out for me, and I don't know why.  Here's the scenario:

 

I have a field called "customizable" which is a Y or N radio box (defaulting to N).

 

The next field is "character_limit".  If "customizable" is set to N, I want this field grayed out (disabled?)  But if someone checks the "Y" box for "customizable", I want this field to be activated.  Below is the code I tried, showing both fields.  (Note that both fields are contained within the same form):

 

 

<tr valign="baseline">

      <td align="right" valign="top" nowrap="nowrap">Customizable:</td>

      <td valign="baseline"><table>

        <tr>

          <td><input type="radio" name="customizable" value="Y" <?php if (!(strcmp("Y","Y"))) {echo "CHECKED";} ?> />

            Yes</td>

        </tr>

        <tr>

          <td><input type="radio" name="customizable" value="N" checked="N" <?php if (!(strcmp("Y","N"))) {echo "CHECKED";} ?> />

            No</td>

        </tr>

      </table></td>

    </tr>

    <tr valign="baseline">

      <td nowrap="nowrap" align="right">Character Limit:</td>

      <td><input name="character_limit" type="text" value="" size="32" maxlength="3" <?php if ($_GET['customizable']="N") {

  ?>

      <?php echo "disabled=\"disabled\""; } ?>

      /></td>

    </tr>

 

What am I doing wrong, and is there an easier way to do this?

Thanks in advance.

 

 

Link to comment
Share on other sites

This seems to more of a Javascript issue. You can set a default value of disabled or not from php, but to change the state depending of radio state in realtime, this must be done from javascript..

 

a simple example:

<input onclick="document.character_limit.disabled = false" type="radio" name="customizable" value="Y" />
<input onclick="document.character_limit.disabled = true" type="radio" name="customizable" value="N" />

<input name="character_limit" type="text" value="" size="32" maxlength="3"/>

Link to comment
Share on other sites

Great advice haaglin, thanks.  I've just only started learning javascript, and it looks like I should probably finish the tutorial(s) I'm taking before moving too far forward.

 

One thing:  Do I need a semicolon after the quotes in the input function?  Because it's still not working.  I added

<script type="text/javascript">

<!--

//-->

</script> 

to the <head> portion of the page.

 

Link to comment
Share on other sites

Do you have any codes inside the script tag? if not just remove it..

 

semicolon is only needed is you have more then one code to execute on the same line..

onclick="document.character_limit.disabled = true; alert('clicked')" //<-- works (Notice the semicolon before alert)
onclick="document.character_limit.disabled = true alert('clicked')" //<-- Dont work.. 

Link to comment
Share on other sites

I sheepishly admit I don't know what to put within the <script> tag.  I'm guessing I need to define an onclick function or nothing happens?  I'm extremely new to java (having just started messing with php), so forgive me if I sound like a total noob.

 

 

Link to comment
Share on other sites

The script tag is the "same" as the php tags (<?php ?>). You place javascript code inside the tags. no need to define a onclick function, this is done in the onclick="" part in the radio buttons. If you want the onclick to execute a function, this function should be placed inside the script tag.

 

If you need more help, i would suggest the javascript forum..

Link to comment
Share on other sites

I sheepishly admit I don't know what to put within the <script> tag.  I'm guessing I need to define an onclick function or nothing happens?  I'm extremely new to java (having just started messing with php), so forgive me if I sound like a total noob.

 

You don't need to define an onclick function.  Write a function with whatever name you feel is appropriate, then call that function from the OnClick attribute of the particular input field.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.