1

I have a php file which uses a script within it.

I'm trying to get an image called by php (the_main_image) and prepend a set of links with this image (along with a favicon which is currently working fine) using jQuery. Here's what I have so far but it currently breaks the function

<script>
    jQuery(document).ready(function($) {

        $("#lesen").click(function() {

            var bla = $('#my_hidden_input').val();
            var test = "<?php the_main_image(); ?>";

        $.ajax({
            url: bla,
            dataType: "text",
            success: function(data) {
                $(".text").html(data);
                $(".text a[href^='http']").each(function() {

                    $(this).prepend(test);
                    $(this).prepend('<img src="https://www.google.com/s2/favicons?domain=' + this.href + '">');



                });
            }
        });
    });

});
</script>

How can I use jQuery var values that contain php within a prepend or other similar jQuery functions?

5
  • what is the error you get in the console? Also check what is printed in the page with Inspect tool. Commented Jan 5, 2018 at 0:55
  • Seems to be a formatting issue when checking the console. So just to check the above code should all work okay and what I am trying to achieve is doable? Commented Jan 5, 2018 at 0:57
  • The out of my php main_image contains html formatting like so <div class="browser-shot "><img src="/image.png/></div> ); is this what is causing the issue because I cant prepend complex values like this? Commented Jan 5, 2018 at 0:59
  • jQuery values cannot contain php code. PHP is executing in the server, then the content gets to the client and get rendered by the browser, so jQuery does not see the <?php the_main_image(); ?> function but instead it sees whatever the output of that function is. Commented Jan 5, 2018 at 1:01
  • the problem here could be that the output of the function the_main_image(); contains double quotes, and that your var test variable is also using double quotes , try using single quotes var test = '<?php the_main_image(); ?>'; Commented Jan 5, 2018 at 1:03

3 Answers 3

2

Hi I have done similar thing in my project, below worked for me.

Since you cannot write php directly in JavaScript, you need take help of your html.

Create a hidden input field in you html like below

<input type="hidden" id="something" value="<?php the_main_image(); ?>"/>

Then read this in jquery like

<script>
    jQuery(document).ready(function($) {

        $("#lesen").click(function() {

            var bla = $('#my_hidden_input').val();
            var test = $("#something").val();

        $.ajax({
            url: bla,
            dataType: "text",
            success: function(data) {
                $(".text").html(data);
                $(".text a[href^='http']").each(function() {

                    $(this).prepend(test);
                    $(this).prepend('<img src="https://www.google.com/s2/favicons?domain=' + this.href + '">');



                });
            }
        });
    });

});
</script>

hope this helps.

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

3 Comments

This is definitely on the right track, how do I place this within .prepend
You have to have the Ajax script URL bla render out the code in this: <?php echo the_main_image(); ?>. The <script> tag, which is on the page is previously rendered into the client & occurs before the Ajax call is made. The contents inside of the Ajax call are rendered out after the Ajax call is made. The right way to do this is to send the value up through the data object & then use $(this).prepend(data.something);.
@JoelRosen check my Edit, once u got the value in to the JS variable test u can use it anywhere u need like prepend in this case.
1

If your function in php does not echo result. Try to add echo. <?php echo the_main_image(); ?>. Seems like you forgot to echo result.

P.S. You can render results from php, even use it inside js code that placed in php files, but you should remember that mixin php with js is bad practice, and should be done cautiously.

How it works: php interpreter read your file before send static html to user and interpret all php related cod inside <?php ?>, if you echoing something it will appears in that row where you do this (or call function than do echoing). As mentioned by somebody if you echoing string with quotes it can break the js if you inline your php tags inside the same type of quotes.

2 Comments

the function might echo the output
Author did not mention. Function can just return string, if it return string it will not render result in that place. Usually if function render strings it has meaningful name for this. Your captain.
0

Try to use ajax to get php value in jquery funcion.

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.