3

I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;

if (isset($_POST['edit']))
{
    echo "<script type=\"text/javascript\">";
    echo "var oldnotes = document.getElementById('oldnotes');";
    echo "oldnotes.disabled = false;";
    echo "var record = document.getElementById('record');";
    echo "record.disabled = false;";
    echo "</script>";
}

I have also tried;

if (isset($_POST['edit']))
{
    echo "<script type=\"text/javascript\">";
    echo "$('#oldnotes').removeAttr('disabled')";
    echo "$('#record').removeAttr('disabled')";
    echo "</script>";
}

But no luck :(

I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.

ashatte
  • 5,442
  • 8
  • 39
  • 50
user3266484
  • 125
  • 1
  • 2
  • 12
  • Please post the generated html code. – tjati Mar 24 '14 at 19:37
  • Is the "Edit" button located inside a form with method="post"? – larsAnders Mar 24 '14 at 19:38
  • Is there something triggering these echoes? Because I see that you're actively changing the elements to disabled – PlaceUserNameHere Mar 24 '14 at 19:40
  • JavaScript is irrelevant to the title - please change the title so is is *relevant*. PHP can only generate HTML (our whatever it sends to the output stream). This might contain JS, but that's not the question being asked.. i.e. *look at the generated output*. – user2864740 Mar 24 '14 at 19:40

8 Answers8

4

This is a better approach for these kind of problems :

if (isset($_POST['edit'])){
?>

<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>

<?php
}
Loïc
  • 11,804
  • 1
  • 31
  • 49
1

The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.

Here's what I suggest:

<?php

if (isset($_POST['edit'])) {
    ?>
    <script type="text/javascript">
        alert("Check entry"); // Use this for checking if your script is reaching here or not
        $('#oldnotes, #record').removeAttr('disabled');
    </script>
    <?php

}
?>

You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.

Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.

If not, something wrong with your edit button and the form POST

Tzar
  • 1,761
  • 2
  • 14
  • 21
1

Try use onclick on your Button

<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">

I hope it helps.

Thiago Melo
  • 1,157
  • 1
  • 14
  • 31
  • Thanks for your response, that works alright but the textbox becomes enabled and then the page immediately refreshes so the textbox is disabled again. Any ideas? If not, its probably just something wrong in the code somewhere..im sure ill find it. If not ill come crying for help tomorrow! :) – user3266484 Mar 24 '14 at 20:07
  • I'm sorry, I forget say: insert "return false;" as the last thing inside onclick="...". It will force the button doesn't submit the form. – Thiago Melo Mar 24 '14 at 20:22
1
 <?
 <script language='javascript'>
 (javascript code here)
 </script>
 ?>
0

Use single quotes for script type

echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
RobPio
  • 137
  • 8
0

use this jquery code:

<script>
$('#oldnotes,#record').attr('disabled','')
</script>
Siamand
  • 1,080
  • 10
  • 19
0

If you want to use JS with PHP you can read here: how to write javascript code inside php

However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.

Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.

<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >

Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.

Hope it helps!

Community
  • 1
  • 1
Daniel ZA
  • 306
  • 2
  • 10
0

you can always make a cookie use document.cookie() set the onclick=“” to changing the cookie and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})

Azure
  • 127
  • 11