Asked  1 Year ago    Answers:  5   Viewed   10 times

Is it possible to insert PHP code into a webpage using javascript after the page has loaded? When a user clicks on a category, I want to show a set of images (which I insert using js), and then search for any associated comments using PHP.

Here is my code :

var temp = '<div class"comment_section">';
var html = temp + '<?php if ( post_password_required() ) return; ?><div id="comments" class="comments-area"><div class="container"><?php ?><?php if ( have_comments() ) : ?><!-- functions for displaying comments here --></div></div>';
html += '</div>';

That didn't work. I also tried swapping the brackets for &lt; and &gt; but that just inserts the above between quotes which is then diplayed as a block of text on the page.

Basically is what I'm trying to do even possible?

 Answers

5

Is it possible to insert PHP code into a webpage using javascript after the page has loaded? The simple answer is no. The page has already been rendered, the only way to change it is using javascript running within the user's browser.

However, if you want to search for any associated comments using PHP, just send a request to the server using AJAX and set up your server to return a well-formed response to that query.

To learn more about ajax, you may want to read What is AJAX, really?.

Saturday, May 29, 2021
 
tedders
 
1

You can use the File.name web API for this.

To accomplish this, you'll want to add an onchange event to your input field, and then create a javascript function to capture the event. In that function, you'll access the selected file's name and perform the validation. For example:

<input
    type="file" name="userfile" id="userfile"
    accept="image/jpeg, application/pdf"
    onchange="validateFile(this)"
/>

<script>
    function validateFile(fileInput) {
        var files = fileInput.files;
        if (files.length === 0) {
            return;
        }

        var fileName = files[0].name;
        if (fileName.length > 15) {
            alert('File input name to long.');
        }
    }
</script>

It's worth noting, for security reasons, you should not store the file with the name the user supplies. It's safe to store it in a database and used as a label when showing users (pending you sanitize it first) - but the file path you save on your server should be 100% controlled by you.

Thursday, April 1, 2021
 
relipse
 
1

The good news is that PHP and JavaScript have a similar idea about what values are true and false.

  • An empty string will be false on both sides. A string with something in it (except 0 in PHP) will be true on both sides.
  • The number 0 will be false on both sides. All other numbers will be true on both sides.

Since the values of a form will always be strings, as Quentin pointed out in his answer, a good practice might be to use an empty string as false value and something else (e.g. 'true') as true value. But I think your way of using 0 and 1 and testing the numerical values is the safest approach because it isn't misleading. (When someone sees 'true' they might think 'false' would also be usable for a false value.

Saturday, May 29, 2021
 
edsk
 
3

You want:

insert into prices (group, id, price)
select 
    7, articleId, 1.50
from article where name like 'ABC%';

where you just hardcode the constant fields.

Thursday, June 3, 2021
 
Yrtymd
 
3

Okay, so you've got two options here :

  1. Use jQuery UI's accordion - its nice, easy and fast. See more info here
  2. Or, if you still wanna do this by yourself, you could remove the fieldset (its not semantically right to use it for this anyway) and create a structure by yourself.

Here's how you do that. Create a HTML structure like this :

<div class="container">
    <div class="header"><span>Expand</span>

    </div>
    <div class="content">
        <ul>
            <li>This is just some random content.</li>
            <li>This is just some random content.</li>
            <li>This is just some random content.</li>
            <li>This is just some random content.</li>
        </ul>
    </div>
</div>

With this CSS: (This is to hide the .content stuff when the page loads.

.container .content {
    display: none;
    padding : 5px;
}

Then, using jQuery, write a click event for the header.

$(".header").click(function () {

    $header = $(this);
    //getting the next element
    $content = $header.next();
    //open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
    $content.slideToggle(500, function () {
        //execute this after slideToggle is done
        //change text of header based on visibility of content div
        $header.text(function () {
            //change text based on condition
            return $content.is(":visible") ? "Collapse" : "Expand";
        });
    });

});

Here's a demo : http://jsfiddle.net/hungerpain/eK8X5/7/

Friday, June 11, 2021
 
Eugenie
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :