简体   繁体   中英

How do I embed Javascript in PHP code?

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.

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
}

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

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.

 <?
 <script language='javascript'>
 (javascript code here)
 </script>
 ?>

Use single quotes for script type

echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";

use this jquery code:

<script>
$('#oldnotes,#record').attr('disabled','')
</script>

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!

您始终可以使用document.cookie()设置onclick=“”来更改 cookie 并制作一个脚本,该脚本在您打开网站时运行并获取 cookie(使用window.onload = function(){/*code goes here*/} )

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