1

I want to JQuery while defining a variable in PHP.

Suppose I have the following index.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Page</title>
    <script type="text/javascript" src="../../../resources/js/common/jquery-latest.min.js"></script>
</head>
<body>
    <div class="bigdaddy">
        <div class="daddy">
            <header class="main"></header>
            <main>
                <aside>
                </aside>
                <article>
                    <div class="div1">
                        Sample1
                    </div>
                    <div class="div2">
                        <div> //Child 1
                            Sample2
                        </div>
                        <p> //Child 2
                            Sample3
                        </p>
                    </div>
                    <?php
                        // the code
                    ?>
                </article>
            </main>
            <footer class="main"></footer>
        </div>
    </div>
</body>
</html>

Questions:

  • How can I select <div> with attribute and attribute value class="div1" to get its node/inner text?
  • How can I select <p> child of <div class="div2"> its node/inner text?
  • Is it even possible to use jQuery in PHP?

Both questions are using jQuery

I thought of

<?php
    $answer1 = $('div[class="div1"]').html();
    echo $answer1;
    $answer2 = $('div.class="div2"').children('p').html();
    echo $answer2;
?>

But it didn't work.

Expected result should be:

Sample1 Sample3

EDIT: I can't use AJAX.

alejnavab
  • 1,136
  • 1
  • 12
  • 30
  • Is it even possible? Should've been first question. – alejnavab Jan 04 '17 at 07:46
  • Your code is not working because you are trying to use jQuery in PHP. If you need to pass values to PHP you should to get them via jQuery and use ajax to pass them to PHP script. – Blady214 Jan 04 '17 at 07:47
  • No its not possible. Or at least not how you try. You can't use Javascript inside of PHP. Javascript is running clientside, PHP is serverside. They can't communicate the way you try. You have to pass the values from JS to PHP with for example AJAX. Edit: Blady was faster :D – Twinfriends Jan 04 '17 at 07:49
  • 1
    You need to understand the client and server. all the javascript/jquery code runs in browser which happens very late as the response from the server already ran and displayed at browser. – Jai Jan 04 '17 at 07:50
  • Edit: added question "is it possible" so everyone can actually answer. – alejnavab Jan 04 '17 at 07:50
  • @alej27 what you are trying to do is not possible unless you use ajax to send data back to server. – Jai Jan 04 '17 at 07:51
  • Do you know then how could I select the inner HTML of `

    ` inside `

    `? Using PHP.
    – alejnavab Jan 04 '17 at 07:53
  • @alej27, why can't you use AJAX, if you use jQuery you can use AJAX also so your edit to the question is invalid as long as you use jQuery. – Ionut Necula Jan 04 '17 at 08:02
  • "Edit: I can't use AJAX." Why? You're using JQuery too. So it won't be a problem of the system. The only reason why I can imagine you wrote this is that you've never heard about AJAX or you're simply to lazy to read more about it to learn it. There's really no good reason why you shouldn't be able to use it. If I'm wrong, correct me. – Twinfriends Jan 04 '17 at 08:08
  • Have you put it inside `` tags? – A. L Jan 04 '17 at 08:19
  • 1
    Take a read at this: http://www.sqa.org.uk/e-learning/ClientSide01CD/page_18.htm – Mattia Nocerino Jan 04 '17 at 08:19
  • I know AJAX. I was using it, but Google bot wasn't obtaining the content generated from it. I asked about that [in this Google products forum post](https://productforums.google.com/forum/#!topic/webmasters/oouCcKurc1Y). [**This is**](https://lh3.googleusercontent.com/-TfOjlmeddYk/WGxGw1SrG-I/AAAAAAAAH2o/g-Hcy9M_KqEBF6eCNwk6JU08tvdVmqvUACLcB/s1600/page-viewed-by-user.png) how my website looks like, but [**this is**](https://lh3.googleusercontent.com/-X_BvY9a4mFM/WGxIe9VJLGI/AAAAAAAAH20/21W_t-n3cfEvXumlTBa7qPhG1j-XvgtfwCLcB/s1600/page-viewed-by-googlebot-desktop.png) how Google sees it. – alejnavab Jan 04 '17 at 08:27
  • @alej27, if this is a SEO problem you need to know that this is offtopic for this website. – Ionut Necula Jan 04 '17 at 08:36
  • In their tour page (http://stackoverflow.com/tour) they say this page is about programming questions. Why is this offtopic? Where do you suggest to ask it? – alejnavab Jan 04 '17 at 08:40
  • @alej27, SEO questions should be asked on http://webmasters.stackexchange.com/ tagged with `seo` – Ionut Necula Jan 04 '17 at 08:44
  • Still it isn't a SEO problem, not only. I'm visiting the website on mobile and it isn't loading (only on iPhone, not on Android nor Windows Phone). **However**, on desktop is fine. So it could be a SEO **and** mobile problem. – alejnavab Jan 04 '17 at 08:48
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – mickmackusa Aug 05 '18 at 12:48

1 Answers1

2

You can use AJAX to send the data you want to the server, and handle the rest using PHP:

var answer1 = $('.div1').text();
var answer2 = $('.div2').find('p').text();
$.ajax({
  type: 'post',
  url: 'your_php_script.php',
  data: {
    answer1 : answer1, answer2: answer2
  },
  success: function(response_from_php_script) {
    console.log(response_from_php_script);
  }
});

And in your PHP script you can get the value of the paragraph you need like this:

 $answer1 = $_POST['answer1'];
 echo $answer1;
 $answer2 = $_POST['answer2'];
 echo $answer2;

Of course you will have to sanitize the post values if you want to insert it in the database, but that's a whole new thing.

I hope this will get you started.

You can read more about AJAX here.

Ionut Necula
  • 11,107
  • 4
  • 45
  • 69